From 0e392dfcaadf476830a5d08b820520fd49973de5 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 15 Jul 2020 17:00:21 +0000 Subject: [PATCH] fpspreadsheet: Add exception handling for CF expressions writing to the workbook's error log. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7542 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../demo_conditional_formatting.lpi | 8 +++++++- .../demo_conditional_formatting.pas | 6 +++--- .../fpspreadsheet/source/common/fpsexprparser.pas | 8 +++++--- .../fpspreadsheet/source/common/fpsopendocument.pas | 12 ++++++++++-- components/fpspreadsheet/source/common/xlsxml.pas | 12 ++++++++++-- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.lpi b/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.lpi index ca317beeb..f9f511971 100644 --- a/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.lpi +++ b/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.lpi @@ -49,7 +49,7 @@ - + @@ -59,6 +59,12 @@ + + + + + + diff --git a/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.pas b/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.pas index c30f9f37d..614ffa6b5 100644 --- a/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.pas +++ b/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.pas @@ -48,7 +48,7 @@ begin sh.WriteFormula(i, 18, '=1.0/1.0'); end; lastCol := 18; - + (* // conditional format #1: equal to number constant sh.WriteText(row, 0, 'equal to constant 5'); sh.WriteText(row, 1, 'background yellow'); @@ -264,7 +264,7 @@ begin fmt.SetFont(wb.AddFont('Courier New', 14, [fssBold], scRed)); fmtIdx := wb.AddCellFormat(fmt); sh.WriteConditionalCellFormat(Range(row, 2, row, lastCol), cfcNotContainsErrors, fmtIdx); - +*) // conditional format: expression inc(row); sh.WriteText(row, 0, 'expression: ISNUMBER($E$5)'); @@ -272,7 +272,7 @@ begin InitFormatRecord(fmt); fmt.SetBackgroundColor(scBlue); fmtIdx := wb.AddCellFormat(fmt); - sh.WriteConditionalCellFormat(Range(row, 2, row, 2), cfcExpression, '=ISNUMBER($E$5)', fmtIdx); + sh.WriteConditionalCellFormat(Range(row, 2, row, 2), cfcExpression, '=IS-NUMBER($E$5)', fmtIdx); // Two rules in the same conditional format inc(row); diff --git a/components/fpspreadsheet/source/common/fpsexprparser.pas b/components/fpspreadsheet/source/common/fpsexprparser.pas index ad0de5341..e54d49430 100644 --- a/components/fpspreadsheet/source/common/fpsexprparser.pas +++ b/components/fpspreadsheet/source/common/fpsexprparser.pas @@ -78,6 +78,8 @@ const ]; type + EGeneralExprParserError = Exception; + // Forward declarations TsExpressionParser = class; TsBuiltInExpressionManager = class; @@ -672,7 +674,7 @@ type property SheetnameTerminator: char read FSheetNameTerminator write FSheetNameTerminator; end; - EExprScanner = class(Exception); + EExprScanner = class(EGeneralExprParserError); PFormatSettings = ^TFormatSettings; { TsExpressionParser } @@ -833,8 +835,8 @@ type PsFormula = ^TsFormula; { Exception classes } - EExprParser = class(Exception); - ECalcEngine = class(Exception); + EExprParser = class(EGeneralExprParserError); + ECalcEngine = class(EGeneralExprParserError); function TokenName(AToken: TsTokenType): String; function ResultTypeName(AResult: TsResultType): String; diff --git a/components/fpspreadsheet/source/common/fpsopendocument.pas b/components/fpspreadsheet/source/common/fpsopendocument.pas index f037c3af4..13016c140 100644 --- a/components/fpspreadsheet/source/common/fpsopendocument.pas +++ b/components/fpspreadsheet/source/common/fpsopendocument.pas @@ -436,8 +436,16 @@ begin begin parser := TsSpreadsheetParser.Create(AWorksheet); try - parser.Expression[fdExcelA1] := Result; // Parse in Excel-A1 dialect - Result := parser.Expression[fdOpenDocument]; // Convert to ODS dialect + try + parser.Expression[fdExcelA1] := Result; // Parse in Excel-A1 dialect + Result := parser.Expression[fdOpenDocument]; // Convert to ODS dialect + except + on EGeneralExprParserError do + begin + Result := VarToStr(v); + AWorksheet.Workbook.AddErrorMsg('Error in CF Expression ' + Result); + end; + end; finally parser.Free; end; diff --git a/components/fpspreadsheet/source/common/xlsxml.pas b/components/fpspreadsheet/source/common/xlsxml.pas index 069d28d48..8ddda5efc 100644 --- a/components/fpspreadsheet/source/common/xlsxml.pas +++ b/components/fpspreadsheet/source/common/xlsxml.pas @@ -289,8 +289,16 @@ begin begin parser := TsSpreadsheetParser.Create(AWorksheet); try - parser.Expression[fdExcelA1] := Result; // Parse in Excel-A1 dialect - Result := parser.R1C1Expression[nil]; // Convert to R1C1 dialect + try + parser.Expression[fdExcelA1] := Result; // Parse in Excel-A1 dialect + Result := parser.R1C1Expression[nil]; // Convert to R1C1 dialect + except + on EGeneralExprParserError do + begin + Result := VarToStr(v); + AWorksheet.Workbook.AddErrorMsg('Error in CF Expression ' + Result); + end; + end; // Note: Using nil here to get absolute references. finally parser.Free;