forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsheet.go
293 lines (265 loc) · 6.47 KB
/
sheet.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package xlsx
import (
"fmt"
"strconv"
)
// Sheet is a high level structure intended to provide user access to
// the contents of a particular sheet within an XLSX file.
type Sheet struct {
Name string
SheetId string
File *File
Rows []*Row
Cols []*Col
MaxRow int
MaxCol int
Hidden bool
SheetViews []SheetView
SheetFormat SheetFormat
MergeCells []MergeCell
HasDrawing bool
Comments []Comment
Tables []Table
PivotTables []PivotTable
Drawings []Drawing
CfRules []CfRule
}
type SheetView struct {
ShowGridLines bool
Pane *Pane
}
type Pane struct {
XSplit float64
YSplit float64
TopLeftCell string
ActivePane string
State string // Either "split" or "frozen"
}
type SheetFormat struct {
DefaultColWidth float64
DefaultRowHeight float64
}
type MergeCell struct {
StartRef, EndRef string
}
type Comment struct {
Ref string
Text string
}
type Table struct {
StartRef, EndRef string
TotalsRowCount int
TableStyleInfo TableStyleInfo
}
type TableStyleInfo struct {
Name string
ShowFirstCol bool
ShowLastCol bool
ShowRowStripes bool
ShowColStripes bool
}
type PivotTable struct {
StartRef, EndRef string
RowItems []RowItem
PivotTableStyleInfo PivotTableStyleInfo
}
type RowItem struct {
Type string
}
type PivotTableStyleInfo struct {
Name string
ShowRowStripes bool
ShowColStripes bool
}
type Drawing struct {
From, To Pos
Pic *Pic
}
type Pos struct {
Col, ColOff, Row, RowOff int
}
type Pic struct {
Image []byte
X, Y, CX, CY int
Href string
}
type CfRule struct {
Ref, Type, Operator string
Priority int
StyleDiff *StyleDiff
Formula string
ColorScale []CfScaleColor
}
type CfScaleColor struct {
Type string
Val float64
Color string
}
// Add a new Row to a Sheet
func (s *Sheet) AddRow() *Row {
row := &Row{Sheet: s}
s.Rows = append(s.Rows, row)
if len(s.Rows) > s.MaxRow {
s.MaxRow = len(s.Rows)
}
return row
}
// Make sure we always have as many Cols as we do cells.
func (s *Sheet) maybeAddCol(cellCount int) {
if cellCount > s.MaxCol {
col := &Col{
Min: cellCount,
Max: cellCount,
Hidden: false,
Collapsed: false,
// Style: 0,
Width: ColWidth}
s.Cols = append(s.Cols, col)
s.MaxCol = cellCount
}
}
// Get a Cell by passing it's cartesian coordinates (zero based) as
// row and column integer indexes.
//
// For example:
//
// cell := sheet.Cell(0,0)
//
// ... would set the variable "cell" to contain a Cell struct
// containing the data from the field "A1" on the spreadsheet.
func (sh *Sheet) Cell(row, col int) *Cell {
if len(sh.Rows) > row && sh.Rows[row] != nil && len(sh.Rows[row].Cells) > col {
return sh.Rows[row].Cells[col]
}
return new(Cell)
}
//Set the width of a single column or multiple columns.
func (s *Sheet) SetColWidth(startcol, endcol int, width float64) error {
if startcol > endcol {
return fmt.Errorf("Could not set width for range %d-%d: startcol must be less than endcol.", startcol, endcol)
}
col := &Col{
Min: startcol + 1,
Max: endcol + 1,
Hidden: false,
Collapsed: false,
// Style: 0,
Width: width}
s.Cols = append(s.Cols, col)
if endcol+1 > s.MaxCol {
s.MaxCol = endcol + 1
}
return nil
}
// Dump sheet to its XML representation, intended for internal use only
func (s *Sheet) makeXLSXSheet(refTable *RefTable, styles *xlsxStyleSheet) *xlsxWorksheet {
worksheet := newXlsxWorksheet()
xSheet := xlsxSheetData{}
maxRow := 0
maxCell := 0
XfId := 0
for r, row := range s.Rows {
if r > maxRow {
maxRow = r
}
xRow := xlsxRow{}
xRow.R = r + 1
for c, cell := range row.Cells {
style := cell.GetStyle()
if style != nil {
xFont, xFill, xBorder, xCellStyleXf, xCellXf := style.makeXLSXStyleElements()
fontId := styles.addFont(xFont)
fillId := styles.addFill(xFill)
// generate NumFmtId and add new NumFmt
xNumFmt := styles.newNumFmt(cell.numFmt)
// HACK - adding light grey fill, as in OO and Google
greyfill := xlsxFill{}
greyfill.PatternFill.PatternType = "lightGrey"
styles.addFill(greyfill)
borderId := styles.addBorder(xBorder)
xCellStyleXf.FontId = fontId
xCellStyleXf.FillId = fillId
xCellStyleXf.BorderId = borderId
xCellStyleXf.NumFmtId = 0 // General
xCellXf.FontId = fontId
xCellXf.FillId = fillId
xCellXf.BorderId = borderId
xCellXf.NumFmtId = xNumFmt.NumFmtId
// apply the numFmtId when it is not the default cellxf
if xCellXf.NumFmtId > 0 {
xCellXf.ApplyNumberFormat = true
}
styles.addCellStyleXf(xCellStyleXf)
XfId = styles.addCellXf(xCellXf)
}
if c > maxCell {
maxCell = c
}
xC := xlsxC{}
xC.R = fmt.Sprintf("%s%d", numericToLetters(c), r+1)
switch cell.cellType {
case CellTypeString:
xC.V = strconv.Itoa(refTable.AddString(cell.Value))
xC.T = "s"
xC.S = XfId
case CellTypeBool:
xC.V = cell.Value
xC.T = "b"
xC.S = XfId
case CellTypeNumeric:
xC.V = cell.Value
xC.S = XfId
case CellTypeFormula:
xC.V = cell.Value
xC.F = &xlsxF{Content: cell.formula}
xC.S = XfId
case CellTypeError:
xC.V = cell.Value
xC.F = &xlsxF{Content: cell.formula}
xC.T = "e"
xC.S = XfId
}
xRow.C = append(xRow.C, xC)
if cell.HMerge > 0 || cell.VMerge > 0 {
// r == rownum, c == colnum
mc := xlsxMergeCell{}
start := fmt.Sprintf("%s%d", numericToLetters(c), r+1)
endcol := c + cell.HMerge
endrow := r + cell.VMerge + 1
end := fmt.Sprintf("%s%d", numericToLetters(endcol), endrow)
mc.Ref = start + ":" + end
if worksheet.MergeCells == nil {
worksheet.MergeCells = &xlsxMergeCells{}
}
worksheet.MergeCells.Cells = append(worksheet.MergeCells.Cells, mc)
}
}
xSheet.Row = append(xSheet.Row, xRow)
}
if worksheet.MergeCells != nil {
worksheet.MergeCells.Count = len(worksheet.MergeCells.Cells)
}
worksheet.Cols = xlsxCols{Col: []xlsxCol{}}
for _, col := range s.Cols {
if col.Width == 0 {
col.Width = ColWidth
}
worksheet.Cols.Col = append(worksheet.Cols.Col,
xlsxCol{Min: col.Min,
Max: col.Max,
Hidden: col.Hidden,
Width: col.Width,
Collapsed: col.Collapsed,
// Style: col.Style
})
}
worksheet.SheetData = xSheet
dimension := xlsxDimension{}
dimension.Ref = fmt.Sprintf("A1:%s%d",
numericToLetters(maxCell), maxRow+1)
if dimension.Ref == "A1:A1" {
dimension.Ref = "A1"
}
worksheet.Dimension = dimension
return worksheet
}