@@ -7,122 +7,133 @@ import (
7
7
8
8
func TestOpenFile (t * testing.T ) {
9
9
// Test update a XLSX file.
10
- f1 , err := OpenFile ("./test/Workbook1.xlsx" )
10
+ xlsx , err := OpenFile ("./test/Workbook1.xlsx" )
11
11
if err != nil {
12
12
t .Log (err )
13
13
}
14
14
// Test get all the rows in a not exists sheet.
15
- rows := f1 .GetRows ("Sheet4" )
15
+ rows := xlsx .GetRows ("Sheet4" )
16
16
// Test get all the rows in a sheet.
17
- rows = f1 .GetRows ("Sheet2" )
17
+ rows = xlsx .GetRows ("Sheet2" )
18
18
for _ , row := range rows {
19
19
for _ , cell := range row {
20
20
t .Log (cell , "\t " )
21
21
}
22
22
t .Log ("\r \n " )
23
23
}
24
- f1 .UpdateLinkedValue ()
25
- f1 .SetCellDefault ("SHEET2" , "A1" , strconv .FormatFloat (float64 (100.1588 ), 'f' , - 1 , 32 ))
26
- f1 .SetCellDefault ("SHEET2" , "A1" , strconv .FormatFloat (float64 (- 100.1588 ), 'f' , - 1 , 64 ))
27
- f1 .SetCellInt ("SHEET2" , "A1" , 100 )
28
- f1 .SetCellStr ("SHEET2" , "C11" , "Knowns" )
29
- f1 .NewSheet (3 , ":\\ /?*[]Maximum 31 characters allowed in sheet title." )
24
+ xlsx .UpdateLinkedValue ()
25
+ xlsx .SetCellDefault ("SHEET2" , "A1" , strconv .FormatFloat (float64 (100.1588 ), 'f' , - 1 , 32 ))
26
+ xlsx .SetCellDefault ("SHEET2" , "A1" , strconv .FormatFloat (float64 (- 100.1588 ), 'f' , - 1 , 64 ))
27
+ xlsx .SetCellInt ("SHEET2" , "A1" , 100 )
28
+ xlsx .SetCellStr ("SHEET2" , "C11" , "Knowns" )
29
+ // Test max characters in a cell.
30
+ var s = "c"
31
+ for i := 0 ; i < 32768 ; i ++ {
32
+ s += "c"
33
+ }
34
+ xlsx .SetCellStr ("SHEET2" , "D11" , s )
35
+ xlsx .NewSheet (3 , ":\\ /?*[]Maximum 31 characters allowed in sheet title." )
30
36
// Test set sheet name with illegal name.
31
- f1 .SetSheetName ("Maximum 31 characters allowed i" , "[Rename]:\\ /?* Maximum 31 characters allowed in sheet title." )
32
- f1 .SetCellInt ("Sheet3" , "A23" , 10 )
33
- f1 .SetCellStr ("SHEET3" , "b230" , "10" )
34
- f1 .SetCellStr ("SHEET10" , "b230" , "10" )
35
- f1 .SetActiveSheet (2 )
36
- f1 .GetCellFormula ("Sheet1" , "B19" ) // Test get cell formula with given rows number.
37
- f1 .GetCellFormula ("Sheet2" , "B20" ) // Test get cell formula with illegal sheet index.
38
- f1 .GetCellFormula ("Sheet1" , "B20" ) // Test get cell formula with illegal rows number.
37
+ xlsx .SetSheetName ("Maximum 31 characters allowed i" , "[Rename]:\\ /?* Maximum 31 characters allowed in sheet title." )
38
+ xlsx .SetCellInt ("Sheet3" , "A23" , 10 )
39
+ xlsx .SetCellStr ("SHEET3" , "b230" , "10" )
40
+ xlsx .SetCellStr ("SHEET10" , "b230" , "10" )
41
+ xlsx .SetActiveSheet (2 )
42
+ xlsx .GetCellFormula ("Sheet1" , "B19" ) // Test get cell formula with given rows number.
43
+ xlsx .GetCellFormula ("Sheet2" , "B20" ) // Test get cell formula with illegal sheet index.
44
+ xlsx .GetCellFormula ("Sheet1" , "B20" ) // Test get cell formula with illegal rows number.
39
45
// Test read cell value with given illegal rows number.
40
- f1 .GetCellValue ("Sheet2" , "a-1" )
46
+ xlsx .GetCellValue ("Sheet2" , "a-1" )
41
47
// Test read cell value with given lowercase column number.
42
- f1 .GetCellValue ("Sheet2" , "a5" )
43
- f1 .GetCellValue ("Sheet2" , "C11" )
44
- f1 .GetCellValue ("Sheet2" , "D11" )
45
- f1 .GetCellValue ("Sheet2" , "D12" )
48
+ xlsx .GetCellValue ("Sheet2" , "a5" )
49
+ xlsx .GetCellValue ("Sheet2" , "C11" )
50
+ xlsx .GetCellValue ("Sheet2" , "D11" )
51
+ xlsx .GetCellValue ("Sheet2" , "D12" )
46
52
// Test SetCellValue function.
47
- f1 .SetCellValue ("Sheet2" , "F1" , "Hello" )
48
- f1 .SetCellValue ("Sheet2" , "G1" , []byte ("World" ))
49
- f1 .SetCellValue ("Sheet2" , "F2" , 42 )
50
- f1 .SetCellValue ("Sheet2" , "F2" , int8 (42 ))
51
- f1 .SetCellValue ("Sheet2" , "F2" , int16 (42 ))
52
- f1 .SetCellValue ("Sheet2" , "F2" , int32 (42 ))
53
- f1 .SetCellValue ("Sheet2" , "F2" , int64 (42 ))
54
- f1 .SetCellValue ("Sheet2" , "F2" , float32 (42.65418 ))
55
- f1 .SetCellValue ("Sheet2" , "F2" , float64 (- 42.65418 ))
56
- f1 .SetCellValue ("Sheet2" , "F2" , float32 (42 ))
57
- f1 .SetCellValue ("Sheet2" , "F2" , float64 (42 ))
58
- f1 .SetCellValue ("Sheet2" , "G2" , nil )
53
+ xlsx .SetCellValue ("Sheet2" , "F1" , "Hello" )
54
+ xlsx .SetCellValue ("Sheet2" , "G1" , []byte ("World" ))
55
+ xlsx .SetCellValue ("Sheet2" , "F2" , 42 )
56
+ xlsx .SetCellValue ("Sheet2" , "F2" , int8 (42 ))
57
+ xlsx .SetCellValue ("Sheet2" , "F2" , int16 (42 ))
58
+ xlsx .SetCellValue ("Sheet2" , "F2" , int32 (42 ))
59
+ xlsx .SetCellValue ("Sheet2" , "F2" , int64 (42 ))
60
+ xlsx .SetCellValue ("Sheet2" , "F2" , float32 (42.65418 ))
61
+ xlsx .SetCellValue ("Sheet2" , "F2" , float64 (- 42.65418 ))
62
+ xlsx .SetCellValue ("Sheet2" , "F2" , float32 (42 ))
63
+ xlsx .SetCellValue ("Sheet2" , "F2" , float64 (42 ))
64
+ xlsx .SetCellValue ("Sheet2" , "G2" , nil )
59
65
// Test completion column.
60
- f1 .SetCellValue ("Sheet2" , "M2" , nil )
66
+ xlsx .SetCellValue ("Sheet2" , "M2" , nil )
61
67
// Test read cell value with given axis large than exists row.
62
- f1 .GetCellValue ("Sheet2" , "E231" )
68
+ xlsx .GetCellValue ("Sheet2" , "E231" )
63
69
// Test get active sheet of XLSX and get sheet name of XLSX by given sheet index.
64
- f1 .GetSheetName (f1 .GetActiveSheetIndex ())
70
+ xlsx .GetSheetName (xlsx .GetActiveSheetIndex ())
65
71
// Test get sheet name of XLSX by given invalid sheet index.
66
- f1 .GetSheetName (4 )
72
+ xlsx .GetSheetName (4 )
67
73
// Test get sheet map of XLSX.
68
- f1 .GetSheetMap ()
69
-
74
+ xlsx .GetSheetMap ()
70
75
for i := 1 ; i <= 300 ; i ++ {
71
- f1 .SetCellStr ("SHEET3" , "c" + strconv .Itoa (i ), strconv .Itoa (i ))
76
+ xlsx .SetCellStr ("SHEET3" , "c" + strconv .Itoa (i ), strconv .Itoa (i ))
77
+ }
78
+ err = xlsx .Save ()
79
+ if err != nil {
80
+ t .Log (err )
81
+ }
82
+ // Test write file to not exist directory.
83
+ err = xlsx .WriteTo ("" )
84
+ if err != nil {
85
+ t .Log (err )
72
86
}
73
- err = f1 .Save ()
87
+ }
88
+
89
+ func TestAddPicture (t * testing.T ) {
90
+ xlsx , err := OpenFile ("./test/Workbook1.xlsx" )
74
91
if err != nil {
75
92
t .Log (err )
76
93
}
77
94
// Test add picture to sheet.
78
- err = f1 .AddPicture ("Sheet2" , "I1" , "L10" , "./test/images/excel.jpg" )
95
+ err = xlsx .AddPicture ("Sheet2" , "I1" , "L10" , "./test/images/excel.jpg" )
79
96
if err != nil {
80
97
t .Log (err )
81
98
}
82
- err = f1 .AddPicture ("Sheet1" , "F21" , "G25" , "./test/images/excel.png" )
99
+ err = xlsx .AddPicture ("Sheet1" , "F21" , "G25" , "./test/images/excel.png" )
83
100
if err != nil {
84
101
t .Log (err )
85
102
}
86
- err = f1 .AddPicture ("Sheet2" , "L1" , "O10" , "./test/images/excel.bmp" )
103
+ err = xlsx .AddPicture ("Sheet2" , "L1" , "O10" , "./test/images/excel.bmp" )
87
104
if err != nil {
88
105
t .Log (err )
89
106
}
90
- err = f1 .AddPicture ("Sheet1" , "G21" , "H25" , "./test/images/excel.ico" )
107
+ err = xlsx .AddPicture ("Sheet1" , "G21" , "H25" , "./test/images/excel.ico" )
91
108
if err != nil {
92
109
t .Log (err )
93
110
}
94
111
// Test add picture to sheet with unsupport file type.
95
- err = f1 .AddPicture ("Sheet1" , "G21" , "H25" , "./test/images/excel.icon" )
112
+ err = xlsx .AddPicture ("Sheet1" , "G21" , "H25" , "./test/images/excel.icon" )
96
113
if err != nil {
97
114
t .Log (err )
98
115
}
99
116
// Test add picture to sheet with invalid file path.
100
- err = f1 .AddPicture ("Sheet1" , "G21" , "H25" , "./test/Workbook1.xlsx" )
117
+ err = xlsx .AddPicture ("Sheet1" , "G21" , "H25" , "./test/Workbook1.xlsx" )
101
118
if err != nil {
102
119
t .Log (err )
103
120
}
104
-
105
121
// Test write file to given path.
106
- err = f1 .WriteTo ("./test/Workbook_2.xlsx" )
107
- if err != nil {
108
- t .Log (err )
109
- }
110
- // Test write file to not exist directory.
111
- err = f1 .WriteTo ("" )
122
+ err = xlsx .WriteTo ("./test/Workbook_2.xlsx" )
112
123
if err != nil {
113
124
t .Log (err )
114
125
}
115
126
}
116
127
117
128
func TestBrokenFile (t * testing.T ) {
118
129
// Test write file with broken file struct.
119
- f2 := File {}
120
- err := f2 .Save ()
130
+ xlsx := File {}
131
+ err := xlsx .Save ()
121
132
if err != nil {
122
133
t .Log (err )
123
134
}
124
135
// Test write file with broken file struct with given path.
125
- err = f2 .WriteTo ("./test/Workbook_3.xlsx" )
136
+ err = xlsx .WriteTo ("./test/Workbook_3.xlsx" )
126
137
if err != nil {
127
138
t .Log (err )
128
139
}
@@ -143,35 +154,63 @@ func TestBrokenFile(t *testing.T) {
143
154
144
155
func TestCreateFile (t * testing.T ) {
145
156
// Test create a XLSX file.
146
- f4 := CreateFile ()
147
- f4 .NewSheet (2 , "XLSXSheet2" )
148
- f4 .NewSheet (3 , "XLSXSheet3" )
149
- f4 .SetCellInt ("Sheet2" , "A23" , 56 )
150
- f4 .SetCellStr ("SHEET1" , "B20" , "42" )
151
- f4 .SetActiveSheet (0 )
157
+ xlsx := CreateFile ()
158
+ xlsx .NewSheet (2 , "XLSXSheet2" )
159
+ xlsx .NewSheet (3 , "XLSXSheet3" )
160
+ xlsx .SetCellInt ("Sheet2" , "A23" , 56 )
161
+ xlsx .SetCellStr ("SHEET1" , "B20" , "42" )
162
+ xlsx .SetActiveSheet (0 )
152
163
// Test add picture to sheet.
153
- err := f4 .AddPicture ("Sheet1" , "H2" , "K12" , "./test/images/excel.gif" )
164
+ err := xlsx .AddPicture ("Sheet1" , "H2" , "K12" , "./test/images/excel.gif" )
154
165
if err != nil {
155
166
t .Log (err )
156
167
}
157
- err = f4 .AddPicture ("Sheet1" , "C2" , "F12" , "./test/images/excel.tif" )
168
+ err = xlsx .AddPicture ("Sheet1" , "C2" , "F12" , "./test/images/excel.tif" )
158
169
if err != nil {
159
170
t .Log (err )
160
171
}
161
- err = f4 .WriteTo ("./test/Workbook_3.xlsx" )
172
+ err = xlsx .WriteTo ("./test/Workbook_3.xlsx" )
162
173
if err != nil {
163
174
t .Log (err )
164
175
}
165
176
}
166
177
167
178
func TestSetColWidth (t * testing.T ) {
168
- f5 , err := OpenFile ("./test/Workbook1.xlsx" )
179
+ xlsx , err := OpenFile ("./test/Workbook1.xlsx" )
180
+ if err != nil {
181
+ t .Log (err )
182
+ }
183
+ xlsx .SetColWidth ("sheet1" , "B" , "A" , 12 )
184
+ xlsx .SetColWidth ("sheet1" , "A" , "B" , 12 )
185
+ err = xlsx .Save ()
186
+ if err != nil {
187
+ t .Log (err )
188
+ }
189
+ }
190
+
191
+ func TestSetCellHyperLink (t * testing.T ) {
192
+ xlsx , err := OpenFile ("./test/Workbook1.xlsx" )
193
+ if err != nil {
194
+ t .Log (err )
195
+ }
196
+ // Test set cell hyperlink in a work sheet already have hyperlinks.
197
+ xlsx .SetCellHyperLink ("sheet1" , "B19" , "https://github.com/Luxurioust/excelize" )
198
+ // Test add first hyperlink in a work sheet.
199
+ xlsx .SetCellHyperLink ("sheet2" , "C1" , "https://github.com/Luxurioust/excelize" )
200
+ err = xlsx .Save ()
201
+ if err != nil {
202
+ t .Log (err )
203
+ }
204
+ }
205
+
206
+ func TestSetCellFormula (t * testing.T ) {
207
+ xlsx , err := OpenFile ("./test/Workbook1.xlsx" )
169
208
if err != nil {
170
209
t .Log (err )
171
210
}
172
- f5 . SetColWidth ("sheet1" , "B " , "A" , 12 )
173
- f5 . SetColWidth ("sheet1" , "A " , "B" , 12 )
174
- err = f5 .Save ()
211
+ xlsx . SetCellFormula ("sheet1" , "B19 " , "SUM(Sheet2!D2,Sheet2!D11)" )
212
+ xlsx . SetCellFormula ("sheet1" , "C19 " , "SUM(Sheet2!D2,Sheet2!D9)" )
213
+ err = xlsx .Save ()
175
214
if err != nil {
176
215
t .Log (err )
177
216
}
0 commit comments