This repository has been archived by the owner on Dec 12, 2024. It is now read-only.
forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
col.go
105 lines (93 loc) · 3.19 KB
/
col.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package xlsx
// Default column width in excel
const ColWidth = 9.5
const Excel2006MaxRowCount = 1048576
const Excel2006MaxRowIndex = Excel2006MaxRowCount - 1
type Col struct {
Min int
Max int
Hidden bool
Width float64
Collapsed bool
OutlineLevel uint8
numFmt string
parsedNumFmt *parsedNumberFormat
style *Style
DataValidation []*xlsxCellDataValidation
}
// SetType will set the format string of a column based on the type that you want to set it to.
// This function does not really make a lot of sense.
func (c *Col) SetType(cellType CellType) {
switch cellType {
case CellTypeString:
c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING]
case CellTypeNumeric:
c.numFmt = builtInNumFmt[builtInNumFmtIndex_INT]
case CellTypeBool:
c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP
case CellTypeInline:
c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING]
case CellTypeError:
c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL] //TEMP
case CellTypeDate:
// Cells that are stored as dates are not properly supported in this library.
// They should instead be stored as a Numeric with a date format.
c.numFmt = builtInNumFmt[builtInNumFmtIndex_GENERAL]
case CellTypeStringFormula:
c.numFmt = builtInNumFmt[builtInNumFmtIndex_STRING]
}
}
// GetStyle returns the Style associated with a Col
func (c *Col) GetStyle() *Style {
return c.style
}
// SetStyle sets the style of a Col
func (c *Col) SetStyle(style *Style) {
c.style = style
}
// SetDataValidation set data validation with zero based start and end.
// Set end to -1 for all rows.
func (c *Col) SetDataValidation(dd *xlsxCellDataValidation, start, end int) {
if end < 0 {
end = Excel2006MaxRowIndex
}
dd.minRow = start
dd.maxRow = end
tmpDD := make([]*xlsxCellDataValidation, 0)
for _, item := range c.DataValidation {
if item.maxRow < dd.minRow {
tmpDD = append(tmpDD, item) //No intersection
} else if item.minRow > dd.maxRow {
tmpDD = append(tmpDD, item) //No intersection
} else if dd.minRow <= item.minRow && dd.maxRow >= item.maxRow {
continue //union , item can be ignored
} else if dd.minRow >= item.minRow {
//Split into three or two, Newly added object, intersect with the current object in the lower half
tmpSplit := new(xlsxCellDataValidation)
*tmpSplit = *item
if dd.minRow > item.minRow { //header whetherneed to split
item.maxRow = dd.minRow - 1
tmpDD = append(tmpDD, item)
}
if dd.maxRow < tmpSplit.maxRow { //footer whetherneed to split
tmpSplit.minRow = dd.maxRow + 1
tmpDD = append(tmpDD, tmpSplit)
}
} else {
item.minRow = dd.maxRow + 1
tmpDD = append(tmpDD, item)
}
}
tmpDD = append(tmpDD, dd)
c.DataValidation = tmpDD
}
// SetDataValidationWithStart set data validation with a zero basd start row.
// This will apply to the rest of the rest of the column.
func (c *Col) SetDataValidationWithStart(dd *xlsxCellDataValidation, start int) {
c.SetDataValidation(dd, start, -1)
}
// SetStreamStyle sets the style and number format id to the ones specified in the given StreamStyle
func (c *Col) SetStreamStyle(style StreamStyle) {
c.style = style.style
c.numFmt = builtInNumFmt[style.xNumFmtId]
}