forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell_test.go
342 lines (270 loc) · 10.2 KB
/
cell_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
package xlsx
import (
. "gopkg.in/check.v1"
)
type StyleSuite struct{}
var _ = Suite(&StyleSuite{})
func (s *StyleSuite) TestNewStyle(c *C) {
style := NewStyle()
c.Assert(style, NotNil)
}
type FontSuite struct{}
var _ = Suite(&FontSuite{})
func (s *FontSuite) TestNewFont(c *C) {
font := NewFont(12, "Verdana")
c.Assert(font, NotNil)
c.Assert(font.Name, Equals, "Verdana")
c.Assert(font.Size, Equals, 12)
}
type CellSuite struct{}
var _ = Suite(&CellSuite{})
// Test that we can set and get a Value from a Cell
func (s *CellSuite) TestValueSet(c *C) {
// Note, this test is fairly pointless, it serves mostly to
// reinforce that this functionality is important, and should
// the mechanics of this all change at some point, to remind
// us not to lose this.
cell := Cell{}
cell.Value = "A string"
c.Assert(cell.Value, Equals, "A string")
}
// Test that GetStyle correctly converts the xlsxStyle.Fonts.
func (s *CellSuite) TestGetStyleWithFonts(c *C) {
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: 0, styles: xStyles}
style = cell.GetStyle()
c.Assert(style, NotNil)
c.Assert(style.Font.Size, Equals, 10)
c.Assert(style.Font.Name, Equals, "Calibra")
}
// Test that SetStyle correctly updates the xlsxStyle.Fonts.
func (s *CellSuite) TestSetStyleWithFonts(c *C) {
file := NewFile()
sheet := file.AddSheet("Test")
row := sheet.AddRow()
cell := row.AddCell()
font := NewFont(12, "Calibra")
style := NewStyle()
style.Font = *font
cell.SetStyle(style)
c.Assert(cell.styleIndex, Equals, 0)
c.Assert(cell.styles.Fonts, HasLen, 1)
xFont := cell.styles.Fonts[0]
c.Assert(xFont.Sz.Val, Equals, "12")
c.Assert(xFont.Name.Val, Equals, "Calibra")
}
// Test that GetStyle correctly converts the xlsxStyle.Fills.
func (s *CellSuite) TestGetStyleWithFills(c *C) {
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: 0, styles: xStyles}
style = cell.GetStyle()
fill := style.Fill
c.Assert(fill.PatternType, Equals, "solid")
c.Assert(fill.BgColor, Equals, "00FF0000")
c.Assert(fill.FgColor, Equals, "FF000000")
}
// Test that SetStyle correctly updates xlsxStyle.Fills.
func (s *CellSuite) TestSetStyleWithFills(c *C) {
file := NewFile()
sheet := file.AddSheet("Test")
row := sheet.AddRow()
cell := row.AddCell()
fill := NewFill("solid", "00FF0000", "FF000000")
style := NewStyle()
style.Fill = *fill
cell.SetStyle(style)
c.Assert(cell.styleIndex, Equals, 0)
c.Assert(cell.styles.Fills, HasLen, 1)
xFill := cell.styles.Fills[0]
xPatternFill := xFill.PatternFill
c.Assert(xPatternFill.PatternType, Equals, "solid")
c.Assert(xPatternFill.FgColor.RGB, Equals, "00FF0000")
c.Assert(xPatternFill.BgColor.RGB, Equals, "FF000000")
}
// Test that GetStyle correctly converts the xlsxStyle.Borders.
func (s *CellSuite) TestGetStyleWithBorders(c *C) {
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: 0, styles: xStyles}
style = cell.GetStyle()
border := style.Border
c.Assert(border.Left, Equals, "thin")
c.Assert(border.Right, Equals, "thin")
c.Assert(border.Top, Equals, "thin")
c.Assert(border.Bottom, Equals, "thin")
}
func (s *CellSuite) TestGetNumberFormat(c *C) {
var cell *Cell
var cellXfs []xlsxXf
var numFmt xlsxNumFmt
var numFmts []xlsxNumFmt
var xStyles *xlsxStyles
var numFmtRefTable map[int]xlsxNumFmt
cellXfs = make([]xlsxXf, 1)
cellXfs[0] = xlsxXf{NumFmtId: 0}
numFmts = make([]xlsxNumFmt, 1)
numFmtRefTable = make(map[int]xlsxNumFmt)
xStyles = &xlsxStyles{NumFmts: numFmts, CellXfs: cellXfs}
cell = &Cell{Value: "123.123", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
numFmt = xlsxNumFmt{NumFmtId: 0, FormatCode: "dd/mm/yy"}
numFmts[0] = numFmt
numFmtRefTable[0] = numFmt
c.Assert(cell.GetNumberFormat(), Equals, "dd/mm/yy")
}
// We can return a string representation of the formatted data
func (l *CellSuite) TestFormattedValue(c *C) {
var cell, earlyCell, negativeCell, smallCell *Cell
var cellXfs []xlsxXf
var numFmt xlsxNumFmt
var numFmts []xlsxNumFmt
var xStyles *xlsxStyles
var numFmtRefTable map[int]xlsxNumFmt
cellXfs = make([]xlsxXf, 1)
cellXfs[0] = xlsxXf{NumFmtId: 1}
numFmts = make([]xlsxNumFmt, 1)
numFmtRefTable = make(map[int]xlsxNumFmt)
xStyles = &xlsxStyles{NumFmts: numFmts, CellXfs: cellXfs}
cell = &Cell{Value: "37947.7500001", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
negativeCell = &Cell{Value: "-37947.7500001", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
smallCell = &Cell{Value: "0.007", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
earlyCell = &Cell{Value: "2.1", numFmtRefTable: numFmtRefTable, styleIndex: 0, styles: xStyles}
setCode := func(code string) {
numFmt = xlsxNumFmt{NumFmtId: 1, FormatCode: code}
numFmts[0] = numFmt
numFmtRefTable[1] = numFmt
}
setCode("general")
c.Assert(cell.FormattedValue(), Equals, "37947.7500001")
c.Assert(negativeCell.FormattedValue(), Equals, "-37947.7500001")
setCode("0")
c.Assert(cell.FormattedValue(), Equals, "37947")
setCode("#,##0") // For the time being we're not doing this
// comma formatting, so it'll fall back to
// the related non-comma form.
c.Assert(cell.FormattedValue(), Equals, "37947")
setCode("0.00")
c.Assert(cell.FormattedValue(), Equals, "37947.75")
setCode("#,##0.00") // For the time being we're not doing this
// comma formatting, so it'll fall back to
// the related non-comma form.
c.Assert(cell.FormattedValue(), Equals, "37947.75")
setCode("#,##0 ;(#,##0)")
c.Assert(cell.FormattedValue(), Equals, "37947")
c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
setCode("#,##0 ;[red](#,##0)")
c.Assert(cell.FormattedValue(), Equals, "37947")
c.Assert(negativeCell.FormattedValue(), Equals, "(37947)")
setCode("0%")
c.Assert(cell.FormattedValue(), Equals, "3794775%")
setCode("0.00%")
c.Assert(cell.FormattedValue(), Equals, "3794775.00%")
setCode("0.00e+00")
c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
setCode("##0.0e+0") // This is wrong, but we'll use it for now.
c.Assert(cell.FormattedValue(), Equals, "3.794775e+04")
setCode("mm-dd-yy")
c.Assert(cell.FormattedValue(), Equals, "11-22-03")
setCode("d-mmm-yy")
c.Assert(cell.FormattedValue(), Equals, "22-Nov-03")
c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-00")
setCode("d-mmm")
c.Assert(cell.FormattedValue(), Equals, "22-Nov")
c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan")
setCode("mmm-yy")
c.Assert(cell.FormattedValue(), Equals, "Nov-03")
setCode("h:mm am/pm")
c.Assert(cell.FormattedValue(), Equals, "6:00 pm")
c.Assert(smallCell.FormattedValue(), Equals, "12:14 am")
setCode("h:mm:ss am/pm")
c.Assert(cell.FormattedValue(), Equals, "6:00:00 pm")
c.Assert(smallCell.FormattedValue(), Equals, "12:14:47 am")
setCode("h:mm")
c.Assert(cell.FormattedValue(), Equals, "18:00")
c.Assert(smallCell.FormattedValue(), Equals, "00:14")
setCode("h:mm:ss")
c.Assert(cell.FormattedValue(), Equals, "18:00:00")
// This is wrong, but there's no eary way aroud it in Go right now, AFAICT.
c.Assert(smallCell.FormattedValue(), Equals, "00:14:47")
setCode("m/d/yy h:mm")
c.Assert(cell.FormattedValue(), Equals, "11/22/03 18:00")
c.Assert(smallCell.FormattedValue(), Equals, "12/30/99 00:14") // Note, that's 1899
c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00 02:24") // and 1900
setCode("mm:ss")
c.Assert(cell.FormattedValue(), Equals, "00:00")
c.Assert(smallCell.FormattedValue(), Equals, "14:47")
setCode("[h]:mm:ss")
c.Assert(cell.FormattedValue(), Equals, "18:00:00")
c.Assert(smallCell.FormattedValue(), Equals, "14:47")
setCode("mmss.0") // I'm not sure about these.
c.Assert(cell.FormattedValue(), Equals, "00.8640")
c.Assert(smallCell.FormattedValue(), Equals, "1447.999997")
setCode("yyyy\\-mm\\-dd")
c.Assert(cell.FormattedValue(), Equals, "2003\\-11\\-22")
setCode("dd/mm/yy")
c.Assert(cell.FormattedValue(), Equals, "22/11/03")
c.Assert(earlyCell.FormattedValue(), Equals, "01/01/00")
setCode("hh:mm:ss")
c.Assert(cell.FormattedValue(), Equals, "18:00:00")
c.Assert(smallCell.FormattedValue(), Equals, "00:14:47")
setCode("dd/mm/yy\\ hh:mm")
c.Assert(cell.FormattedValue(), Equals, "22/11/03\\ 18:00")
setCode("yy-mm-dd")
c.Assert(cell.FormattedValue(), Equals, "03-11-22")
setCode("d-mmm-yyyy")
c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
c.Assert(earlyCell.FormattedValue(), Equals, "1-Jan-1900")
setCode("m/d/yy")
c.Assert(cell.FormattedValue(), Equals, "11/22/03")
c.Assert(earlyCell.FormattedValue(), Equals, "1/1/00")
setCode("m/d/yyyy")
c.Assert(cell.FormattedValue(), Equals, "11/22/2003")
c.Assert(earlyCell.FormattedValue(), Equals, "1/1/1900")
setCode("dd-mmm-yyyy")
c.Assert(cell.FormattedValue(), Equals, "22-Nov-2003")
setCode("dd/mm/yyyy")
c.Assert(cell.FormattedValue(), Equals, "22/11/2003")
setCode("mm/dd/yy hh:mm am/pm")
c.Assert(cell.FormattedValue(), Equals, "11/22/03 06:00 pm")
setCode("mm/dd/yyyy hh:mm:ss")
c.Assert(cell.FormattedValue(), Equals, "11/22/2003 18:00:00")
c.Assert(smallCell.FormattedValue(), Equals, "12/30/1899 00:14:47")
setCode("yyyy-mm-dd hh:mm:ss")
c.Assert(cell.FormattedValue(), Equals, "2003-11-22 18:00:00")
c.Assert(smallCell.FormattedValue(), Equals, "1899-12-30 00:14:47")
}