forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
row_test.go
84 lines (76 loc) · 1.88 KB
/
row_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
package xlsx
import (
"testing"
qt "github.com/frankban/quicktest"
)
func TestRow(t *testing.T) {
c := qt.New(t)
// Test we can add a new Cell to a Row
csRunO(c, "TestAddCell", func(c *qt.C, option FileOption) {
var f *File
f = NewFile(option)
sheet, _ := f.AddSheet("MySheet")
row := sheet.AddRow()
cell := row.AddCell()
c.Assert(cell, qt.Not(qt.IsNil))
c.Assert(row.cellCount, qt.Equals, 1)
})
csRunO(c, "TestGetCell", func(c *qt.C, option FileOption) {
var f *File
f = NewFile(option)
sheet, _ := f.AddSheet("MySheet")
row := sheet.AddRow()
cell := row.AddCell()
cell.SetValue("foo")
cell1 := row.AddCell()
cell1.SetValue("bar")
cell2 := row.GetCell(0)
c.Assert(cell.Value, qt.Equals, cell2.Value)
})
csRunO(c, "TestForEachCell", func(c *qt.C, option FileOption) {
var f *File
f, err := OpenFile("./testdocs/empty_cells.xlsx", option)
c.Assert(err, qt.Equals, nil)
sheet := f.Sheets[0]
c.Run("NoOptions", func(c *qt.C) {
output := [][]string{}
err := sheet.ForEachRow(func(r *Row) error {
cells := []string{}
err := r.ForEachCell(func(c *Cell) error {
cells = append(cells, c.Value)
return nil
})
if err != nil {
return err
}
c.Assert(cells, qt.HasLen, 4)
output = append(output, cells)
return nil
})
c.Assert(err, qt.Equals, nil)
})
c.Run("SkipEmptyCells", func(c *qt.C) {
output := [][]string{}
err := sheet.ForEachRow(func(r *Row) error {
cells := []string{}
err := r.ForEachCell(func(c *Cell) error {
cells = append(cells, c.Value)
return nil
}, SkipEmptyCells)
if err != nil {
return err
}
output = append(output, cells)
return nil
})
c.Assert(err, qt.Equals, nil)
c.Assert(output, qt.DeepEquals,
[][]string{
{"B1", "C1", "D1"},
{"A2", "C2", "D2"},
{"A3", "B3", "D3"},
{"A4", "B4", "C4"},
})
})
})
}