forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sheet.go
454 lines (401 loc) · 11.9 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
package xlsx
import (
"errors"
"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
File *File
Rows []*Row
Cols []*Col
MaxRow int
MaxCol int
Hidden bool
Selected bool
SheetViews []SheetView
SheetFormat SheetFormat
AutoFilter *AutoFilter
}
type SheetView struct {
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
OutlineLevelCol uint8
OutlineLevelRow uint8
}
type AutoFilter struct {
TopLeftCell string
BottomRightCell 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
}
// Add a new Row to a Sheet at a specific index
func (s *Sheet) AddRowAtIndex(index int) (*Row, error) {
if index < 0 || index > len(s.Rows) {
return nil, errors.New("AddRowAtIndex: index out of bounds")
}
row := &Row{Sheet: s}
s.Rows = append(s.Rows, nil)
if index < len(s.Rows) {
copy(s.Rows[index+1:], s.Rows[index:])
}
s.Rows[index] = row
if len(s.Rows) > s.MaxRow {
s.MaxRow = len(s.Rows)
}
return row, nil
}
// Removes a row at a specific index
func (s *Sheet) RemoveRowAtIndex(index int) error {
if index < 0 || index >= len(s.Rows) {
return errors.New("RemoveRowAtIndex: index out of bounds")
}
s.Rows = append(s.Rows[:index], s.Rows[index+1:]...)
return nil
}
// Make sure we always have as many Rows as we do cells.
func (s *Sheet) maybeAddRow(rowCount int) {
if rowCount > s.MaxRow {
loopCnt := rowCount - s.MaxRow
for i := 0; i < loopCnt; i++ {
row := &Row{Sheet: s}
s.Rows = append(s.Rows, row)
}
s.MaxRow = rowCount
}
}
// Make sure we always have as many Rows as we do cells.
func (s *Sheet) Row(idx int) *Row {
s.maybeAddRow(idx + 1)
return s.Rows[idx]
}
// Make sure we always have as many Cols as we do cells.
func (s *Sheet) maybeAddCol(cellCount int) {
if cellCount > s.MaxCol {
loopCnt := cellCount - s.MaxCol
currIndex := s.MaxCol + 1
for i := 0; i < loopCnt; i++ {
col := &Col{
style: NewStyle(),
Min: currIndex,
Max: currIndex,
Hidden: false,
Collapsed: false}
s.Cols = append(s.Cols, col)
currIndex++
}
s.MaxCol = cellCount
}
}
// Make sure we always have as many Cols as we do cells.
func (s *Sheet) Col(idx int) *Col {
s.maybeAddCol(idx + 1)
return s.Cols[idx]
}
// 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 the user requests a row beyond what we have, then extend.
for len(sh.Rows) <= row {
sh.AddRow()
}
r := sh.Rows[row]
for len(r.Cells) <= col {
r.AddCell()
}
return r.Cells[col]
}
//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)
}
end := endcol + 1
s.maybeAddCol(end)
for ; startcol < end; startcol++ {
s.Cols[startcol].Width = width
}
return nil
}
// When merging cells, the cell may be the 'original' or the 'covered'.
// First, figure out which cells are merge starting points. Then create
// the necessary cells underlying the merge area.
// Then go through all the underlying cells and apply the appropriate
// border, based on the original cell.
func (s *Sheet) handleMerged() {
merged := make(map[string]*Cell)
for r, row := range s.Rows {
for c, cell := range row.Cells {
if cell.HMerge > 0 || cell.VMerge > 0 {
coord := GetCellIDStringFromCoords(c, r)
merged[coord] = cell
}
}
}
// This loop iterates over all cells that should be merged and applies the correct
// borders to them depending on their position. If any cells required by the merge
// are missing, they will be allocated by s.Cell().
for key, cell := range merged {
maincol, mainrow, _ := GetCoordsFromCellIDString(key)
for rownum := 0; rownum <= cell.VMerge; rownum++ {
for colnum := 0; colnum <= cell.HMerge; colnum++ {
// make cell
s.Cell(mainrow+rownum, maincol+colnum)
}
}
}
}
// 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
var maxLevelCol, maxLevelRow uint8
// Scan through the sheet and see if there are any merged cells. If there
// are, we may need to extend the size of the sheet. There needs to be
// phantom cells underlying the area covered by the merged cell
s.handleMerged()
for index, sheetView := range s.SheetViews {
if sheetView.Pane != nil {
worksheet.SheetViews.SheetView[index].Pane = &xlsxPane{
XSplit: sheetView.Pane.XSplit,
YSplit: sheetView.Pane.YSplit,
TopLeftCell: sheetView.Pane.TopLeftCell,
ActivePane: sheetView.Pane.ActivePane,
State: sheetView.Pane.State,
}
}
}
if s.Selected {
worksheet.SheetViews.SheetView[0].TabSelected = true
}
if s.SheetFormat.DefaultRowHeight != 0 {
worksheet.SheetFormatPr.DefaultRowHeight = s.SheetFormat.DefaultRowHeight
}
worksheet.SheetFormatPr.DefaultColWidth = s.SheetFormat.DefaultColWidth
colsXfIdList := make([]int, len(s.Cols))
for c, col := range s.Cols {
XfId := 0
if col.Min == 0 {
col.Min = 1
}
if col.Max == 0 {
col.Max = 1
}
style := col.GetStyle()
//col's style always not nil
if style != nil {
xNumFmt := styles.newNumFmt(col.numFmt)
XfId = handleStyleForXLSX(style, xNumFmt.NumFmtId, styles)
}
colsXfIdList[c] = XfId
var customWidth bool
if col.Width == 0 {
col.Width = ColWidth
customWidth = false
} else {
customWidth = true
}
// When the cols content is empty, the cols flag is not output in the xml file.
if worksheet.Cols == nil {
worksheet.Cols = &xlsxCols{Col: []xlsxCol{}}
}
worksheet.Cols.Col = append(worksheet.Cols.Col,
xlsxCol{Min: col.Min,
Max: col.Max,
Hidden: col.Hidden,
Width: col.Width,
CustomWidth: customWidth,
Collapsed: col.Collapsed,
OutlineLevel: col.OutlineLevel,
Style: XfId,
})
if col.OutlineLevel > maxLevelCol {
maxLevelCol = col.OutlineLevel
}
if nil != col.DataValidation {
if nil == worksheet.DataValidations {
worksheet.DataValidations = &xlsxCellDataValidations{}
}
colName := ColIndexToLetters(c)
for _, dd := range col.DataValidation {
if dd.minRow == dd.maxRow {
dd.Sqref = colName + RowIndexToString(dd.minRow)
} else {
dd.Sqref = colName + RowIndexToString(dd.minRow) + cellRangeChar + colName + RowIndexToString(dd.maxRow)
}
worksheet.DataValidations.DataValidation = append(worksheet.DataValidations.DataValidation, dd)
}
worksheet.DataValidations.Count = len(worksheet.DataValidations.DataValidation)
}
}
for r, row := range s.Rows {
if r > maxRow {
maxRow = r
}
xRow := xlsxRow{}
xRow.R = r + 1
if row.isCustom {
xRow.CustomHeight = true
xRow.Ht = fmt.Sprintf("%g", row.Height)
}
xRow.OutlineLevel = row.OutlineLevel
if row.OutlineLevel > maxLevelRow {
maxLevelRow = row.OutlineLevel
}
for c, cell := range row.Cells {
XfId := colsXfIdList[c]
// generate NumFmtId and add new NumFmt
xNumFmt := styles.newNumFmt(cell.NumFmt)
style := cell.style
if style != nil {
XfId = handleStyleForXLSX(style, xNumFmt.NumFmtId, styles)
} else if len(cell.NumFmt) > 0 && !compareFormatString(s.Cols[c].numFmt, cell.NumFmt) {
XfId = handleNumFmtIdForXLSX(xNumFmt.NumFmtId, styles)
}
if c > maxCell {
maxCell = c
}
xC := xlsxC{
S: XfId,
R: GetCellIDStringFromCoords(c, r),
}
if cell.formula != "" {
xC.F = &xlsxF{Content: cell.formula}
}
switch cell.cellType {
case CellTypeInline:
// Inline strings are turned into shared strings since they are more efficient.
// This is what Excel does as well.
fallthrough
case CellTypeString:
if len(cell.Value) > 0 {
xC.V = strconv.Itoa(refTable.AddString(cell.Value))
}
xC.T = "s"
case CellTypeNumeric:
// Numeric is the default, so the type can be left blank
xC.V = cell.Value
case CellTypeBool:
xC.V = cell.Value
xC.T = "b"
case CellTypeError:
xC.V = cell.Value
xC.T = "e"
case CellTypeDate:
xC.V = cell.Value
xC.T = "d"
case CellTypeStringFormula:
xC.V = cell.Value
xC.T = "str"
default:
panic(errors.New("unknown cell type cannot be marshaled"))
}
xRow.C = append(xRow.C, xC)
if nil != cell.DataValidation {
if nil == worksheet.DataValidations {
worksheet.DataValidations = &xlsxCellDataValidations{}
}
cell.DataValidation.Sqref = xC.R
worksheet.DataValidations.DataValidation = append(worksheet.DataValidations.DataValidation, cell.DataValidation)
worksheet.DataValidations.Count = len(worksheet.DataValidations.DataValidation)
}
if cell.HMerge > 0 || cell.VMerge > 0 {
// r == rownum, c == colnum
mc := xlsxMergeCell{}
start := GetCellIDStringFromCoords(c, r)
endCol := c + cell.HMerge
endRow := r + cell.VMerge
end := GetCellIDStringFromCoords(endCol, endRow)
mc.Ref = start + cellRangeChar + end
if worksheet.MergeCells == nil {
worksheet.MergeCells = &xlsxMergeCells{}
}
worksheet.MergeCells.Cells = append(worksheet.MergeCells.Cells, mc)
}
}
xSheet.Row = append(xSheet.Row, xRow)
}
// Update sheet format with the freshly determined max levels
s.SheetFormat.OutlineLevelCol = maxLevelCol
s.SheetFormat.OutlineLevelRow = maxLevelRow
// .. and then also apply this to the xml worksheet
worksheet.SheetFormatPr.OutlineLevelCol = s.SheetFormat.OutlineLevelCol
worksheet.SheetFormatPr.OutlineLevelRow = s.SheetFormat.OutlineLevelRow
if worksheet.MergeCells != nil {
worksheet.MergeCells.Count = len(worksheet.MergeCells.Cells)
}
if s.AutoFilter != nil {
worksheet.AutoFilter = &xlsxAutoFilter{Ref: fmt.Sprintf("%v:%v", s.AutoFilter.TopLeftCell, s.AutoFilter.BottomRightCell)}
}
worksheet.SheetData = xSheet
dimension := xlsxDimension{}
dimension.Ref = "A1:" + GetCellIDStringFromCoords(maxCell, maxRow)
if dimension.Ref == "A1:A1" {
dimension.Ref = "A1"
}
worksheet.Dimension = dimension
return worksheet
}
func handleStyleForXLSX(style *Style, NumFmtId int, styles *xlsxStyleSheet) (XfId int) {
xFont, xFill, xBorder, xCellXf := style.makeXLSXStyleElements()
fontId := styles.addFont(xFont)
fillId := styles.addFill(xFill)
// HACK - adding light grey fill, as in OO and Google
greyfill := xlsxFill{}
greyfill.PatternFill.PatternType = "lightGray"
styles.addFill(greyfill)
borderId := styles.addBorder(xBorder)
xCellXf.FontId = fontId
xCellXf.FillId = fillId
xCellXf.BorderId = borderId
xCellXf.NumFmtId = NumFmtId
// apply the numFmtId when it is not the default cellxf
if xCellXf.NumFmtId > 0 {
xCellXf.ApplyNumberFormat = true
}
xCellXf.Alignment.Horizontal = style.Alignment.Horizontal
xCellXf.Alignment.Indent = style.Alignment.Indent
xCellXf.Alignment.ShrinkToFit = style.Alignment.ShrinkToFit
xCellXf.Alignment.TextRotation = style.Alignment.TextRotation
xCellXf.Alignment.Vertical = style.Alignment.Vertical
xCellXf.Alignment.WrapText = style.Alignment.WrapText
XfId = styles.addCellXf(xCellXf)
return
}
func handleNumFmtIdForXLSX(NumFmtId int, styles *xlsxStyleSheet) (XfId int) {
xCellXf := makeXLSXCellElement()
xCellXf.NumFmtId = NumFmtId
if xCellXf.NumFmtId > 0 {
xCellXf.ApplyNumberFormat = true
}
XfId = styles.addCellXf(xCellXf)
return
}