From 9a712310107af7bd44af700d270b080e0114a0dd Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Fri, 26 Jun 2020 11:18:30 +0000 Subject: [PATCH] fpspreadsheet: Add writing of error-related conditional formatting conditions to xlsx writer. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7499 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../demo_conditional_formatting.pas | 15 +++++++++++++++ .../fpspreadsheet/source/common/xlsxooxml.pas | 12 +++++++----- 2 files changed, 22 insertions(+), 5 deletions(-) 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 5af5458dc..067050c53 100644 --- a/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.pas +++ b/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.pas @@ -70,6 +70,21 @@ begin fmtIdx := wb.AddCellFormat(fmt); sh.WriteConditionalCellFormat(Range(0, 4, 5, 4), cfcContainsText, 'bc', fmtIdx); + { ------ 5th conditional format: containsErrors -------------------------- } + sh.WriteFormula(0, 6, '=1.0/0.0'); + sh.WriteFormula(1, 6, '=1.0/1.0'); + sh.WriteFormula(2, 6, '=1.0/2.0'); + + InitFormatRecord(fmt); + fmt.SetBackgroundColor(scGreen); + fmtIdx := wb.AddCellFormat(fmt); + sh.WriteConditionalCellFormat(Range(0, 6, 5, 6), cfcNotContainsErrors, fmtIdx); + + // Condition for ContainsErrors after NoContainsErrors to get higher priority + fmt.SetBackgroundColor(scRed); + fmtIdx := wb.AddCellFormat(fmt); + sh.WriteConditionalCellFormat(Range(0, 0, 100, 100), cfcContainsErrors, fmtIdx); + { ------ Save workbook to file-------------------------------------------- } wb.WriteToFile('test.xlsx', true); finally diff --git a/components/fpspreadsheet/source/common/xlsxooxml.pas b/components/fpspreadsheet/source/common/xlsxooxml.pas index 8d7d00851..c8903d7c3 100644 --- a/components/fpspreadsheet/source/common/xlsxooxml.pas +++ b/components/fpspreadsheet/source/common/xlsxooxml.pas @@ -3347,13 +3347,15 @@ const ('equal', 'notEqual', 'greaterThan', 'lessThan', 'greaterThanOrEqual', 'lessThanOrEqual'); OPERATOR_NAMES_2: array[cfcBetween..cfcNotBetween] of String = ('between', 'notBetween'); - OPERATOR_NAMES_Text: array[cfcBeginsWith..cfcNotContainsText] of String = - ('beginsWith', 'endsWith', 'containsText', 'notContainsText'); - FORMULA: array[cfcBeginsWith..cfcNotContainsText] of String = ( + OPERATOR_NAMES_Text: array[cfcBeginsWith..cfcNotContainsErrors] of String = + ('beginsWith', 'endsWith', 'containsText', 'notContainsText', 'containsErrors', 'notContainsErrors'); + FORMULA: array[cfcBeginsWith..cfcNotContainsErrors] of String = ( 'LEFT(%0:s,LEN("%1:s"))="%1:s"', // cfcBeginsWith 'RIGHT(%0:s,Len("%1:s"))="%1:s"', // cfcEndsWidth 'NOT(ISERROR(SEARCH("%1:s",%0:s)))', // cfcContainsText - 'ISERROR(SEARCH("%1:s",%0:s))' // cfcNotContainsText + 'ISERROR(SEARCH("%1:s",%0:s))', // cfcNotContainsText + 'ISERROR(%0:s)', // cfcContainsErrors + 'NOT(ISERROR(%0:s))' // cfcNotContainsErrors ); var i: Integer; @@ -3406,7 +3408,7 @@ begin [dxfId, APriority, aveStr, stdDevStr, eqAveStr])); end; - cfcBeginsWith..cfcNotContainsText: + cfcBeginsWith..cfcNotContainsErrors: begin firstCellOfRange := GetCellString(ARange.Row1, ARange.Col1); if ARule.Condition = cfcNotContainsText then opStr := ' operator="notContains"' else opStr := '';