-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
columns.go
74 lines (61 loc) · 1.83 KB
/
columns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright (c) 2017 Andrey Gayvoronsky <[email protected]>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlsx
import (
"github.com/plandem/xlsx/internal/ml"
)
type columns struct {
sheet *sheetInfo
}
//newColumns creates an object that implements columns functionality (don't miss with Col)
func newColumns(sheet *sheetInfo) *columns {
return &columns{sheet: sheet}
}
func (cols *columns) Resolve(index int) *ml.Col {
var data *ml.Col
//Cols has 1-based index, but we are using 0-based to unify all indexes at library
index++
for _, c := range cols.sheet.ml.Cols.Items {
//existing non-grouped column?
if c.Min == c.Max && c.Min == index {
data = c
break
}
}
if data == nil {
for _, c := range cols.sheet.ml.Cols.Items {
//mark grouped column as updated and create non-grouped column with same settings
if index >= c.Min && index <= c.Max {
data = &ml.Col{}
*data = *c
data.Min = index
data.Max = index
break
}
}
//if there was no any grouped column, then create a new one non-grouped
if data == nil {
data = &ml.Col{
Min: index,
Max: index,
}
}
cols.sheet.ml.Cols.Items = append(cols.sheet.ml.Cols.Items, data)
}
return data
}
func (cols *columns) Delete(index int) {
//Cols has 1-based index, but we are using 0-based to unify all indexes at library
index++
//N.B.: we can have few columns with same index - grouped and non-grouped, so both should be processed
for idx, c := range cols.sheet.ml.Cols.Items {
if c.Min == c.Max && c.Min == index {
//non-grouped column should be deleted
cols.sheet.ml.Cols.Items = append(cols.sheet.ml.Cols.Items[:idx], cols.sheet.ml.Cols.Items[idx+1:]...)
} else if index >= c.Min && index <= c.Max {
//grouped column should be only updated
c.Max--
}
}
}