Skip to content

Commit

Permalink
This update the return value for the CalcCellValue function (qax-os…
Browse files Browse the repository at this point in the history
…#1490)

- Using formula error string in the result of the `CalcCellValue` function
- Using the error message in the `CalcCellValue` function returns error
- Update unit tests
  • Loading branch information
rpoetrap authored Mar 15, 2023
1 parent 8bef61e commit 89b8c87
Show file tree
Hide file tree
Showing 2 changed files with 2,131 additions and 2,129 deletions.
15 changes: 8 additions & 7 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,7 @@ func (f *File) CalcCellValue(sheet, cell string, opts ...Options) (result string
iterations: make(map[string]uint),
iterationsCache: make(map[string]formulaArg),
}, sheet, cell); err != nil {
result = token.String
return
}
if !rawCellValue {
Expand Down Expand Up @@ -1002,8 +1003,8 @@ func (f *File) evalInfixExp(ctx *calcContext, sheet, cell string, tokens []efp.T
inArray = false
continue
}
if err = f.evalInfixExpFunc(ctx, sheet, cell, token, nextToken, opfStack, opdStack, opftStack, opfdStack, argsStack); err != nil {
return newEmptyFormulaArg(), err
if errArg := f.evalInfixExpFunc(ctx, sheet, cell, token, nextToken, opfStack, opdStack, opftStack, opfdStack, argsStack); errArg.Type == ArgError {
return errArg, errors.New(errArg.Error)
}
}
}
Expand All @@ -1021,17 +1022,17 @@ func (f *File) evalInfixExp(ctx *calcContext, sheet, cell string, tokens []efp.T
}

// evalInfixExpFunc evaluate formula function in the infix expression.
func (f *File) evalInfixExpFunc(ctx *calcContext, sheet, cell string, token, nextToken efp.Token, opfStack, opdStack, opftStack, opfdStack, argsStack *Stack) error {
func (f *File) evalInfixExpFunc(ctx *calcContext, sheet, cell string, token, nextToken efp.Token, opfStack, opdStack, opftStack, opfdStack, argsStack *Stack) formulaArg {
if !isFunctionStopToken(token) {
return nil
return newEmptyFormulaArg()
}
prepareEvalInfixExp(opfStack, opftStack, opfdStack, argsStack)
// call formula function to evaluate
arg := callFuncByName(&formulaFuncs{f: f, sheet: sheet, cell: cell, ctx: ctx}, strings.NewReplacer(
"_xlfn.", "", ".", "dot").Replace(opfStack.Peek().(efp.Token).TValue),
[]reflect.Value{reflect.ValueOf(argsStack.Peek().(*list.List))})
if arg.Type == ArgError && opfStack.Len() == 1 {
return errors.New(arg.Value())
return arg
}
argsStack.Pop()
opftStack.Pop() // remove current function separator
Expand All @@ -1050,7 +1051,7 @@ func (f *File) evalInfixExpFunc(ctx *calcContext, sheet, cell string, token, nex
}
opdStack.Push(newStringFormulaArg(val))
}
return nil
return newEmptyFormulaArg()
}

// prepareEvalInfixExp check the token and stack state for formula function
Expand Down Expand Up @@ -11612,7 +11613,7 @@ func (fn *formulaFuncs) IFNA(argsList *list.List) formulaArg {
return newErrorFormulaArg(formulaErrorVALUE, "IFNA requires 2 arguments")
}
arg := argsList.Front().Value.(formulaArg)
if arg.Type == ArgError && arg.Value() == formulaErrorNA {
if arg.Type == ArgError && arg.String == formulaErrorNA {
return argsList.Back().Value.(formulaArg)
}
return arg
Expand Down
Loading

0 comments on commit 89b8c87

Please sign in to comment.