Skip to content

Commit

Permalink
This closes #987: support nested calc for if formula (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
bailantaotao authored Aug 12, 2021
1 parent 43a057b commit 61d0ed1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
21 changes: 17 additions & 4 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions calc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 61d0ed1

Please sign in to comment.