This closes #987: support nested calc for if formula (#988)

This commit is contained in:
bailantaotao 2021-08-12 14:53:59 +08:00 committed by GitHub
parent 43a057b1ea
commit 61d0ed1ff2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

21
calc.go
View File

@ -6746,7 +6746,7 @@ func (fn *formulaFuncs) IF(argsList *list.List) formulaArg {
var (
cond bool
err error
result string
result formulaArg
)
switch token.Type {
case ArgString:
@ -6757,13 +6757,26 @@ func (fn *formulaFuncs) IF(argsList *list.List) formulaArg {
return newBoolFormulaArg(cond)
}
if cond {
return newStringFormulaArg(argsList.Front().Next().Value.(formulaArg).String)
value := argsList.Front().Next().Value.(formulaArg)
switch value.Type {
case ArgNumber:
result = value.ToNumber()
default:
result = newStringFormulaArg(value.String)
}
return result
}
if argsList.Len() == 3 {
result = argsList.Back().Value.(formulaArg).String
value := argsList.Back().Value.(formulaArg)
switch value.Type {
case ArgNumber:
result = value.ToNumber()
default:
result = newStringFormulaArg(value.String)
}
}
}
return newStringFormulaArg(result)
return result
}
// Lookup and Reference Functions

View File

@ -1090,6 +1090,8 @@ func TestCalcCellValue(t *testing.T) {
`=IF(1<>1, "equal", "notequal")`: "notequal",
`=IF("A"="A", "equal", "notequal")`: "equal",
`=IF("A"<>"A", "equal", "notequal")`: "notequal",
`=IF(FALSE,0,ROUND(4/2,0))`: "2",
`=IF(TRUE,ROUND(4/2,0),0)`: "2",
// Excel Lookup and Reference Functions
// CHOOSE
"=CHOOSE(4,\"red\",\"blue\",\"green\",\"brown\")": "brown",