-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
cell.go
374 lines (307 loc) · 9.22 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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// Copyright (c) 2017 Andrey Gayvoronsky <[email protected]>
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlsx
import (
"errors"
"fmt"
"github.com/plandem/xlsx/format/styles"
"github.com/plandem/xlsx/internal/ml"
"github.com/plandem/xlsx/internal/number_format"
"github.com/plandem/xlsx/internal/number_format/convert"
"github.com/plandem/xlsx/types"
"github.com/plandem/xlsx/types/hyperlink"
"math"
"strconv"
"time"
)
//Cell is a higher level object that wraps ml.Cell with functionality
type Cell struct {
ml *ml.Cell
sheet *sheetInfo
}
var (
errTypeMismatch = errors.New("type mismatch")
)
//Type returns current type of cell
func (c *Cell) Type() types.CellType {
return c.ml.Type
}
//Value returns current raw value of cell
func (c *Cell) Value() string {
var value string
switch c.ml.Type {
case types.CellTypeInlineString:
if c.ml.InlineStr != nil {
value = fromRichText(c.ml.InlineStr)
}
case types.CellTypeSharedString:
var sid int
if len(c.ml.Value) > 0 {
sid, _ = strconv.Atoi(c.ml.Value)
}
value = fromRichText(c.sheet.workbook.doc.sharedStrings.get(sid))
default:
value = c.ml.Value
}
return value
}
//String returns formatted value as string respecting cell number format and type. Any errors ignored to conform String() interface.
func (c *Cell) String() string {
//if cell has error, then just return value that Excel put here
if c.ml.Type == types.CellTypeError {
return c.ml.Value
}
code := c.sheet.workbook.doc.styleSheet.resolveNumberFormat(c.ml.Style)
//N.B.: Maybe it's not a good idea to use resolved value (e.g. inline string) for conversion?!
return number.Format(c.Value(), code, c.ml.Type)
}
//Date try to convert and return current raw value as time.Time
func (c *Cell) Date() (time.Time, error) {
if c.ml.Type == types.CellTypeDate || c.ml.Type == types.CellTypeNumber || c.ml.Type == types.CellTypeGeneral {
return convert.ToDate(c.ml.Value)
}
return time.Now(), errTypeMismatch
}
//Int try to convert and return current raw value as signed integer
func (c *Cell) Int() (int, error) {
if c.ml.Type == types.CellTypeNumber || c.ml.Type == types.CellTypeGeneral {
return convert.ToInt(c.ml.Value)
}
return 0, errTypeMismatch
}
//Uint try to convert and return current raw value as unsigned integer
func (c *Cell) Uint() (uint, error) {
if c.ml.Type == types.CellTypeNumber || c.ml.Type == types.CellTypeGeneral {
return convert.ToUint(c.ml.Value)
}
return 0, errTypeMismatch
}
//Float try to convert and return current raw value as float64
func (c *Cell) Float() (float64, error) {
if c.ml.Type == types.CellTypeNumber || c.ml.Type == types.CellTypeGeneral {
return convert.ToFloat(c.ml.Value)
}
return math.NaN(), errTypeMismatch
}
//Bool try to convert and return current raw value as bool
func (c *Cell) Bool() (bool, error) {
if c.ml.Type == types.CellTypeBool || c.ml.Type == types.CellTypeGeneral || c.ml.Type == types.CellTypeNumber {
return convert.ToBool(c.ml.Value)
}
return false, errTypeMismatch
}
//setGeneral sets the value as general type
func (c *Cell) setGeneral(value string) {
c.ml.Type = types.CellTypeGeneral
c.ml.Value = value
c.ml.Formula = nil
c.ml.InlineStr = nil
}
//SetText sets shared rich text
func (c *Cell) SetText(parts ...interface{}) error {
//we can update sharedStrings only when sheet is in write mode, to prevent pollution of sharedStrings with fake values
if (c.sheet.mode() & sheetModeWrite) == 0 {
panic(errorNotSupportedWrite)
}
//sharedStrings is the only place that can be mutated from the 'sheet' perspective
text, cellStyles, err := toRichText(parts...)
if err == nil {
sid := c.sheet.workbook.doc.sharedStrings.addText(text)
c.ml.Formula = nil
c.ml.Type = types.CellTypeSharedString
c.ml.Value = strconv.Itoa(sid)
if cellStyles != nil {
c.SetStyles(cellStyles)
}
}
return err
}
//SetInlineText sets inline rich text
func (c *Cell) SetInlineText(parts ...interface{}) error {
text, cellStyles, err := toRichText(parts...)
if err == nil {
c.ml.Type = types.CellTypeInlineString
c.ml.Value = ""
c.ml.Formula = nil
c.ml.InlineStr = text
if cellStyles != nil {
c.SetStyles(cellStyles)
}
}
return err
}
//SetInt sets an signed integer value
func (c *Cell) SetInt(value int) {
c.ml.Type = types.CellTypeNumber
c.ml.Value = strconv.FormatInt(int64(value), 10)
if c.ml.Style == styles.DirectStyleID(0) {
c.ml.Style = c.sheet.workbook.doc.styleSheet.typedStyles[number.Integer]
}
c.ml.Formula = nil
c.ml.InlineStr = nil
}
//SetUint sets an unsigned integer value
func (c *Cell) SetUint(value uint) {
c.ml.Type = types.CellTypeNumber
c.ml.Value = strconv.FormatUint(uint64(value), 10)
if c.ml.Style == styles.DirectStyleID(0) {
c.ml.Style = c.sheet.workbook.doc.styleSheet.typedStyles[number.Integer]
}
c.ml.Formula = nil
c.ml.InlineStr = nil
}
//SetFloat sets a float value
func (c *Cell) SetFloat(value float64) {
c.ml.Type = types.CellTypeNumber
c.ml.Value = strconv.FormatFloat(value, 'f', -1, 64)
if c.ml.Style == styles.DirectStyleID(0) {
c.ml.Style = c.sheet.workbook.doc.styleSheet.typedStyles[number.Float]
}
c.ml.Formula = nil
c.ml.InlineStr = nil
}
//SetBool sets a bool value
func (c *Cell) SetBool(value bool) {
c.ml.Type = types.CellTypeBool
c.ml.Formula = nil
c.ml.InlineStr = nil
if value {
c.ml.Value = "1"
} else {
c.ml.Value = "0"
}
}
//setDate is a general setter for date types
func (c *Cell) setDate(value time.Time, t number.Type) {
c.ml.Type = types.CellTypeDate
c.ml.Value = value.Format(convert.ISO8601)
if c.ml.Style == styles.DirectStyleID(0) {
c.ml.Style = c.sheet.workbook.doc.styleSheet.typedStyles[t]
}
c.ml.Formula = nil
c.ml.InlineStr = nil
}
//SetDateTime sets a time value with number format for datetime
func (c *Cell) SetDateTime(value time.Time) {
c.setDate(value, number.DateTime)
}
//SetDate sets a time value with number format for date
func (c *Cell) SetDate(value time.Time) {
c.setDate(value, number.Date)
}
//SetTime sets a time value with number format for time
func (c *Cell) SetTime(value time.Time) {
c.setDate(value, number.Time)
}
//SetDeltaTime sets a time value with number format for delta time
func (c *Cell) SetDeltaTime(value time.Time) {
c.setDate(value, number.DeltaTime)
}
//nolint
//SetValue sets a value
func (c *Cell) SetValue(value interface{}) {
switch v := value.(type) {
case int:
c.SetInt(v)
case int8:
c.SetInt(int(v))
case int16:
c.SetInt(int(v))
case int32:
c.SetInt(int(v))
case int64:
c.SetInt(int(v))
case uint:
c.SetUint(v)
case uint8:
c.SetUint(uint(v))
case uint16:
c.SetUint(uint(v))
case uint32:
c.SetUint(uint(v))
case uint64:
c.SetUint(uint(v))
case float32:
c.SetFloat(float64(v))
case float64:
c.SetFloat(v)
case string:
c.SetText(v)
case []byte:
c.SetText(string(v))
case bool:
c.SetBool(v)
case time.Time:
c.setDate(v, number.DateTime)
case []interface{}:
_ = c.SetText(v...)
case nil:
c.Reset()
default:
c.setGeneral(fmt.Sprintf("%v", value))
}
}
//Reset resets current current cell information
func (c *Cell) Reset() {
*c.ml = ml.Cell{Ref: c.ml.Ref}
}
//Clear clears cell's value
func (c *Cell) Clear() {
c.ml.Value = ""
}
//HasFormula returns true if cell has formula
func (c *Cell) HasFormula() bool {
return c.ml.Formula != nil && (*c.ml.Formula != ml.CellFormula{})
}
//Styles returns DirectStyleID of active format for cell
func (c *Cell) Styles() styles.DirectStyleID {
return c.ml.Style
}
//SetStyles sets style format to requested DirectStyleID or styles.Info
func (c *Cell) SetStyles(s interface{}) {
c.ml.Style = c.sheet.resolveStyleID(s)
}
//SetValueWithFormat is helper function that internally works as SetValue and SetStyles with NumberFormat
func (c *Cell) SetValueWithFormat(value interface{}, formatCode string) {
c.ml.Style = c.sheet.resolveStyleID(styles.New(styles.NumberFormat(formatCode)))
c.SetValue(value)
}
//Hyperlink returns resolved hyperlink.Info if there is any hyperlink or nil otherwise
func (c *Cell) Hyperlink() *hyperlink.Info {
return c.sheet.hyperlinks.Get(c.ml.Ref)
}
//SetHyperlink sets hyperlink for cell, where link can be string or hyperlink.Info
func (c *Cell) SetHyperlink(link interface{}) error {
format, err := c.sheet.hyperlinks.Add(types.RefFromIndexes(c.ml.Ref.ToIndexes()).ToBounds(), link)
if err != nil {
return err
}
c.SetStyles(format)
return nil
}
//SetValueWithHyperlink is helper function that internally works as SetValue and SetHyperlink
func (c *Cell) SetValueWithHyperlink(value interface{}, link interface{}) error {
err := c.SetHyperlink(link)
if err == nil {
c.SetValue(value)
}
return err
}
//RemoveHyperlink removes hyperlink from cell
func (c *Cell) RemoveHyperlink() {
c.sheet.hyperlinks.Remove(types.RefFromIndexes(c.ml.Ref.ToIndexes()).ToBounds())
}
//SetComment sets comment for cell, where comment can be string or comment.Info
func (c *Cell) SetComment(comment interface{}) error {
return c.sheet.comments.Add(c.ml.Ref, comment)
}
//Comment returns text of comment if there is any comment or empty string
func (c *Cell) Comment() string {
comment := c.sheet.comments.Get(c.ml.Ref)
return fromRichText(comment)
}
//RemoveComment removes comment from cell
func (c *Cell) RemoveComment() {
c.sheet.comments.Remove(c.ml.Ref)
}