-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fninformation.go
354 lines (320 loc) · 11.9 KB
/
fninformation.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
package formula
import (
"math/big"
"regexp"
"strconv"
"strings"
"time"
"go.devnw.com/ooxml/spreadsheet/reference"
)
func init() {
initRegexpInformation()
RegisterFunction("NA", NA)
RegisterFunction("ISBLANK", IsBlank)
RegisterFunction("ISERR", IsErr)
RegisterFunction("ISERROR", IsError)
RegisterFunction("ISEVEN", IsEven)
RegisterFunctionComplex("_xlfn.ISFORMULA", IsFormula)
RegisterFunctionComplex("ORG.OPENOFFICE.ISLEAPYEAR", IsLeapYear)
RegisterFunctionComplex("ISLOGICAL", IsLogical)
RegisterFunction("ISNA", IsNA)
RegisterFunction("ISNONTEXT", IsNonText)
RegisterFunction("ISNUMBER", IsNumber)
RegisterFunction("ISODD", IsOdd)
RegisterFunctionComplex("ISREF", IsRef)
RegisterFunction("ISTEXT", IsText)
RegisterFunctionComplex("CELL", Cell)
}
var bs string = string([]byte{92})
var integer, finance, intSep, intPar, decimal, decJust, decPar, par, percent, intCur, cur, curLabel, mdy, dmy, sci *regexp.Regexp
func initRegexpInformation() {
integer = regexp.MustCompile(`^0+$`) // 12345
intSep = regexp.MustCompile("^((#|0)+,)+(#|0)+(;|$)") // 123,456,789
intPar = regexp.MustCompile("^(#|0|,)*_\\);") // (123,456,789)
finance = regexp.MustCompile("^0+\\.(0+)$") // 1.23
decimal = regexp.MustCompile("^((#|0)+,)+(#|0)+\\.(0+).*(;|$)") // 1.234
decJust = regexp.MustCompile("^(_|-| )+\\* #+,#+0\\.(0+).*;") // 1.234 with justified horizontal alignment
decPar = regexp.MustCompile("^((#|0)+,)+(#|0)+\\.((#|0)+)_\\).*;") // (1.234)
percent = regexp.MustCompile("^(#|0)+\\.((#|0)+)%$") // 12.34%
intCur = regexp.MustCompile("\\[\\$\\$-.+\\](\\* )?(#|0)+,(#|0)+;") // $1,234
cur = regexp.MustCompile("\\[\\$\\$-.+\\](\\* )?(#|0)+,(#|0)+\\.((#|0|-)+).*;") // $1,234.56
curLabel = regexp.MustCompile("^((#|0)+,)+(#|0)+(\\.((#|0|-)+))?.+\\[\\$.+\\].*;") // 1,234.56 USD
mdy = regexp.MustCompile("^M+(/| |,|\"|" + bs + bs + ")+D+(/| |,|\"|" + bs + bs + ")+Y+$") // 01/21/2019
dmy = regexp.MustCompile("^D+(/| |\\.|\"|" + bs + bs + ")+M+(/| |\\.|\"|" + bs + bs + ")+Y+$") // 21. Jan. 2019
sci = regexp.MustCompile("^(#|0)+\\.((#|0)*)E\\+(#|0)+(;|$)") // 1.02E+002
par = regexp.MustCompile("^.*_\\).*;") // (anything in parentheses)
}
// NA is an implementation of the Excel NA() function that just returns the #N/A! error.
func NA(args []Result) Result {
if len(args) != 0 {
MakeErrorResult("NA() accepts no arguments")
}
return MakeErrorResultType(ErrorTypeNA, "")
}
// Cell is an implementation of the Excel CELL function that returns information
// about the formatting, location, or contents of a cell.
func Cell(ctx Context, ev Evaluator, args []Result) Result {
if len(args) != 1 && len(args) != 2 {
return MakeErrorResult("CELL requires one or two arguments")
}
typ := args[0].AsString()
if typ.Type != ResultTypeString {
return MakeErrorResult("CELL requires first argument to be of type string")
}
ref := args[1].Ref
if ref.Type != ReferenceTypeCell {
return MakeErrorResult("CELL requires second argument to be of type reference")
}
refStr := ref.Value
switch typ.ValueString {
case "address":
cr, err := reference.ParseCellReference(refStr)
if err != nil {
return MakeErrorResult("Incorrect reference: " + refStr)
}
address := "$" + cr.Column + "$" + strconv.Itoa(int(cr.RowIdx))
if cr.SheetName != "" {
address = cr.SheetName + "!" + address
}
return MakeStringResult(address)
case "col":
cr, err := reference.ParseCellReference(refStr)
if err != nil {
return MakeErrorResult("Incorrect reference: " + refStr)
}
return MakeNumberResult(float64(cr.ColumnIdx + 1))
case "color":
red := strings.Contains(ctx.GetFormat(refStr), "[RED]")
return MakeBoolResult(red)
case "contents":
return args[1]
case "filename":
return MakeStringResult(ctx.GetFilename())
case "format":
result := "G"
format := ctx.GetFormat(refStr)
if format == "General" || integer.MatchString(format) {
result = "F0"
} else if format == "0%" {
result = "P0"
} else if format == "MMM DD" {
result = "D2"
} else if format == "MM/YY" {
result = "D3"
} else if format == "MM/DD/YY\\ HH:MM\\ AM/PM" || format == "MM/DD/YYYY\\ HH:MM:SS" {
result = "D4"
} else if format == "MM\\-DD" {
result = "D5"
} else if format == "HH:MM:SS\\ AM/PM" {
result = "D6"
} else if format == "HH:MM\\ AM/PM" {
result = "D7"
} else if format == "HH:MM:SS" {
result = "D8"
} else if format == "HH:MM" {
result = "D9"
} else if intSep.MatchString(format) {
result = ".0"
} else if intPar.MatchString(format) {
result = ".0()"
} else if intCur.MatchString(format) {
result = "C0"
} else if mdy.MatchString(format) || dmy.MatchString(format) {
result = "D1"
} else if submatch := finance.FindStringSubmatch(format); len(submatch) > 1 {
result = "F" + strconv.Itoa(len(submatch[1]))
} else if submatch := decJust.FindStringSubmatch(format); len(submatch) > 1 {
result = "." + strconv.Itoa(len(submatch[2]))
} else if submatch := percent.FindStringSubmatch(format); len(submatch) > 1 {
result = "P" + strconv.Itoa(len(submatch[2]))
} else if submatch := cur.FindStringSubmatch(format); len(submatch) > 1 {
result = "C" + itemFromEndLength(submatch, 1)
} else if submatch := curLabel.FindStringSubmatch(format); len(submatch) > 1 {
result = "C" + itemFromEndLength(submatch, 1)
} else if submatch := decPar.FindStringSubmatch(format); len(submatch) > 1 {
result = "." + itemFromEndLength(submatch, 1) + "()"
} else if submatch := decimal.FindStringSubmatch(format); len(submatch) > 1 {
result = "." + itemFromEndLength(submatch, 1)
} else if submatch := sci.FindStringSubmatch(format); len(submatch) > 1 {
result = "S" + itemFromEndLength(submatch, 3)
}
if result != "G" && strings.Contains(format, "[RED]") {
result += "-"
}
return MakeStringResult(result)
case "parentheses":
format := ctx.GetFormat(refStr)
if par.MatchString(format) {
return MakeNumberResult(1)
} else {
return MakeNumberResult(0)
}
case "prefix":
return MakeStringResult(ctx.GetLabelPrefix(refStr))
case "protect":
result := 0.0
if ctx.GetLocked(refStr) {
result = 1.0
}
return MakeNumberResult(result)
case "row":
cr, err := reference.ParseCellReference(refStr)
if err != nil {
return MakeErrorResult("Incorrect reference: " + refStr)
}
return MakeNumberResult(float64(cr.RowIdx))
case "type":
switch args[1].Type {
case ResultTypeEmpty:
return MakeStringResult("b")
case ResultTypeString:
return MakeStringResult("l")
default:
return MakeStringResult("v")
}
case "width":
cr, err := reference.ParseCellReference(refStr)
if err != nil {
return MakeErrorResult("Incorrect reference: " + refStr)
}
if cr.SheetName == "" {
return MakeNumberResult(ctx.GetWidth(int(cr.ColumnIdx)))
} else {
return MakeNumberResult(ctx.Sheet(cr.SheetName).GetWidth(int(cr.ColumnIdx)))
}
}
return MakeErrorResult("Incorrect first argument of CELL: " + typ.ValueString)
}
func itemFromEndLength(submatch []string, additionalShift int) string {
return strconv.Itoa(len(submatch[len(submatch)-1-additionalShift]))
}
// ISBLANK is an implementation of the Excel ISBLANK() function.
func IsBlank(args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISBLANK() accepts a single argument")
}
return MakeBoolResult(args[0].Type == ResultTypeEmpty)
}
// ISERR is an implementation of the Excel ISERR() function.
func IsErr(args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISERR() accepts a single argument")
}
return MakeBoolResult(args[0].Type == ResultTypeError && args[0].ValueString != "#N/A")
}
// ISERROR is an implementation of the Excel ISERROR() function.
func IsError(args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISERROR() accepts a single argument")
}
return MakeBoolResult(args[0].Type == ResultTypeError)
}
// ISEVEN is an implementation of the Excel ISEVEN() function.
func IsEven(args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISEVEN() accepts a single argument")
}
if args[0].Type != ResultTypeNumber {
return MakeErrorResult("ISEVEN accepts a numeric argument")
}
value := int(args[0].ValueNumber)
return MakeBoolResult(value == value/2*2)
}
// ISFORMULA is an implementation of the Excel ISFORMULA() function.
func IsFormula(ctx Context, ev Evaluator, args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISFORMULA() accepts a single argument")
}
ref := args[0].Ref
if ref.Type != ReferenceTypeCell {
return MakeErrorResult("ISFORMULA requires the first argument to be of type reference")
}
return MakeBoolResult(ctx.HasFormula(ref.Value))
}
// IsLeapYear is an implementation of the Excel ISLEAPYEAR() function.
func IsLeapYear(ctx Context, ev Evaluator, args []Result) Result {
if len(args) != 1 || args[0].Type != ResultTypeNumber {
return MakeErrorResult("ISLEAPYEAR requires a single number argument")
}
epoch := ctx.GetEpoch()
t, err := getValueAsTime(args[0].Value(), epoch)
if err != nil {
return MakeErrorResult("ISLEAPYEAR requires a single number argument")
}
year := t.Year()
return MakeBoolResult(isLeapYear(year))
}
func getValueAsTime(value string, epoch time.Time) (time.Time, error) {
f, _, err := big.ParseFloat(value, 10, 128, big.ToNearestEven)
if err != nil {
return time.Time{}, err
}
day := new(big.Float)
day.SetUint64(uint64(24 * time.Hour))
f.Mul(f, day)
ns, _ := f.Uint64()
t := epoch.Add(time.Duration(ns))
return asLocal(t), nil
}
func asLocal(d time.Time) time.Time {
d = d.UTC()
return time.Date(d.Year(), d.Month(), d.Day(), d.Hour(),
d.Minute(), d.Second(), d.Nanosecond(), time.Local)
}
// IsLogical is an implementation of the Excel ISLOGICAL() function.
func IsLogical(ctx Context, ev Evaluator, args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("ISLOGICAL requires a single number argument")
}
ref := args[0].Ref
if ref.Type != ReferenceTypeCell {
return MakeErrorResult("ISLOGICAL requires the first argument to be of type reference")
}
return MakeBoolResult(ctx.Cell(ref.Value, ev).IsBoolean)
}
// IsNA is an implementation of the Excel ISNA() function.
func IsNA(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("ISNA requires one argument")
}
return MakeBoolResult(args[0].Type == ResultTypeError && args[0].ValueString == "#N/A")
}
// ISNONTEXT is an implementation of the Excel ISNONTEXT() function.
func IsNonText(args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISNONTEXT() accepts a single argument")
}
return MakeBoolResult(args[0].Type != ResultTypeString)
}
// ISNUMBER is an implementation of the Excel ISNUMBER() function.
func IsNumber(args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISNUMBER() accepts a single argument")
}
return MakeBoolResult(args[0].Type == ResultTypeNumber)
}
// ISODD is an implementation of the Excel ISODD() function.
func IsOdd(args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISODD() accepts a single argument")
}
if args[0].Type != ResultTypeNumber {
return MakeErrorResult("ISODD accepts a numeric argument")
}
value := int(args[0].ValueNumber)
return MakeBoolResult(value != value/2*2)
}
// ISREF is an implementation of the Excel ISREF() function.
func IsRef(ctx Context, ev Evaluator, args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISREF() accepts a single argument")
}
return MakeBoolResult(ev.LastEvalIsRef())
}
// ISTEXT is an implementation of the Excel ISTEXT() function.
func IsText(args []Result) Result {
if len(args) != 1 {
MakeErrorResult("ISTEXT() accepts a single argument")
}
return MakeBoolResult(args[0].Type == ResultTypeString)
}