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
/
sheet_test.go
505 lines (424 loc) · 20.1 KB
/
sheet_test.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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
package xlsx
import (
"bytes"
"encoding/xml"
. "gopkg.in/check.v1"
)
type SheetSuite struct{}
var _ = Suite(&SheetSuite{})
// Test we can add a Row to a Sheet
func (s *SheetSuite) TestAddRow(c *C) {
// Create a file with three rows.
var f *File
f = NewFile()
sheet, _ := f.AddSheet("MySheet")
row0 := sheet.AddRow()
cell0 := row0.AddCell()
cell0.Value = "Row 0"
c.Assert(row0, NotNil)
row1 := sheet.AddRow()
cell1 := row1.AddCell()
cell1.Value = "Row 1"
row2 := sheet.AddRow()
cell2 := row2.AddCell()
cell2.Value = "Row 2"
// Check the file
expected := []string{"Row 0", "Row 1", "Row 2"}
c.Assert(len(sheet.Rows), Equals, len(expected))
for i, row := range sheet.Rows {
c.Assert(row.Cells[0].Value, Equals, expected[i])
}
// Insert a row in the middle
row1pt5, err := sheet.AddRowAtIndex(2)
c.Assert(err, IsNil)
cell1pt5 := row1pt5.AddCell()
cell1pt5.Value = "Row 1.5"
expected = []string{"Row 0", "Row 1", "Row 1.5", "Row 2"}
c.Assert(len(sheet.Rows), Equals, len(expected))
for i, row := range sheet.Rows {
c.Assert(row.Cells[0].Value, Equals, expected[i])
}
// Insert a row at the beginning
rowNewStart, err := sheet.AddRowAtIndex(0)
c.Assert(err, IsNil)
cellNewStart := rowNewStart.AddCell()
cellNewStart.Value = "Row -1"
// Insert a row at one index past the end, this is the same as AddRow().
row2pt5, err := sheet.AddRowAtIndex(5)
c.Assert(err, IsNil)
cell2pt5 := row2pt5.AddCell()
cell2pt5.Value = "Row 2.5"
expected = []string{"Row -1", "Row 0", "Row 1", "Row 1.5", "Row 2", "Row 2.5"}
c.Assert(len(sheet.Rows), Equals, len(expected))
for i, row := range sheet.Rows {
c.Assert(row.Cells[0].Value, Equals, expected[i])
}
// Negative and out of range indicies should fail for insert
_, err = sheet.AddRowAtIndex(-1)
c.Assert(err, NotNil)
// Since we allow inserting into the position that does not yet exist, it has to be 1 greater
// than you would think in order to fail.
_, err = sheet.AddRowAtIndex(7)
c.Assert(err, NotNil)
// Negative and out of range indicies should fail for remove
err = sheet.RemoveRowAtIndex(-1)
c.Assert(err, NotNil)
err = sheet.RemoveRowAtIndex(6)
c.Assert(err, NotNil)
// Remove from the beginning, the end, and the middle.
err = sheet.RemoveRowAtIndex(0)
c.Assert(err, IsNil)
err = sheet.RemoveRowAtIndex(4)
c.Assert(err, IsNil)
err = sheet.RemoveRowAtIndex(2)
c.Assert(err, IsNil)
expected = []string{"Row 0", "Row 1", "Row 2"}
c.Assert(len(sheet.Rows), Equals, len(expected))
for i, row := range sheet.Rows {
c.Assert(row.Cells[0].Value, Equals, expected[i])
}
}
// Test we can get row by index from Sheet
func (s *SheetSuite) TestGetRowByIndex(c *C) {
var f *File
f = NewFile()
sheet, _ := f.AddSheet("MySheet")
row := sheet.Row(10)
c.Assert(row, NotNil)
c.Assert(len(sheet.Rows), Equals, 11)
row = sheet.Row(2)
c.Assert(row, NotNil)
c.Assert(len(sheet.Rows), Equals, 11)
}
func (s *SheetSuite) TestMakeXLSXSheetFromRows(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = "A cell!"
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
xSheet := sheet.makeXLSXSheet(refTable, styles)
c.Assert(xSheet.Dimension.Ref, Equals, "A1")
c.Assert(xSheet.SheetData.Row, HasLen, 1)
xRow := xSheet.SheetData.Row[0]
c.Assert(xRow.R, Equals, 1)
c.Assert(xRow.Spans, Equals, "")
c.Assert(xRow.C, HasLen, 1)
xC := xRow.C[0]
c.Assert(xC.R, Equals, "A1")
c.Assert(xC.S, Equals, 0)
c.Assert(xC.T, Equals, "s") // Shared string type
c.Assert(xC.V, Equals, "0") // reference to shared string
xSST := refTable.makeXLSXSST()
c.Assert(xSST.Count, Equals, 1)
c.Assert(xSST.UniqueCount, Equals, 1)
c.Assert(xSST.SI, HasLen, 1)
xSI := xSST.SI[0]
c.Assert(xSI.T, Equals, "A cell!")
}
// Test if the NumFmts assigned properly according the FormatCode in cell.
func (s *SheetSuite) TestMakeXLSXSheetWithNumFormats(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell1 := row.AddCell()
cell1.Value = "A cell!"
cell1.NumFmt = "general"
cell2 := row.AddCell()
cell2.Value = "37947.7500001"
cell2.NumFmt = "0"
cell3 := row.AddCell()
cell3.Value = "37947.7500001"
cell3.NumFmt = "mm-dd-yy"
cell4 := row.AddCell()
cell4.Value = "37947.7500001"
cell4.NumFmt = "hh:mm:ss"
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
worksheet := sheet.makeXLSXSheet(refTable, styles)
c.Assert(styles.CellStyleXfs, IsNil)
c.Assert(styles.CellXfs.Count, Equals, 4)
c.Assert(styles.CellXfs.Xf[0].NumFmtId, Equals, 0)
c.Assert(styles.CellXfs.Xf[1].NumFmtId, Equals, 1)
c.Assert(styles.CellXfs.Xf[2].NumFmtId, Equals, 14)
c.Assert(styles.CellXfs.Xf[3].NumFmtId, Equals, 164)
c.Assert(styles.NumFmts.Count, Equals, 1)
c.Assert(styles.NumFmts.NumFmt[0].NumFmtId, Equals, 164)
c.Assert(styles.NumFmts.NumFmt[0].FormatCode, Equals, "hh:mm:ss")
// Finally we check that the cell points to the right CellXf /
// CellStyleXf.
c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 0)
c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
}
// When we create the xlsxSheet we also populate the xlsxStyles struct
// with style information.
func (s *SheetSuite) TestMakeXLSXSheetAlsoPopulatesXLSXSTyles(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell1 := row.AddCell()
cell1.Value = "A cell!"
style1 := NewStyle()
style1.Font = *NewFont(10, "Verdana")
style1.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
style1.Border = *NewBorder("none", "thin", "none", "thin")
cell1.SetStyle(style1)
// We need a second style to check that Xfs are populated correctly.
cell2 := row.AddCell()
cell2.Value = "Another cell!"
style2 := NewStyle()
style2.Font = *NewFont(10, "Verdana")
style2.Fill = *NewFill("solid", "FFFFFFFF", "00000000")
style2.Border = *NewBorder("none", "thin", "none", "thin")
cell2.SetStyle(style2)
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
worksheet := sheet.makeXLSXSheet(refTable, styles)
c.Assert(styles.Fonts.Count, Equals, 2)
c.Assert(styles.Fonts.Font[0].Sz.Val, Equals, "12")
c.Assert(styles.Fonts.Font[0].Name.Val, Equals, "Verdana")
c.Assert(styles.Fonts.Font[1].Sz.Val, Equals, "10")
c.Assert(styles.Fonts.Font[1].Name.Val, Equals, "Verdana")
c.Assert(styles.Fills.Count, Equals, 3)
c.Assert(styles.Fills.Fill[0].PatternFill.PatternType, Equals, "none")
c.Assert(styles.Fills.Fill[0].PatternFill.FgColor.RGB, Equals, "")
c.Assert(styles.Fills.Fill[0].PatternFill.BgColor.RGB, Equals, "")
c.Assert(styles.Borders.Count, Equals, 2)
c.Assert(styles.Borders.Border[1].Left.Style, Equals, "none")
c.Assert(styles.Borders.Border[1].Right.Style, Equals, "thin")
c.Assert(styles.Borders.Border[1].Top.Style, Equals, "none")
c.Assert(styles.Borders.Border[1].Bottom.Style, Equals, "thin")
c.Assert(styles.CellStyleXfs, IsNil)
c.Assert(styles.CellXfs.Count, Equals, 2)
c.Assert(styles.CellXfs.Xf[0].FontId, Equals, 0)
c.Assert(styles.CellXfs.Xf[0].FillId, Equals, 0)
c.Assert(styles.CellXfs.Xf[0].BorderId, Equals, 0)
// Finally we check that the cell points to the right CellXf /
// CellStyleXf.
c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 1)
c.Assert(worksheet.SheetData.Row[0].C[1].S, Equals, 1)
}
// If the column width is not customised, the xslxCol.CustomWidth field is set to 0.
func (s *SheetSuite) TestMakeXLSXSheetDefaultsCustomColWidth(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell1 := row.AddCell()
cell1.Value = "A cell!"
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
worksheet := sheet.makeXLSXSheet(refTable, styles)
c.Assert(worksheet.Cols.Col[0].CustomWidth, Equals, false)
}
// If the column width is customised, the xslxCol.CustomWidth field is set to 1.
func (s *SheetSuite) TestMakeXLSXSheetSetsCustomColWidth(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell1 := row.AddCell()
cell1.Value = "A cell!"
err := sheet.SetColWidth(0, 1, 10.5)
c.Assert(err, IsNil)
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
worksheet := sheet.makeXLSXSheet(refTable, styles)
c.Assert(worksheet.Cols.Col[1].CustomWidth, Equals, true)
}
func (s *SheetSuite) TestMarshalSheet(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = "A cell!"
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
xSheet := sheet.makeXLSXSheet(refTable, styles)
output := bytes.NewBufferString(xml.Header)
body, err := xml.Marshal(xSheet)
c.Assert(err, IsNil)
c.Assert(body, NotNil)
_, err = output.Write(body)
c.Assert(err, IsNil)
expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetPr filterMode="false"><pageSetUpPr fitToPage="false"></pageSetUpPr></sheetPr><dimension ref="A1"></dimension><sheetViews><sheetView windowProtection="false" showFormulas="false" showGridLines="true" showRowColHeaders="true" showZeros="true" rightToLeft="false" tabSelected="true" showOutlineSymbols="true" defaultGridColor="true" view="normal" topLeftCell="A1" colorId="64" zoomScale="100" zoomScaleNormal="100" zoomScalePageLayoutView="100" workbookViewId="0"><selection pane="topLeft" activeCell="A1" activeCellId="0" sqref="A1"></selection></sheetView></sheetViews><sheetFormatPr defaultRowHeight="12.85"></sheetFormatPr><cols><col collapsed="false" hidden="false" max="1" min="1" style="0" width="9.5"></col></cols><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c></row></sheetData><printOptions headings="false" gridLines="false" gridLinesSet="true" horizontalCentered="false" verticalCentered="false"></printOptions><pageMargins left="0.7875" right="0.7875" top="1.05277777777778" bottom="1.05277777777778" header="0.7875" footer="0.7875"></pageMargins><pageSetup paperSize="9" scale="100" firstPageNumber="1" fitToWidth="1" fitToHeight="1" pageOrder="downThenOver" orientation="portrait" usePrinterDefaults="false" blackAndWhite="false" draft="false" cellComments="none" useFirstPageNumber="true" horizontalDpi="300" verticalDpi="300" copies="1"></pageSetup><headerFooter differentFirst="false" differentOddEven="false"><oddHeader>&C&"Times New Roman,Regular"&12&A</oddHeader><oddFooter>&C&"Times New Roman,Regular"&12Page &P</oddFooter></headerFooter></worksheet>`
c.Assert(output.String(), Equals, expectedXLSXSheet)
}
func (s *SheetSuite) TestMarshalSheetWithMultipleCells(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = "A cell (with value 1)!"
cell = row.AddCell()
cell.Value = "A cell (with value 2)!"
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
xSheet := sheet.makeXLSXSheet(refTable, styles)
output := bytes.NewBufferString(xml.Header)
body, err := xml.Marshal(xSheet)
c.Assert(err, IsNil)
c.Assert(body, NotNil)
_, err = output.Write(body)
c.Assert(err, IsNil)
expectedXLSXSheet := `<?xml version="1.0" encoding="UTF-8"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><sheetPr filterMode="false"><pageSetUpPr fitToPage="false"></pageSetUpPr></sheetPr><dimension ref="A1:B1"></dimension><sheetViews><sheetView windowProtection="false" showFormulas="false" showGridLines="true" showRowColHeaders="true" showZeros="true" rightToLeft="false" tabSelected="true" showOutlineSymbols="true" defaultGridColor="true" view="normal" topLeftCell="A1" colorId="64" zoomScale="100" zoomScaleNormal="100" zoomScalePageLayoutView="100" workbookViewId="0"><selection pane="topLeft" activeCell="A1" activeCellId="0" sqref="A1"></selection></sheetView></sheetViews><sheetFormatPr defaultRowHeight="12.85"></sheetFormatPr><cols><col collapsed="false" hidden="false" max="1" min="1" style="0" width="9.5"></col><col collapsed="false" hidden="false" max="2" min="2" style="0" width="9.5"></col></cols><sheetData><row r="1"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c></row></sheetData><printOptions headings="false" gridLines="false" gridLinesSet="true" horizontalCentered="false" verticalCentered="false"></printOptions><pageMargins left="0.7875" right="0.7875" top="1.05277777777778" bottom="1.05277777777778" header="0.7875" footer="0.7875"></pageMargins><pageSetup paperSize="9" scale="100" firstPageNumber="1" fitToWidth="1" fitToHeight="1" pageOrder="downThenOver" orientation="portrait" usePrinterDefaults="false" blackAndWhite="false" draft="false" cellComments="none" useFirstPageNumber="true" horizontalDpi="300" verticalDpi="300" copies="1"></pageSetup><headerFooter differentFirst="false" differentOddEven="false"><oddHeader>&C&"Times New Roman,Regular"&12&A</oddHeader><oddFooter>&C&"Times New Roman,Regular"&12Page &P</oddFooter></headerFooter></worksheet>`
c.Assert(output.String(), Equals, expectedXLSXSheet)
}
func (s *SheetSuite) TestSetColWidth(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
_ = sheet.SetColWidth(0, 0, 10.5)
_ = sheet.SetColWidth(1, 5, 11)
c.Assert(sheet.Cols[0].Width, Equals, 10.5)
c.Assert(sheet.Cols[0].Max, Equals, 1)
c.Assert(sheet.Cols[0].Min, Equals, 1)
c.Assert(sheet.Cols[1].Width, Equals, float64(11))
c.Assert(sheet.Cols[1].Max, Equals, 2)
c.Assert(sheet.Cols[1].Min, Equals, 2)
}
func (s *SheetSuite) TestSetRowHeightCM(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
row.SetHeightCM(1.5)
c.Assert(row.Height, Equals, 42.51968505)
}
func (s *SheetSuite) TestAlignment(c *C) {
leftalign := *DefaultAlignment()
leftalign.Horizontal = "left"
centerHalign := *DefaultAlignment()
centerHalign.Horizontal = "center"
rightalign := *DefaultAlignment()
rightalign.Horizontal = "right"
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
style := NewStyle()
hrow := sheet.AddRow()
// Horizontals
cell := hrow.AddCell()
cell.Value = "left"
style.Alignment = leftalign
style.ApplyAlignment = true
cell.SetStyle(style)
style = NewStyle()
cell = hrow.AddCell()
cell.Value = "centerH"
style.Alignment = centerHalign
style.ApplyAlignment = true
cell.SetStyle(style)
style = NewStyle()
cell = hrow.AddCell()
cell.Value = "right"
style.Alignment = rightalign
style.ApplyAlignment = true
cell.SetStyle(style)
// Verticals
topalign := *DefaultAlignment()
topalign.Vertical = "top"
centerValign := *DefaultAlignment()
centerValign.Vertical = "center"
bottomalign := *DefaultAlignment()
bottomalign.Vertical = "bottom"
style = NewStyle()
vrow := sheet.AddRow()
cell = vrow.AddCell()
cell.Value = "top"
style.Alignment = topalign
style.ApplyAlignment = true
cell.SetStyle(style)
style = NewStyle()
cell = vrow.AddCell()
cell.Value = "centerV"
style.Alignment = centerValign
style.ApplyAlignment = true
cell.SetStyle(style)
style = NewStyle()
cell = vrow.AddCell()
cell.Value = "bottom"
style.Alignment = bottomalign
style.ApplyAlignment = true
cell.SetStyle(style)
parts, err := file.MarshallParts()
c.Assert(err, IsNil)
obtained := parts["xl/styles.xml"]
shouldbe := `<?xml version="1.0" encoding="UTF-8"?>
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"><fonts count="1"><font><sz val="12"/><name val="Verdana"/><family val="0"/><charset val="0"/></font></fonts><fills count="2"><fill><patternFill patternType="none"/></fill><fill><patternFill patternType="lightGray"/></fill></fills><borders count="1"><border><left style="none"></left><right style="none"></right><top style="none"></top><bottom style="none"></bottom></border></borders><cellXfs count="8"><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf><xf applyAlignment="0" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="left" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="center" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="right" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="top" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="center" wrapText="0"/></xf><xf applyAlignment="1" applyBorder="0" applyFont="0" applyFill="0" applyNumberFormat="0" applyProtection="0" borderId="0" fillId="0" fontId="0" numFmtId="0"><alignment horizontal="general" indent="0" shrinkToFit="0" textRotation="0" vertical="bottom" wrapText="0"/></xf></cellXfs></styleSheet>`
expected := bytes.NewBufferString(shouldbe)
c.Assert(obtained, Equals, expected.String())
}
func (s *SheetSuite) TestBorder(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell1 := row.AddCell()
cell1.Value = "A cell!"
style1 := NewStyle()
style1.Border = *NewBorder("thin", "thin", "thin", "thin")
style1.ApplyBorder = true
cell1.SetStyle(style1)
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
worksheet := sheet.makeXLSXSheet(refTable, styles)
c.Assert(styles.Borders.Border[1].Left.Style, Equals, "thin")
c.Assert(styles.Borders.Border[1].Right.Style, Equals, "thin")
c.Assert(styles.Borders.Border[1].Top.Style, Equals, "thin")
c.Assert(styles.Borders.Border[1].Bottom.Style, Equals, "thin")
c.Assert(worksheet.SheetData.Row[0].C[0].S, Equals, 1)
}
func (s *SheetSuite) TestOutlineLevels(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
r1 := sheet.AddRow()
c11 := r1.AddCell()
c11.Value = "A1"
c12 := r1.AddCell()
c12.Value = "B1"
r2 := sheet.AddRow()
c21 := r2.AddCell()
c21.Value = "A2"
c22 := r2.AddCell()
c22.Value = "B2"
r3 := sheet.AddRow()
c31 := r3.AddCell()
c31.Value = "A3"
c32 := r3.AddCell()
c32.Value = "B3"
// Add some groups
r1.OutlineLevel = 1
r2.OutlineLevel = 2
sheet.Col(0).OutlineLevel = 1
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
worksheet := sheet.makeXLSXSheet(refTable, styles)
c.Assert(worksheet.SheetFormatPr.OutlineLevelCol, Equals, uint8(1))
c.Assert(worksheet.SheetFormatPr.OutlineLevelRow, Equals, uint8(2))
c.Assert(worksheet.Cols.Col[0].OutlineLevel, Equals, uint8(1))
c.Assert(worksheet.Cols.Col[1].OutlineLevel, Equals, uint8(0))
c.Assert(worksheet.SheetData.Row[0].OutlineLevel, Equals, uint8(1))
c.Assert(worksheet.SheetData.Row[1].OutlineLevel, Equals, uint8(2))
c.Assert(worksheet.SheetData.Row[2].OutlineLevel, Equals, uint8(0))
}
func (s *SheetSuite) TestAutoFilter(c *C) {
file := NewFile()
sheet, _ := file.AddSheet("Sheet1")
r1 := sheet.AddRow()
r1.AddCell()
r1.AddCell()
r1.AddCell()
r2 := sheet.AddRow()
r2.AddCell()
r2.AddCell()
r2.AddCell()
r3 := sheet.AddRow()
r3.AddCell()
r3.AddCell()
r3.AddCell()
// Define a filter area
sheet.AutoFilter = &AutoFilter{TopLeftCell: "B2", BottomRightCell: "C3"}
refTable := NewSharedStringRefTable()
styles := newXlsxStyleSheet(nil)
worksheet := sheet.makeXLSXSheet(refTable, styles)
c.Assert(worksheet.AutoFilter, NotNil)
c.Assert(worksheet.AutoFilter.Ref, Equals, "B2:C3")
}