diff --git a/components/fpspreadsheet/fpsexprparser.pas b/components/fpspreadsheet/fpsexprparser.pas index 48cde3e3a..180b7a1e8 100644 --- a/components/fpspreadsheet/fpsexprparser.pas +++ b/components/fpspreadsheet/fpsexprparser.pas @@ -795,6 +795,7 @@ type end; EExprParser = class(Exception); + ECalcEngine = class(Exception); function TokenName(AToken: TsTokenType): String; function ResultTypeName(AResult: TsResultType): String; @@ -881,7 +882,7 @@ resourcestring ErrInvalidArgumentCount = 'Invalid argument count for function %s'; SErrInvalidResultType = 'Invalid result type: %s'; SErrNotVariable = 'Identifier %s is not a variable'; - SErrCircularReference = 'Circular reference found when calculating worksheet formulas'; + SErrCircularReference = 'Circular reference found when calculating worksheet formula in cell %s'; { --------------------------------------------------------------------- Auxiliary functions @@ -3846,7 +3847,7 @@ begin csNotCalculated: Worksheet.CalcFormula(cell); csCalculating: - raise Exception.Create(SErrCircularReference); + raise ECalcEngine.CreateFmt(SErrCircularReference, [GetCellString(cell^.Row, cell^.Col)]); end; Result.ResultType := rtCell; diff --git a/components/fpspreadsheet/fpspreadsheet.pas b/components/fpspreadsheet/fpspreadsheet.pas index 033fe7d76..a9659c4ae 100755 --- a/components/fpspreadsheet/fpspreadsheet.pas +++ b/components/fpspreadsheet/fpspreadsheet.pas @@ -1386,14 +1386,21 @@ begin begin formula := ACell^.FormulaValue; parser.ActiveCell := nil; - end - else + end else begin formula := ACell^.SharedFormulaBase^.FormulaValue; parser.ActiveCell := ACell; end; - parser.Expression := formula; - res := parser.Evaluate; + try + parser.Expression := formula; + res := parser.Evaluate; + except + on E:ECalcEngine do + begin + Workbook.AddErrorMsg(E.Message); + Res := ErrorResult(errIllegalRef); + end; + end; case res.ResultType of rtEmpty : WriteBlank(ACell); rtError : WriteErrorValue(ACell, res.ResError); @@ -1450,7 +1457,7 @@ begin node := FCells.FindSuccessor(node); end; - // Step 2 - calculate cells. If a not-calculated cell is found it is + // Step 2 - calculate cells. If a not-yet-calculated cell is found it is // calculated and then marked as such. node := FCells.FindLowest; while Assigned(Node) do begin