From efa8009d96ab19565e710f6d8d45cfb51e258ed4 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 1 Jul 2020 16:08:11 +0000 Subject: [PATCH] fpspreadsheet: Avoid writing duplicate conditional formatting styles to "styles.xml" of ods file. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7511 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../demo_conditional_formatting.pas | 10 +++---- .../source/common/fpsopendocument.pas | 29 ++++++++++++------- 2 files changed, 24 insertions(+), 15 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 9939c5325..6a041e9b8 100644 --- a/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.pas +++ b/components/fpspreadsheet/examples/other/conditional_formatting/demo_conditional_formatting.pas @@ -18,12 +18,12 @@ begin wb := TsWorkbook.Create; try sh := wb.AddWorksheet('test'); - sh.WriteDefaultColWidth(20, suMillimeters); + sh.WriteDefaultColWidth(15, suMillimeters); sh.WriteText(0, 0, 'Condition'); sh.WriteColWidth(0, 60, suMillimeters); sh.WriteText(0, 1, 'Format'); - sh.WriteColWidth(1, 70, suMillimeters); + sh.WriteColWidth(1, 90, suMillimeters); sh.WriteText(0, 2, 'Test values'); row := 2; @@ -49,7 +49,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'); @@ -255,11 +255,11 @@ begin fmt.SetBackgroundColor(scRed); fmtIdx := wb.AddCellFormat(fmt); sh.WriteConditionalCellFormat(Range(row, 2, row, lastCol), cfcContainsErrors, fmtIdx); - *) + // conditional format #6: no errors inc(row); sh.WriteText(row, 0, 'no errors'); - sh.WriteText(row, 1, 'background red'); + sh.WriteText(row, 1, 'background yellow, font "Courier New"/red/bold/14'); fmt.SetBackgroundColor(scYellow); fmt.SetFont(wb.AddFont('Courier New', 14, [fssBold], scRed)); fmtIdx := wb.AddCellFormat(fmt); diff --git a/components/fpspreadsheet/source/common/fpsopendocument.pas b/components/fpspreadsheet/source/common/fpsopendocument.pas index 62917058d..529103d0c 100644 --- a/components/fpspreadsheet/source/common/fpsopendocument.pas +++ b/components/fpspreadsheet/source/common/fpsopendocument.pas @@ -6022,24 +6022,33 @@ var fmtIndex: Integer; cf_rule: TsCFRule; stylename: String; + L: TStrings; begin book := TsWorkbook(FWorkbook); nCF := book.GetNumConditionalFormats; - for i := 0 to nCF-1 do - begin - CF := book.GetConditionalFormat(i); - for j := 0 to CF.RulesCount-1 do + L := TStringList.Create; + try + for i := 0 to nCF-1 do begin - cf_Rule := CF.Rules[j]; - if cf_Rule is TsCFCellRule then + CF := book.GetConditionalFormat(i); + for j := 0 to CF.RulesCount-1 do begin - fmtIndex := TsCFCellRule(cf_Rule).FormatIndex; - fmt := book.GetCellFormat(TsCFCellRule(cf_Rule).FormatIndex); - stylename := Format('conditional_%d', [fmtIndex]); - WriteConditionalStyle(AStream, stylename, fmt); + cf_Rule := CF.Rules[j]; + if cf_Rule is TsCFCellRule then + begin + fmtIndex := TsCFCellRule(cf_Rule).FormatIndex; + fmt := book.GetCellFormat(TsCFCellRule(cf_Rule).FormatIndex); + stylename := Format('conditional_%d', [fmtIndex]); + if L.IndexOf(styleName) = -1 then begin + WriteConditionalStyle(AStream, stylename, fmt); + L.Add(styleName); + end; + end; end; end; + finally + L.Free; end; end;