forked from qax-os/excelize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
col_test.go
170 lines (132 loc) · 6.16 KB
/
col_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
package excelize
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestColumnVisibility(t *testing.T) {
t.Run("TestBook1", func(t *testing.T) {
f, err := prepareTestBook1()
assert.NoError(t, err)
assert.NoError(t, f.SetColVisible("Sheet1", "F", false))
assert.NoError(t, f.SetColVisible("Sheet1", "F", true))
visible, err := f.GetColVisible("Sheet1", "F")
assert.Equal(t, true, visible)
assert.NoError(t, err)
// Test get column visiable on not exists worksheet.
_, err = f.GetColVisible("SheetN", "F")
assert.EqualError(t, err, "sheet SheetN is not exist")
// Test get column visiable with illegal cell coordinates.
_, err = f.GetColVisible("Sheet1", "*")
assert.EqualError(t, err, `invalid column name "*"`)
assert.EqualError(t, f.SetColVisible("Sheet1", "*", false), `invalid column name "*"`)
f.NewSheet("Sheet3")
assert.NoError(t, f.SetColVisible("Sheet3", "E", false))
assert.EqualError(t, f.SetColVisible("SheetN", "E", false), "sheet SheetN is not exist")
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestColumnVisibility.xlsx")))
})
t.Run("TestBook3", func(t *testing.T) {
f, err := prepareTestBook3()
assert.NoError(t, err)
f.GetColVisible("Sheet1", "B")
})
}
func TestOutlineLevel(t *testing.T) {
f := NewFile()
f.GetColOutlineLevel("Sheet1", "D")
f.NewSheet("Sheet2")
assert.NoError(t, f.SetColOutlineLevel("Sheet1", "D", 4))
f.GetColOutlineLevel("Sheet1", "D")
f.GetColOutlineLevel("Shee2", "A")
assert.NoError(t, f.SetColWidth("Sheet2", "A", "D", 13))
assert.NoError(t, f.SetColOutlineLevel("Sheet2", "B", 2))
assert.NoError(t, f.SetRowOutlineLevel("Sheet1", 2, 7))
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "D", 8), "invalid outline level")
assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 2, 8), "invalid outline level")
// Test set row outline level on not exists worksheet.
assert.EqualError(t, f.SetRowOutlineLevel("SheetN", 1, 4), "sheet SheetN is not exist")
// Test get row outline level on not exists worksheet.
_, err := f.GetRowOutlineLevel("SheetN", 1)
assert.EqualError(t, err, "sheet SheetN is not exist")
// Test set and get column outline level with illegal cell coordinates.
assert.EqualError(t, f.SetColOutlineLevel("Sheet1", "*", 1), `invalid column name "*"`)
_, err = f.GetColOutlineLevel("Sheet1", "*")
assert.EqualError(t, err, `invalid column name "*"`)
// Test set column outline level on not exists worksheet.
assert.EqualError(t, f.SetColOutlineLevel("SheetN", "E", 2), "sheet SheetN is not exist")
assert.EqualError(t, f.SetRowOutlineLevel("Sheet1", 0, 1), "invalid row number 0")
level, err := f.GetRowOutlineLevel("Sheet1", 2)
assert.NoError(t, err)
assert.Equal(t, uint8(7), level)
_, err = f.GetRowOutlineLevel("Sheet1", 0)
assert.EqualError(t, err, `invalid row number 0`)
level, err = f.GetRowOutlineLevel("Sheet1", 10)
assert.NoError(t, err)
assert.Equal(t, uint8(0), level)
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestOutlineLevel.xlsx")))
f, err = OpenFile(filepath.Join("test", "Book1.xlsx"))
assert.NoError(t, err)
f.SetColOutlineLevel("Sheet2", "B", 2)
}
func TestSetColStyle(t *testing.T) {
f := NewFile()
style, err := f.NewStyle(`{"fill":{"type":"pattern","color":["#94d3a2"],"pattern":1}}`)
assert.NoError(t, err)
// Test set column style on not exists worksheet.
assert.EqualError(t, f.SetColStyle("SheetN", "E", style), "sheet SheetN is not exist")
// Test set column style with illegal cell coordinates.
assert.EqualError(t, f.SetColStyle("Sheet1", "*", style), `invalid column name "*"`)
assert.EqualError(t, f.SetColStyle("Sheet1", "A:*", style), `invalid column name "*"`)
assert.NoError(t, f.SetColStyle("Sheet1", "B", style))
// Test set column style with already exists column with style.
assert.NoError(t, f.SetColStyle("Sheet1", "B", style))
assert.NoError(t, f.SetColStyle("Sheet1", "D:C", style))
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestSetColStyle.xlsx")))
}
func TestColWidth(t *testing.T) {
f := NewFile()
f.SetColWidth("Sheet1", "B", "A", 12)
f.SetColWidth("Sheet1", "A", "B", 12)
f.GetColWidth("Sheet1", "A")
f.GetColWidth("Sheet1", "C")
// Test set and get column width with illegal cell coordinates.
_, err := f.GetColWidth("Sheet1", "*")
assert.EqualError(t, err, `invalid column name "*"`)
assert.EqualError(t, f.SetColWidth("Sheet1", "*", "B", 1), `invalid column name "*"`)
assert.EqualError(t, f.SetColWidth("Sheet1", "A", "*", 1), `invalid column name "*"`)
// Test set column width on not exists worksheet.
assert.EqualError(t, f.SetColWidth("SheetN", "B", "A", 12), "sheet SheetN is not exist")
// Test get column width on not exists worksheet.
_, err = f.GetColWidth("SheetN", "A")
assert.EqualError(t, err, "sheet SheetN is not exist")
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestColWidth.xlsx")))
convertRowHeightToPixels(0)
}
func TestInsertCol(t *testing.T) {
f := NewFile()
sheet1 := f.GetSheetName(1)
fillCells(f, sheet1, 10, 10)
f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
f.MergeCell(sheet1, "A1", "C3")
assert.NoError(t, f.AutoFilter(sheet1, "A2", "B2", `{"column":"B","expression":"x != blanks"}`))
assert.NoError(t, f.InsertCol(sheet1, "A"))
// Test insert column with illegal cell coordinates.
assert.EqualError(t, f.InsertCol("Sheet1", "*"), `invalid column name "*"`)
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestInsertCol.xlsx")))
}
func TestRemoveCol(t *testing.T) {
f := NewFile()
sheet1 := f.GetSheetName(1)
fillCells(f, sheet1, 10, 15)
f.SetCellHyperLink(sheet1, "A5", "https://github.com/360EntSecGroup-Skylar/excelize", "External")
f.SetCellHyperLink(sheet1, "C5", "https://github.com", "External")
f.MergeCell(sheet1, "A1", "B1")
f.MergeCell(sheet1, "A2", "B2")
assert.NoError(t, f.RemoveCol(sheet1, "A"))
assert.NoError(t, f.RemoveCol(sheet1, "A"))
// Test remove column with illegal cell coordinates.
assert.EqualError(t, f.RemoveCol("Sheet1", "*"), `invalid column name "*"`)
// Test remove column on not exists worksheet.
assert.EqualError(t, f.RemoveCol("SheetN", "B"), "sheet SheetN is not exist")
assert.NoError(t, f.SaveAs(filepath.Join("test", "TestRemoveCol.xlsx")))
}