forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib_test.go
837 lines (789 loc) · 23.2 KB
/
lib_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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
package xlsx
import (
"bytes"
"encoding/xml"
"strconv"
"strings"
"testing"
)
// Test we can correctly open a XSLX file and return a xlsx.File
// struct.
func TestOpenFile(t *testing.T) {
var xlsxFile *File
var error error
xlsxFile, error = OpenFile("testfile.xlsx")
if error != nil {
t.Error(error.Error())
return
}
if xlsxFile == nil {
t.Error("OpenFile returned nil FileInterface without generating an os.Error")
return
}
}
// Test that when we open a real XLSX file we create xlsx.Sheet
// objects for the sheets inside the file and that these sheets are
// themselves correct.
func TestCreateSheet(t *testing.T) {
var xlsxFile *File
var error error
var sheet *Sheet
var row *Row
xlsxFile, error = OpenFile("testfile.xlsx")
if error != nil {
t.Error(error.Error())
return
}
if xlsxFile == nil {
t.Error("OpenFile returned a nil File pointer but did not generate an error.")
return
}
sheetLen := len(xlsxFile.Sheets)
if sheetLen == 0 {
t.Error("Expected len(xlsxFile.Sheets) > 0, but got ", sheetLen)
return
}
sheet = xlsxFile.Sheets[0]
rowLen := len(sheet.Rows)
if rowLen != 2 {
t.Error("Expected len(sheet.Rows) == 2, but got ", rowLen)
return
}
row = sheet.Rows[0]
if len(row.Cells) != 2 {
t.Error("Expected len(row.Cells) == 2")
return
}
cell := row.Cells[0]
cellstring := cell.String()
if cellstring != "Foo" {
t.Error("Expected cell.String() == 'Foo', got ", cellstring)
}
}
// Test that GetStyle correctly converts the xlsxStyle.Fonts.
func TestGetStyleWithFonts(t *testing.T) {
var cell *Cell
var style *Style
var xStyles *xlsxStyles
var fonts []xlsxFont
var cellXfs []xlsxXf
fonts = make([]xlsxFont, 1)
fonts[0] = xlsxFont{
Sz: xlsxVal{Val: "10"},
Name: xlsxVal{Val: "Calibra"}}
cellXfs = make([]xlsxXf, 1)
cellXfs[0] = xlsxXf{ApplyFont: true, FontId: 0}
xStyles = &xlsxStyles{Fonts: fonts, CellXfs: cellXfs}
cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
style = cell.GetStyle()
if style == nil {
t.Error("No style returned by cell.GetStyle()")
}
if style.Font.Size != 10 {
t.Error("Expected style.Font.Size == 10, but got ", style.Font.Size)
}
if style.Font.Name != "Calibra" {
t.Error("Expected style.Font.Name == 'Calibra', but got ", style.Font.Name)
}
}
// Test that GetStyle correctly converts the xlsxStyle.Fills.
func TestGetStyleWithFills(t *testing.T) {
var cell *Cell
var style *Style
var xStyles *xlsxStyles
var fills []xlsxFill
var cellXfs []xlsxXf
fills = make([]xlsxFill, 1)
fills[0] = xlsxFill{
PatternFill: xlsxPatternFill{
PatternType: "solid",
FgColor: xlsxColor{RGB: "FF000000"},
BgColor: xlsxColor{RGB: "00FF0000"}}}
cellXfs = make([]xlsxXf, 1)
cellXfs[0] = xlsxXf{ApplyFill: true, FillId: 0}
xStyles = &xlsxStyles{Fills: fills, CellXfs: cellXfs}
cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
style = cell.GetStyle()
fill := style.Fill
if fill.PatternType != "solid" {
t.Error("Expected fill.PatternType == 'solid', but got ",
fill.PatternType)
}
if fill.BgColor != "00FF0000" {
t.Error("Expected fill.BgColor == '00FF0000', but got ",
fill.BgColor)
}
if fill.FgColor != "FF000000" {
t.Error("Expected fill.FgColor == 'FF000000', but got ",
fill.FgColor)
}
}
// Test that GetStyle correctly converts the xlsxStyle.Borders.
func TestGetStyleWithBorders(t *testing.T) {
var cell *Cell
var style *Style
var xStyles *xlsxStyles
var borders []xlsxBorder
var cellXfs []xlsxXf
borders = make([]xlsxBorder, 1)
borders[0] = xlsxBorder{
Left: xlsxLine{Style: "thin"},
Right: xlsxLine{Style: "thin"},
Top: xlsxLine{Style: "thin"},
Bottom: xlsxLine{Style: "thin"}}
cellXfs = make([]xlsxXf, 1)
cellXfs[0] = xlsxXf{ApplyBorder: true, BorderId: 0}
xStyles = &xlsxStyles{Borders: borders, CellXfs: cellXfs}
cell = &Cell{Value: "123", styleIndex: 1, styles: xStyles}
style = cell.GetStyle()
border := style.Border
if border.Left != "thin" {
t.Error("Expected border.Left == 'thin', but got ",
border.Left)
}
if border.Right != "thin" {
t.Error("Expected border.Right == 'thin', but got ",
border.Right)
}
if border.Top != "thin" {
t.Error("Expected border.Top == 'thin', but got ",
border.Top)
}
if border.Bottom != "thin" {
t.Error("Expected border.Bottom == 'thin', but got ",
border.Bottom)
}
}
// Test that we can correctly extract a reference table from the
// sharedStrings.xml file embedded in the XLSX file and return a
// reference table of string values from it.
func TestReadSharedStringsFromZipFile(t *testing.T) {
var xlsxFile *File
var error error
xlsxFile, error = OpenFile("testfile.xlsx")
if error != nil {
t.Error(error.Error())
return
}
if xlsxFile.referenceTable == nil {
t.Error("expected non nil xlsxFile.referenceTable")
return
}
}
func testXf(t *testing.T, result, expected *xlsxXf) {
if result.ApplyAlignment != expected.ApplyAlignment {
t.Error("Expected result.ApplyAlignment == ", expected.ApplyAlignment,
", got", result.ApplyAlignment)
return
}
if result.ApplyBorder != expected.ApplyBorder {
t.Error("Expected result.ApplyBorder == ", expected.ApplyBorder,
", got ", result.ApplyBorder)
return
}
if result.ApplyFont != expected.ApplyFont {
t.Error("Expect result.ApplyFont == ", expected.ApplyFont,
", got ", result.ApplyFont)
return
}
if result.ApplyFill != expected.ApplyFill {
t.Error("Expected result.ApplyFill == ", expected.ApplyFill,
", got ", result.ApplyFill)
return
}
if result.ApplyProtection != expected.ApplyProtection {
t.Error("Expexcted result.ApplyProtection == ", expected.ApplyProtection,
", got ", result.ApplyProtection)
return
}
if result.BorderId != expected.BorderId {
t.Error("Expected BorderId == ", expected.BorderId,
". got ", result.BorderId)
return
}
if result.FillId != expected.FillId {
t.Error("Expected result.FillId == ", expected.FillId,
", got ", result.FillId)
return
}
if result.FontId != expected.FontId {
t.Error("Expected result.FontId == ", expected.FontId,
", got ", result.FontId)
return
}
if result.NumFmtId != expected.NumFmtId {
t.Error("Expected result.NumFmtId == ", expected.NumFmtId,
", got ", result.NumFmtId)
return
}
}
// We can correctly extract a style table from the style.xml file
// embedded in the XLSX file and return a styles struct from it.
func TestReadStylesFromZipFile(t *testing.T) {
var xlsxFile *File
var error error
var fontCount, fillCount, borderCount, cellStyleXfCount, cellXfCount int
var font xlsxFont
var fill xlsxFill
var border xlsxBorder
var xf xlsxXf
xlsxFile, error = OpenFile("testfile.xlsx")
if error != nil {
t.Error(error.Error())
return
}
if xlsxFile.styles == nil {
t.Error("expected non nil xlsxFile.styles")
return
}
fontCount = len(xlsxFile.styles.Fonts)
if fontCount != 4 {
t.Error("expected exactly 4 xslxFonts, got ", fontCount)
return
}
font = xlsxFile.styles.Fonts[0]
if font.Sz.Val != "11" {
t.Error("expected font.Sz.Val == 11, got ", font.Sz.Val)
return
}
if font.Name.Val != "Calibri" {
t.Error("expected font.Name.Val == 'Calibri', got ", font.Name.Val)
return
}
fillCount = len(xlsxFile.styles.Fills)
if fillCount != 3 {
t.Error("Expected exactly 3 xlsxFills, got ", fillCount)
return
}
fill = xlsxFile.styles.Fills[2]
if fill.PatternFill.PatternType != "solid" {
t.Error("Expected PatternFill.PatternType == 'solid', but got ",
fill.PatternFill.PatternType)
return
}
borderCount = len(xlsxFile.styles.Borders)
if borderCount != 2 {
t.Error("Expected exactly 2 xlsxBorders, got ", borderCount)
return
}
border = xlsxFile.styles.Borders[1]
if border.Left.Style != "thin" {
t.Error("Expected border.Left.Style == 'thin', got ", border.Left.Style)
return
}
if border.Right.Style != "thin" {
t.Error("Expected border.Right.Style == 'thin', got ", border.Right.Style)
return
}
if border.Top.Style != "thin" {
t.Error("Expected border.Top.Style == 'thin', got ", border.Top.Style)
return
}
if border.Bottom.Style != "thin" {
t.Error("Expected border.Bottom.Style == 'thin', got ", border.Bottom.Style)
return
}
cellStyleXfCount = len(xlsxFile.styles.CellStyleXfs)
if cellStyleXfCount != 20 {
t.Error("Expected excactly 20 cellStyleXfs, got ", cellStyleXfCount)
return
}
xf = xlsxFile.styles.CellStyleXfs[0]
expectedXf := &xlsxXf{
ApplyAlignment: true,
ApplyBorder: true,
ApplyFont: true,
ApplyFill: false,
ApplyProtection: true,
BorderId: 0,
FillId: 0,
FontId: 0,
NumFmtId: 164}
testXf(t, &xf, expectedXf)
cellXfCount = len(xlsxFile.styles.CellXfs)
if cellXfCount != 3 {
t.Error("Expected excactly 3 cellXfs, got ", cellXfCount)
return
}
xf = xlsxFile.styles.CellXfs[0]
expectedXf = &xlsxXf{
ApplyAlignment: false,
ApplyBorder: false,
ApplyFont: false,
ApplyFill: false,
ApplyProtection: false,
BorderId: 0,
FillId: 0,
FontId: 0,
NumFmtId: 164}
testXf(t, &xf, expectedXf)
}
// We can correctly extract a map of relationship Ids to the worksheet files in
// which they are contained from the XLSX file.
func TestReadWorkbookRelationsFromZipFile(t *testing.T) {
var xlsxFile *File
var error error
xlsxFile, error = OpenFile("testfile.xlsx")
if error != nil {
t.Error(error.Error())
return
}
sheetCount := len(xlsxFile.Sheet)
if sheetCount != 3 {
t.Error("Expected 3 items in xlsxFile.Sheet, but found ", strconv.Itoa(sheetCount))
}
}
// We can extract a map of relationship Ids to the worksheet files in
// which they are contained from the XLSX file, even when the
// worksheet files have arbitrary, non-numeric names.
func TestReadWorkbookRelationsFromZipFileWithFunnyNames(t *testing.T) {
var xlsxFile *File
var error error
xlsxFile, error = OpenFile("testrels.xlsx")
if error != nil {
t.Error(error.Error())
return
}
sheetCount := len(xlsxFile.Sheet)
if sheetCount != 2 {
t.Error("Expected 3 items in xlsxFile.Sheet, but found ", strconv.Itoa(sheetCount))
}
bob := xlsxFile.Sheet["Bob"]
row1 := bob.Rows[0]
cell1 := row1.Cells[0]
if cell1.String() != "I am Bob" {
t.Error("Expected cell1.String() == 'I am Bob', but got '" + cell1.String() + "'")
}
}
func TestLettersToNumeric(t *testing.T) {
cases := map[string]int{"A": 0, "G": 6, "z": 25, "AA": 26, "Az": 51,
"BA": 52, "Bz": 77, "ZA": 26*26 + 0, "ZZ": 26*26 + 25,
"AAA": 26*26 + 26 + 0, "AMI": 1022}
for input, ans := range cases {
output := lettersToNumeric(input)
if output != ans {
t.Error("Expected output '"+input+"' == ", ans,
"but got ", strconv.Itoa(output))
}
}
}
func TestLetterOnlyMapFunction(t *testing.T) {
var input string = "ABC123"
var output string = strings.Map(letterOnlyMapF, input)
if output != "ABC" {
t.Error("Expected output == 'ABC' but got ", output)
}
input = "abc123"
output = strings.Map(letterOnlyMapF, input)
if output != "ABC" {
t.Error("Expected output == 'ABC' but got ", output)
}
}
func TestIntOnlyMapFunction(t *testing.T) {
var input string = "ABC123"
var output string = strings.Map(intOnlyMapF, input)
if output != "123" {
t.Error("Expected output == '123' but got ", output)
}
}
func TestGetCoordsFromCellIDString(t *testing.T) {
var cellIDString string = "A3"
var x, y int
var error error
x, y, error = getCoordsFromCellIDString(cellIDString)
if error != nil {
t.Error(error)
}
if x != 0 {
t.Error("Expected x == 0, but got ", strconv.Itoa(x))
}
if y != 2 {
t.Error("Expected y == 2, but got ", strconv.Itoa(y))
}
}
func TestGetMaxMinFromDimensionRef(t *testing.T) {
var dimensionRef string = "A1:B2"
var minx, miny, maxx, maxy int
var err error
minx, miny, maxx, maxy, err = getMaxMinFromDimensionRef(dimensionRef)
if err != nil {
t.Error(err)
}
if minx != 0 {
t.Error("Expected minx == 0, but got ", strconv.Itoa(minx))
}
if miny != 0 {
t.Error("Expected miny == 0, but got ", strconv.Itoa(miny))
}
if maxx != 1 {
t.Error("Expected maxx == 0, but got ", strconv.Itoa(maxx))
}
if maxy != 1 {
t.Error("Expected maxy == 0, but got ", strconv.Itoa(maxy))
}
}
func TestGetRangeFromString(t *testing.T) {
var rangeString string
var lower, upper int
var error error
rangeString = "1:3"
lower, upper, error = getRangeFromString(rangeString)
if error != nil {
t.Error(error)
}
if lower != 1 {
t.Error("Expected lower bound == 1, but got ", strconv.Itoa(lower))
}
if upper != 3 {
t.Error("Expected upper bound == 3, but got ", strconv.Itoa(upper))
}
}
func TestMakeRowFromSpan(t *testing.T) {
var rangeString string
var row *Row
var length int
rangeString = "1:3"
row = makeRowFromSpan(rangeString)
length = len(row.Cells)
if length != 3 {
t.Error("Expected a row with 3 cells, but got ", strconv.Itoa(length))
}
rangeString = "5:7" // Note - we ignore lower bound!
row = makeRowFromSpan(rangeString)
length = len(row.Cells)
if length != 7 {
t.Error("Expected a row with 7 cells, but got ", strconv.Itoa(length))
}
rangeString = "1:1"
row = makeRowFromSpan(rangeString)
length = len(row.Cells)
if length != 1 {
t.Error("Expected a row with 1 cells, but got ", strconv.Itoa(length))
}
}
func TestReadRowsFromSheet(t *testing.T) {
var sharedstringsXML = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4">
<si>
<t>Foo</t>
</si>
<si>
<t>Bar</t>
</si>
<si>
<t xml:space="preserve">Baz </t>
</si>
<si>
<t>Quuk</t>
</si>
</sst>`)
var sheetxml = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"
xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<dimension ref="A1:B2"/>
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0">
<selection activeCell="C2" sqref="C2"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
<sheetData>
<row r="1" spans="1:2">
<c r="A1" t="s">
<v>0</v>
</c>
<c r="B1" t="s">
<v>1</v>
</c>
</row>
<row r="2" spans="1:2">
<c r="A2" t="s">
<v>2</v>
</c>
<c r="B2" t="s">
<v>3</v>
</c>
</row>
</sheetData>
<pageMargins left="0.7" right="0.7"
top="0.78740157499999996"
bottom="0.78740157499999996"
header="0.3"
footer="0.3"/>
</worksheet>`)
worksheet := new(xlsxWorksheet)
error := xml.NewDecoder(sheetxml).Decode(worksheet)
if error != nil {
t.Error(error.Error())
return
}
sst := new(xlsxSST)
error = xml.NewDecoder(sharedstringsXML).Decode(sst)
if error != nil {
t.Error(error.Error())
return
}
file := new(File)
file.referenceTable = MakeSharedStringRefTable(sst)
rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
if maxRows != 2 {
t.Error("Expected maxRows == 2")
}
if maxCols != 2 {
t.Error("Expected maxCols == 2")
}
row := rows[0]
if len(row.Cells) != 2 {
t.Error("Expected len(row.Cells) == 2, got ", strconv.Itoa(len(row.Cells)))
}
cell1 := row.Cells[0]
if cell1.String() != "Foo" {
t.Error("Expected cell1.String() == 'Foo', got ", cell1.String())
}
cell2 := row.Cells[1]
if cell2.String() != "Bar" {
t.Error("Expected cell2.String() == 'Bar', got ", cell2.String())
}
}
func TestReadRowsFromSheetWithLeadingEmptyRows(t *testing.T) {
var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="2" uniqueCount="2"><si><t>ABC</t></si><si><t>DEF</t></si></sst>`)
var sheetxml = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
<dimension ref="A4:A5"/>
<sheetViews>
<sheetView tabSelected="1" workbookViewId="0">
<selection activeCell="A2" sqref="A2"/>
</sheetView>
</sheetViews>
<sheetFormatPr baseColWidth="10" defaultRowHeight="15" x14ac:dyDescent="0"/>
<sheetData>
<row r="4" spans="1:1">
<c r="A4" t="s">
<v>0</v>
</c>
</row>
<row r="5" spans="1:1">
<c r="A5" t="s">
<v>1</v>
</c>
</row>
</sheetData>
<pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5"/>
<pageSetup paperSize="9" orientation="portrait" horizontalDpi="4294967292" verticalDpi="4294967292"/>
<extLst>
<ext uri="{64002731-A6B0-56B0-2670-7721B7C09600}" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main">
<mx:PLV Mode="0" OnePage="0" WScale="0"/>
</ext>
</extLst>
</worksheet>
`)
worksheet := new(xlsxWorksheet)
error := xml.NewDecoder(sheetxml).Decode(worksheet)
if error != nil {
t.Error(error.Error())
return
}
sst := new(xlsxSST)
error = xml.NewDecoder(sharedstringsXML).Decode(sst)
if error != nil {
t.Error(error.Error())
return
}
file := new(File)
file.referenceTable = MakeSharedStringRefTable(sst)
rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
if maxRows != 2 {
t.Error("Expected maxRows == 2, got ", strconv.Itoa(len(rows)))
}
if maxCols != 1 {
t.Error("Expected maxCols == 1, got ", strconv.Itoa(maxCols))
}
}
func TestReadRowsFromSheetWithEmptyCells(t *testing.T) {
var sharedstringsXML = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="8" uniqueCount="5">
<si>
<t>Bob</t>
</si>
<si>
<t>Alice</t>
</si>
<si>
<t>Sue</t>
</si>
<si>
<t>Yes</t>
</si>
<si>
<t>No</t>
</si>
</sst>
`)
var sheetxml = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:C3"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="D3" sqref="D3"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/>
<sheetData>
<row r="1" spans="1:3">
<c r="A1" t="s">
<v>
0
</v>
</c>
<c r="B1" t="s">
<v>
1
</v>
</c>
<c r="C1" t="s">
<v>
2
</v>
</c>
</row>
<row r="2" spans="1:3">
<c r="A2" t="s">
<v>
3
</v>
</c>
<c r="B2" t="s">
<v>
4
</v>
</c>
<c r="C2" t="s">
<v>
3
</v>
</c>
</row>
<row r="3" spans="1:3">
<c r="A3" t="s">
<v>
4
</v>
</c>
<c r="C3" t="s">
<v>
3
</v>
</c>
</row>
</sheetData>
<pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/>
</worksheet>
`)
worksheet := new(xlsxWorksheet)
error := xml.NewDecoder(sheetxml).Decode(worksheet)
if error != nil {
t.Error(error.Error())
return
}
sst := new(xlsxSST)
error = xml.NewDecoder(sharedstringsXML).Decode(sst)
if error != nil {
t.Error(error.Error())
return
}
file := new(File)
file.referenceTable = MakeSharedStringRefTable(sst)
rows, maxCols, maxRows := readRowsFromSheet(worksheet, file)
if maxRows != 3 {
t.Error("Expected maxRows == 3, got ", strconv.Itoa(len(rows)))
}
if maxCols != 3 {
t.Error("Expected maxCols == 3, got ", strconv.Itoa(maxCols))
}
row := rows[2]
if len(row.Cells) != 3 {
t.Error("Expected len(row.Cells) == 3, got ", strconv.Itoa(len(row.Cells)))
}
cell1 := row.Cells[0]
if cell1.String() != "No" {
t.Error("Expected cell1.String() == 'No', got ", cell1.String())
}
cell2 := row.Cells[1]
if cell2.String() != "" {
t.Error("Expected cell2.String() == '', got ", cell2.String())
}
cell3 := row.Cells[2]
if cell3.String() != "Yes" {
t.Error("Expected cell3.String() == 'Yes', got ", cell3.String())
}
}
func TestReadRowsFromSheetWithTrailingEmptyCells(t *testing.T) {
var row *Row
var cell1, cell2, cell3, cell4 *Cell
var sharedstringsXML = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4"><si><t>A</t></si><si><t>B</t></si><si><t>C</t></si><si><t>D</t></si></sst>`)
var sheetxml = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:D8"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="A7" sqref="A7"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/><sheetData><row r="1" spans="1:4"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="s"><v>2</v></c><c r="D1" t="s"><v>3</v></c></row><row r="2" spans="1:4"><c r="A2"><v>1</v></c></row><row r="3" spans="1:4"><c r="B3"><v>1</v></c></row><row r="4" spans="1:4"><c r="C4"><v>1</v></c></row><row r="5" spans="1:4"><c r="D5"><v>1</v></c></row><row r="6" spans="1:4"><c r="C6"><v>1</v></c></row><row r="7" spans="1:4"><c r="B7"><v>1</v></c></row><row r="8" spans="1:4"><c r="A8"><v>1</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/></worksheet>
`)
worksheet := new(xlsxWorksheet)
error := xml.NewDecoder(sheetxml).Decode(worksheet)
if error != nil {
t.Error(error.Error())
return
}
sst := new(xlsxSST)
error = xml.NewDecoder(sharedstringsXML).Decode(sst)
if error != nil {
t.Error(error.Error())
return
}
file := new(File)
file.referenceTable = MakeSharedStringRefTable(sst)
rows, maxCol, maxRow := readRowsFromSheet(worksheet, file)
if maxCol != 4 {
t.Error("Expected maxCol == 4, got ", strconv.Itoa(maxCol))
}
if maxRow != 8 {
t.Error("Expected maxRow == 8, got ", strconv.Itoa(maxRow))
}
row = rows[0]
if len(row.Cells) != 4 {
t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
}
cell1 = row.Cells[0]
if cell1.String() != "A" {
t.Error("Expected cell1.String() == 'A', got ", cell1.String())
}
cell2 = row.Cells[1]
if cell2.String() != "B" {
t.Error("Expected cell2.String() == 'B', got ", cell2.String())
}
cell3 = row.Cells[2]
if cell3.String() != "C" {
t.Error("Expected cell3.String() == 'C', got ", cell3.String())
}
cell4 = row.Cells[3]
if cell4.String() != "D" {
t.Error("Expected cell4.String() == 'D', got ", cell4.String())
}
row = rows[1]
if len(row.Cells) != 4 {
t.Error("Expected len(row.Cells) == 4, got ", strconv.Itoa(len(row.Cells)))
}
cell1 = row.Cells[0]
if cell1.String() != "1" {
t.Error("Expected cell1.String() == '1', got ", cell1.String())
}
cell2 = row.Cells[1]
if cell2.String() != "" {
t.Error("Expected cell2.String() == '', got ", cell2.String())
}
cell3 = row.Cells[2]
if cell3.String() != "" {
t.Error("Expected cell3.String() == '', got ", cell3.String())
}
cell4 = row.Cells[3]
if cell4.String() != "" {
t.Error("Expected cell4.String() == '', got ", cell4.String())
}
}