forked from IGLOU-EU/unioffice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fnindex.go
439 lines (402 loc) · 11.2 KB
/
fnindex.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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
// Copyright 2017 Baliance. All rights reserved.
//
// Use of this source code is governed by the terms of the Affero GNU General
// Public License version 3.0 as published by the Free Software Foundation and
// appearing in the file LICENSE included in the packaging of this file. A
// commercial license can be purchased by contacting sales@baliance.com.
package formula
import (
"fmt"
"strings"
"baliance.com/gooxml/spreadsheet/reference"
)
func init() {
RegisterFunction("INDEX", Index)
RegisterFunctionComplex("INDIRECT", Indirect)
RegisterFunctionComplex("OFFSET", Offset)
RegisterFunction("HLOOKUP", HLookup)
RegisterFunction("LOOKUP", Lookup)
RegisterFunction("VLOOKUP", VLookup)
RegisterFunction("TRANSPOSE", Transpose)
}
// Index implements the Excel INDEX function
func Index(args []Result) Result {
if len(args) < 3 {
return MakeErrorResult("INDEX requires three arguments")
}
arr := args[0]
if arr.Type != ResultTypeArray && arr.Type != ResultTypeList {
return MakeErrorResult("INDEX requires first argument of type array")
}
rowArg := args[1].AsNumber()
if rowArg.Type != ResultTypeNumber {
return MakeErrorResult("INDEX requires numeric row argument")
}
colArg := args[2].AsNumber()
if colArg.Type != ResultTypeNumber {
return MakeErrorResult("INDEX requires numeric col argument")
}
row := int(rowArg.ValueNumber) - 1
col := int(colArg.ValueNumber) - 1
var rowVal []Result
if arr.Type == ResultTypeArray {
if row < 0 || row >= len(arr.ValueArray) {
return MakeErrorResult("INDEX has row out of range")
}
rowVal = arr.ValueArray[row]
} else {
if row < 0 || row >= 1 {
return MakeErrorResult("INDEX has row out of range")
}
rowVal = arr.ValueList
}
if col < 0 || col > len(rowVal) {
return MakeErrorResult("INDEX has col out of range")
}
rv := rowVal[col]
// empty cell returns a zero
if rv.Type == ResultTypeEmpty {
return MakeNumberResult(0)
}
return rv
}
// Indirect is an implementation of the Excel INDIRECT function that returns the
// contents of a cell.
func Indirect(ctx Context, ev Evaluator, args []Result) Result {
if len(args) != 1 && len(args) != 2 {
return MakeErrorResult("INDIRECT requires one or two arguments")
}
sarg := args[0].AsString()
if sarg.Type != ResultTypeString {
return MakeErrorResult("INDIRECT requires first argument to be of type string")
}
return ctx.Cell(sarg.ValueString, ev)
}
func Offset(ctx Context, ev Evaluator, args []Result) Result {
if len(args) != 5 {
return MakeErrorResult("OFFSET requires one or two arguments")
}
ref := args[0].Ref
// resolve a named range
for ref.Type == ReferenceTypeNamedRange {
ref = ctx.NamedRange(ref.Value)
}
origin := ""
switch ref.Type {
case ReferenceTypeCell:
origin = ref.Value
case ReferenceTypeRange:
sp := strings.Split(ref.Value, ":")
if len(sp) == 2 {
origin = sp[0]
}
default:
return MakeErrorResult(fmt.Sprintf("Invalid range in OFFSET(): %s", ref.Type))
}
col, rowIdx, err := ParseCellReference(origin)
if err != nil {
return MakeErrorResult(fmt.Sprintf("parse origin error OFFSET(): %s", err))
}
rOff := args[1].AsNumber()
if rOff.Type != ResultTypeNumber {
return MakeErrorResult("OFFSET requires numeric row offset")
}
cOff := args[2].AsNumber()
if cOff.Type != ResultTypeNumber {
return MakeErrorResult("OFFSET requires numeric col offset")
}
height := args[3].AsNumber()
if height.Type != ResultTypeNumber {
return MakeErrorResult("OFFSET requires numeric height")
}
width := args[4].AsNumber()
if width.Type != ResultTypeNumber {
return MakeErrorResult("OFFSET requires numeric width")
}
colIdx := reference.ColumnToIndex(col)
origRow := rowIdx + uint32(rOff.ValueNumber)
origCol := colIdx + uint32(cOff.ValueNumber)
endRow := origRow + uint32(height.ValueNumber) - 1
endCol := origCol + uint32(width.ValueNumber) - 1
beg := fmt.Sprintf("%s%d", reference.IndexToColumn(origCol), origRow)
end := fmt.Sprintf("%s%d", reference.IndexToColumn(endCol), endRow)
return resultFromCellRange(ctx, ev, beg, end)
}
// VLookup implements the VLOOKUP function that returns a matching value from a
// column in an array.
func VLookup(args []Result) Result {
if len(args) < 3 {
return MakeErrorResult("VLOOKUP requires at least three arguments")
}
if len(args) > 4 {
return MakeErrorResult("VLOOKUP requires at most four arguments")
}
lookupValue := args[0]
arr := args[1]
if arr.Type != ResultTypeArray {
return MakeErrorResult("VLOOKUP requires second argument of type array")
}
colArg := args[2].AsNumber()
if colArg.Type != ResultTypeNumber {
return MakeErrorResult("VLOOKUP requires numeric col argument")
}
exactMatch := false
if len(args) == 4 {
em := args[3].AsNumber()
if em.Type != ResultTypeNumber {
return MakeErrorResult("VLOOKUP requires numeric match argument")
}
if em.ValueNumber == 0 {
exactMatch = true
}
}
// 1 indexed in input, so change to zero indexed
col := int(colArg.ValueNumber) - 1
matchIdx := -1
wasExact := false
lfor:
for i, row := range arr.ValueArray {
if len(row) == 0 {
continue
}
rval := row[0]
switch compareResults(rval, lookupValue, false) {
case cmpResultLess:
// less than
matchIdx = i
case cmpResultEqual:
// exact match
matchIdx = i
wasExact = true
break lfor
}
}
if matchIdx == -1 {
return MakeErrorResultType(ErrorTypeNA, "VLOOKUP no result found")
}
row := arr.ValueArray[matchIdx]
if col < 0 || col >= len(row) {
return MakeErrorResult("VLOOKUP has invalid column index")
}
if wasExact || !exactMatch {
return row[col]
}
return MakeErrorResultType(ErrorTypeNA, "VLOOKUP no result found")
}
type cmpResult int8
const (
cmpResultEqual cmpResult = 0
cmpResultLess cmpResult = -1
cmpResultGreater cmpResult = 1
cmpResultInvalid cmpResult = 2
)
func compareResults(lhs, rhs Result, caseSensitive bool) cmpResult {
lhs = lhs.AsNumber()
rhs = rhs.AsNumber()
// differing types
if lhs.Type != rhs.Type {
return cmpResultInvalid
}
// both numbers
if lhs.Type == ResultTypeNumber {
if lhs.ValueNumber == rhs.ValueNumber {
return cmpResultEqual
}
if lhs.ValueNumber < rhs.ValueNumber {
return cmpResultLess
}
return cmpResultGreater
}
// both strings
if lhs.Type == ResultTypeString {
if !caseSensitive {
return cmpResult(strings.Compare(strings.ToLower(lhs.ValueString),
strings.ToLower(rhs.ValueString)))
}
return cmpResult(strings.Compare(lhs.ValueString, rhs.ValueString))
}
// empty cells are equal
if lhs.Type == ResultTypeEmpty {
return cmpResultEqual
}
// compare lists recursively
if lhs.Type == ResultTypeList {
if len(lhs.ValueList) < len(rhs.ValueList) {
return cmpResultLess
}
if len(lhs.ValueList) > len(rhs.ValueList) {
return cmpResultGreater
}
for i := range lhs.ValueList {
cmp := compareResults(lhs.ValueList[i], rhs.ValueList[i], caseSensitive)
if cmp != cmpResultEqual {
return cmp
}
}
return cmpResultEqual
}
// compare arrays recursively
if lhs.Type == ResultTypeList {
if len(lhs.ValueArray) < len(rhs.ValueArray) {
return cmpResultLess
}
if len(lhs.ValueArray) > len(rhs.ValueArray) {
return cmpResultGreater
}
for i := range lhs.ValueArray {
lrow := lhs.ValueArray[i]
rrow := lhs.ValueArray[i]
if len(lrow) < len(rrow) {
return cmpResultLess
}
if len(lrow) > len(rrow) {
return cmpResultGreater
}
for c := range lrow {
cmp := compareResults(lrow[c], rrow[c], caseSensitive)
if cmp != cmpResultEqual {
return cmp
}
}
}
return cmpResultEqual
}
return cmpResultInvalid
}
// Lookup implements the LOOKUP function that returns a matching value from a
// column, or from the same index in a second column.
func Lookup(args []Result) Result {
if len(args) < 2 {
return MakeErrorResult("LOOKUP requires at least two arguments")
}
if len(args) > 3 {
return MakeErrorResult("LOOKUP requires at most three arguments")
}
lookupValue := args[0]
arr := args[1]
if arr.Type != ResultTypeArray && arr.Type != ResultTypeList {
return MakeErrorResult("VLOOKUP requires second argument of type array")
}
col := extractCol(arr)
idx := -1
for i, v := range col {
if compareResults(lookupValue, v, false) == cmpResultEqual {
idx = i
}
}
if idx == -1 {
return MakeErrorResultType(ErrorTypeNA, "LOOKUP no result found")
}
resultCol := col
if len(args) == 3 {
resultCol = extractCol(args[2])
}
if idx < 0 || idx >= len(resultCol) {
return MakeErrorResultType(ErrorTypeNA, "LOOKUP no result found")
}
return resultCol[idx]
}
func extractCol(arr Result) []Result {
col := arr.ValueList
if arr.Type == ResultTypeArray {
col = nil
for _, r := range arr.ValueArray {
if len(r) > 0 {
col = append(col, r[0])
} else {
col = append(col, MakeEmptyResult())
}
}
}
return col
}
// HLookup implements the HLOOKUP function that returns a matching value from a
// row in an array.
func HLookup(args []Result) Result {
if len(args) < 3 {
return MakeErrorResult("HLOOKUP requires at least three arguments")
}
if len(args) > 4 {
return MakeErrorResult("HLOOKUP requires at most four arguments")
}
lookupValue := args[0]
arr := args[1]
if arr.Type != ResultTypeArray {
return MakeErrorResult("HLOOKUP requires second argument of type array")
}
rowArg := args[2].AsNumber()
if rowArg.Type != ResultTypeNumber {
return MakeErrorResult("HLOOKUP requires numeric row argument")
}
exactMatch := false
if len(args) == 4 {
em := args[3].AsNumber()
if em.Type != ResultTypeNumber {
return MakeErrorResult("HLOOKUP requires numeric match argument")
}
if em.ValueNumber == 0 {
exactMatch = true
}
}
matchIdx := -1
wasExact := false
if len(arr.ValueArray) == 0 {
return MakeErrorResult("HLOOKUP requires non-empty array")
}
row := arr.ValueArray[0]
lfor:
for i, val := range row {
switch compareResults(val, lookupValue, false) {
case cmpResultLess:
// less than
matchIdx = i
case cmpResultEqual:
// exact match
matchIdx = i
wasExact = true
break lfor
}
}
if matchIdx == -1 {
return MakeErrorResultType(ErrorTypeNA, "HLOOKUP no result found")
}
// 1 indexed in input, so change to zero indexed
rowIdx := int(rowArg.ValueNumber) - 1
if rowIdx < 0 || rowIdx > len(arr.ValueArray) {
return MakeErrorResult("HLOOKUP had invalid index")
}
row = arr.ValueArray[rowIdx]
if matchIdx < 0 || matchIdx >= len(row) {
return MakeErrorResult("VLOOKUP has invalid column index")
}
if wasExact || !exactMatch {
return row[matchIdx]
}
return MakeErrorResultType(ErrorTypeNA, "VLOOKUP no result found")
}
// Transpose implements the TRANSPOSE function that transposes a cell range.
func Transpose(args []Result) Result {
if len(args) != 1 {
return MakeErrorResult("TRANSPOSE requires a single argument")
}
if args[0].Type != ResultTypeArray && args[0].Type != ResultTypeList {
return MakeErrorResult("TRANSPOSE requires a range argument")
}
arg := args[0]
// list to array of rows with one column
if arg.Type == ResultTypeList {
res := [][]Result{}
for _, v := range arg.ValueList {
res = append(res, []Result{v})
}
return MakeArrayResult(res)
}
// tranpose the array
res := make([][]Result, len(arg.ValueArray[0]))
for _, row := range arg.ValueArray {
for j, v := range row {
res[j] = append(res[j], v)
}
}
return MakeArrayResult(res)
}