This closes #994, fix LOOKUP function for Array form

This commit is contained in:
Stani 2021-08-19 16:12:37 +02:00 committed by GitHub
parent a55f354eb3
commit dca03c6230
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 5 deletions

18
calc.go
View File

@ -7263,7 +7263,11 @@ func (fn *formulaFuncs) LOOKUP(argsList *list.List) formulaArg {
if lookupVector.Type != ArgMatrix && lookupVector.Type != ArgList { if lookupVector.Type != ArgMatrix && lookupVector.Type != ArgList {
return newErrorFormulaArg(formulaErrorVALUE, "LOOKUP requires second argument of table array") return newErrorFormulaArg(formulaErrorVALUE, "LOOKUP requires second argument of table array")
} }
cols, matchIdx := lookupCol(lookupVector), -1 arrayForm := lookupVector.Type == ArgMatrix
if arrayForm && len(lookupVector.Matrix) == 0 {
return newErrorFormulaArg(formulaErrorVALUE, "LOOKUP requires not empty range as second argument")
}
cols, matchIdx := lookupCol(lookupVector, 0), -1
for idx, col := range cols { for idx, col := range cols {
lhs := lookupValue lhs := lookupValue
switch col.Type { switch col.Type {
@ -7280,9 +7284,13 @@ func (fn *formulaFuncs) LOOKUP(argsList *list.List) formulaArg {
break break
} }
} }
column := cols var column []formulaArg
if argsList.Len() == 3 { if argsList.Len() == 3 {
column = lookupCol(argsList.Back().Value.(formulaArg)) column = lookupCol(argsList.Back().Value.(formulaArg), 0)
} else if arrayForm && len(lookupVector.Matrix[0]) > 1 {
column = lookupCol(lookupVector, 1)
} else {
column = cols
} }
if matchIdx < 0 || matchIdx >= len(column) { if matchIdx < 0 || matchIdx >= len(column) {
return newErrorFormulaArg(formulaErrorNA, "LOOKUP no result found") return newErrorFormulaArg(formulaErrorNA, "LOOKUP no result found")
@ -7291,13 +7299,13 @@ func (fn *formulaFuncs) LOOKUP(argsList *list.List) formulaArg {
} }
// lookupCol extract columns for LOOKUP. // lookupCol extract columns for LOOKUP.
func lookupCol(arr formulaArg) []formulaArg { func lookupCol(arr formulaArg, idx int) []formulaArg {
col := arr.List col := arr.List
if arr.Type == ArgMatrix { if arr.Type == ArgMatrix {
col = nil col = nil
for _, r := range arr.Matrix { for _, r := range arr.Matrix {
if len(r) > 0 { if len(r) > 0 {
col = append(col, r[0]) col = append(col, r[idx])
continue continue
} }
col = append(col, newEmptyFormulaArg()) col = append(col, newEmptyFormulaArg())

View File

@ -1131,6 +1131,9 @@ func TestCalcCellValue(t *testing.T) {
// LOOKUP // LOOKUP
"=LOOKUP(F8,F8:F9,F8:F9)": "32080", "=LOOKUP(F8,F8:F9,F8:F9)": "32080",
"=LOOKUP(F8,F8:F9,D8:D9)": "Feb", "=LOOKUP(F8,F8:F9,D8:D9)": "Feb",
"=LOOKUP(E3,E2:E5,F2:F5)": "22100",
"=LOOKUP(E3,E2:F5)": "22100",
"=LOOKUP(1,MUNIT(1))": "1",
"=LOOKUP(1,MUNIT(1),MUNIT(1))": "1", "=LOOKUP(1,MUNIT(1),MUNIT(1))": "1",
// ROW // ROW
"=ROW()": "1", "=ROW()": "1",
@ -2090,6 +2093,7 @@ func TestCalcCellValue(t *testing.T) {
"=LOOKUP()": "LOOKUP requires at least 2 arguments", "=LOOKUP()": "LOOKUP requires at least 2 arguments",
"=LOOKUP(D2,D1,D2)": "LOOKUP requires second argument of table array", "=LOOKUP(D2,D1,D2)": "LOOKUP requires second argument of table array",
"=LOOKUP(D2,D1,D2,FALSE)": "LOOKUP requires at most 3 arguments", "=LOOKUP(D2,D1,D2,FALSE)": "LOOKUP requires at most 3 arguments",
"=LOOKUP(1,MUNIT(0))": "LOOKUP requires not empty range as second argument",
"=LOOKUP(D1,MUNIT(1),MUNIT(1))": "LOOKUP no result found", "=LOOKUP(D1,MUNIT(1),MUNIT(1))": "LOOKUP no result found",
// ROW // ROW
"=ROW(1,2)": "ROW requires at most 1 argument", "=ROW(1,2)": "ROW requires at most 1 argument",