From 61d0ed1ff26fbe47b4bfdc6adbc6db09743beb3a Mon Sep 17 00:00:00 2001 From: bailantaotao Date: Thu, 12 Aug 2021 14:53:59 +0800 Subject: [PATCH] This closes #987: support nested calc for if formula (#988) --- calc.go | 21 +++++++++++++++++---- calc_test.go | 2 ++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/calc.go b/calc.go index 18605db..cd7fa97 100644 --- a/calc.go +++ b/calc.go @@ -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 diff --git a/calc_test.go b/calc_test.go index 20505fc..54bdc01 100644 --- a/calc_test.go +++ b/calc_test.go @@ -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",