forked from tealeg/xlsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.go
337 lines (306 loc) · 7.85 KB
/
cell.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
package xlsx
import (
"fmt"
"math"
"strconv"
)
// CellType is an int type for storing metadata about the data type in the cell.
type CellType int
// Known types for cell values.
const (
CellTypeString CellType = iota
CellTypeFormula
CellTypeNumeric
CellTypeBool
CellTypeInline
CellTypeError
)
// Cell is a high level structure intended to provide user access to
// the contents of Cell within an xlsx.Row.
type Cell struct {
Row *Row
Value string
formula string
style *Style
numFmt string
date1904 bool
Hidden bool
HMerge int
VMerge int
Href string
cellType CellType
}
// CellInterface defines the public API of the Cell.
type CellInterface interface {
String() string
FormattedValue() string
}
// NewCell creates a cell and adds it to a row.
func NewCell(r *Row) *Cell {
return &Cell{style: NewStyle(), Row: r}
}
// Merge with other cells, horizontally and/or vertically.
func (c *Cell) Merge(hcells, vcells int) {
c.HMerge = hcells
c.VMerge = vcells
}
// Type returns the CellType of a cell. See CellType constants for more details.
func (c *Cell) Type() CellType {
return c.cellType
}
// SetString sets the value of a cell to a string.
func (c *Cell) SetString(s string) {
c.Value = s
c.formula = ""
c.cellType = CellTypeString
}
// String returns the value of a Cell as a string.
func (c *Cell) String() string {
return c.FormattedValue()
}
// SetFloat sets the value of a cell to a float.
func (c *Cell) SetFloat(n float64) {
c.SetFloatWithFormat(n, "0.00e+00")
}
/*
The following are samples of format samples.
* "0.00e+00"
* "0", "#,##0"
* "0.00", "#,##0.00", "@"
* "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)"
* "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)"
* "0%", "0.00%"
* "0.00e+00", "##0.0e+0"
*/
// SetFloatWithFormat sets the value of a cell to a float and applies
// formatting to the cell.
func (c *Cell) SetFloatWithFormat(n float64, format string) {
c.Value = strconv.FormatFloat(n, 'e', -1, 64)
c.numFmt = format
c.formula = ""
c.cellType = CellTypeNumeric
}
// Float returns the value of cell as a number.
func (c *Cell) Float() (float64, error) {
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return math.NaN(), err
}
return f, nil
}
// SetInt64 sets a cell's value to a 64-bit integer.
func (c *Cell) SetInt64(n int64) {
c.Value = fmt.Sprintf("%d", n)
c.numFmt = "0"
c.formula = ""
c.cellType = CellTypeNumeric
}
// Int64 returns the value of cell as 64-bit integer.
func (c *Cell) Int64() (int64, error) {
f, err := strconv.ParseInt(c.Value, 10, 64)
if err != nil {
return -1, err
}
return f, nil
}
// SetInt sets a cell's value to an integer.
func (c *Cell) SetInt(n int) {
c.Value = fmt.Sprintf("%d", n)
c.numFmt = "0"
c.formula = ""
c.cellType = CellTypeNumeric
}
// Int returns the value of cell as integer.
// Has max 53 bits of precision
// See: float64(int64(math.MaxInt))
func (c *Cell) Int() (int, error) {
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return -1, err
}
return int(f), nil
}
// SetBool sets a cell's value to a boolean.
func (c *Cell) SetBool(b bool) {
if b {
c.Value = "1"
} else {
c.Value = "0"
}
c.cellType = CellTypeBool
}
// Bool returns a boolean from a cell's value.
// TODO: Determine if the current return value is
// appropriate for types other than CellTypeBool.
func (c *Cell) Bool() bool {
// If bool, just return the value.
if c.cellType == CellTypeBool {
return c.Value == "1"
}
// If numeric, base it on a non-zero.
if c.cellType == CellTypeNumeric {
return c.Value != "0"
}
// Return whether there's an empty string.
return c.Value != ""
}
// SetFormula sets the format string for a cell.
func (c *Cell) SetFormula(formula string) {
c.formula = formula
c.cellType = CellTypeFormula
}
// Formula returns the formula string for the cell.
func (c *Cell) Formula() string {
return c.formula
}
// GetStyle returns the Style associated with a Cell
func (c *Cell) GetStyle() *Style {
return c.style
}
// SetStyle sets the style of a cell.
func (c *Cell) SetStyle(style *Style) {
c.style = style
}
// GetNumberFormat returns the number format string for a cell.
func (c *Cell) GetNumberFormat() string {
return c.numFmt
}
func (c *Cell) formatToTime(format string) string {
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return err.Error()
}
return TimeFromExcelTime(f, c.date1904).Format(format)
}
func (c *Cell) formatToFloat(format string) string {
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return err.Error()
}
return fmt.Sprintf(format, f)
}
func (c *Cell) formatToInt(format string) string {
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return err.Error()
}
return fmt.Sprintf(format, int(f))
}
// FormattedValue returns the formatted version of the value.
// If it's a string type, c.Value will just be returned. Otherwise,
// it will attempt to apply Excel formatting to the value.
func (c *Cell) FormattedValue() string {
var numberFormat = c.GetNumberFormat()
switch numberFormat {
case "general", "@":
return c.Value
case "0", "#,##0":
return c.formatToInt("%d")
case "0.00", "#,##0.00":
return c.formatToFloat("%.2f")
case "#,##0 ;(#,##0)", "#,##0 ;[red](#,##0)":
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return err.Error()
}
if f < 0 {
i := int(math.Abs(f))
return fmt.Sprintf("(%d)", i)
}
i := int(f)
return fmt.Sprintf("%d", i)
case "#,##0.00;(#,##0.00)", "#,##0.00;[red](#,##0.00)":
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return err.Error()
}
if f < 0 {
return fmt.Sprintf("(%.2f)", f)
}
return fmt.Sprintf("%.2f", f)
case "0%":
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return err.Error()
}
f = f * 100
return fmt.Sprintf("%d%%", int(f))
case "0.00%":
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return err.Error()
}
f = f * 100
return fmt.Sprintf("%.2f%%", f)
case "0.00e+00", "##0.0e+0":
return c.formatToFloat("%e")
case "mm-dd-yy":
return c.formatToTime("01-02-06")
case "d-mmm-yy":
return c.formatToTime("2-Jan-06")
case "d-mmm":
return c.formatToTime("2-Jan")
case "mmm-yy":
return c.formatToTime("Jan-06")
case "h:mm am/pm":
return c.formatToTime("3:04 pm")
case "h:mm:ss am/pm":
return c.formatToTime("3:04:05 pm")
case "h:mm":
return c.formatToTime("15:04")
case "h:mm:ss":
return c.formatToTime("15:04:05")
case "m/d/yy h:mm":
return c.formatToTime("1/2/06 15:04")
case "mm:ss":
return c.formatToTime("04:05")
case "[h]:mm:ss":
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return err.Error()
}
t := TimeFromExcelTime(f, c.date1904)
if t.Hour() > 0 {
return t.Format("15:04:05")
}
return t.Format("04:05")
case "mmss.0":
f, err := strconv.ParseFloat(c.Value, 64)
if err != nil {
return err.Error()
}
t := TimeFromExcelTime(f, c.date1904)
return fmt.Sprintf("%0d%0d.%d", t.Minute(), t.Second(), t.Nanosecond()/1000)
case "yyyy\\-mm\\-dd", "yyyy\\-mm\\-dd;@":
return c.formatToTime("2006\\-01\\-02")
case "dd/mm/yy":
return c.formatToTime("02/01/06")
case "hh:mm:ss":
return c.formatToTime("15:04:05")
case "dd/mm/yy\\ hh:mm":
return c.formatToTime("02/01/06\\ 15:04")
case "dd/mm/yyyy hh:mm:ss":
return c.formatToTime("02/01/2006 15:04:05")
case "yyyy/mm/dd":
return c.formatToTime("2006/01/02")
case "yy-mm-dd":
return c.formatToTime("06-01-02")
case "d-mmm-yyyy":
return c.formatToTime("2-Jan-2006")
case "m/d/yy":
return c.formatToTime("1/2/06")
case "m/d/yyyy":
return c.formatToTime("1/2/2006")
case "dd-mmm-yyyy":
return c.formatToTime("02-Jan-2006")
case "dd/mm/yyyy":
return c.formatToTime("02/01/2006")
case "mm/dd/yy hh:mm am/pm":
return c.formatToTime("01/02/06 03:04 pm")
case "mm/dd/yyyy hh:mm:ss":
return c.formatToTime("01/02/2006 15:04:05")
case "yyyy-mm-dd hh:mm:ss":
return c.formatToTime("2006-01-02 15:04:05")
}
return c.Value
}