From 006ca0de49f8220a68073943f569bb3ef2116680 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Thu, 2 Jul 2020 09:56:11 +0000 Subject: [PATCH] fpspreadsheet: Complete writing of color range conditional formatting to ODS. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7518 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../source/common/fpsconditionalformat.pas | 81 ++++++++++++++++--- .../source/common/fpsopendocument.pas | 24 ++++-- .../source/common/fpspreadsheet.pas | 7 +- .../source/common/fpspreadsheet_cf.inc | 15 +++- 4 files changed, 108 insertions(+), 19 deletions(-) diff --git a/components/fpspreadsheet/source/common/fpsconditionalformat.pas b/components/fpspreadsheet/source/common/fpsconditionalformat.pas index 207472d6a..191d93c00 100644 --- a/components/fpspreadsheet/source/common/fpsconditionalformat.pas +++ b/components/fpspreadsheet/source/common/fpsconditionalformat.pas @@ -36,19 +36,23 @@ type end; { Color range } - TsCFColorRangeValue = (crvMin, crvMax, crvPercentile); + TsCFColorRangeValueKind = (crvkMin, crvkMax, crvkPercent, crkValue); TsCFColorRangeRule = class(TsCFRule) - StartValue: TsCFColorRangeValue; - CenterValue: TsCFColorRangeValue; - EndValue: TsCFColorRangeValue; - StartValueParam: Double; - CenterValueParam: Double; - EndValueParam: Double; + StartValueKind: TsCFColorRangeValueKind; + CenterValueKind: TsCFColorRangeValueKind; + EndValueKind: TsCFColorRangeValueKind; + StartValue: Double; + CenterValue: Double; + EndValue: Double; StartColor: TsColor; CenterColor: TsColor; EndColor: TsColor; + constructor Create; procedure Assign(ASource: TsCFRule); override; + procedure SetupEnd(AColor: TsColor; AKind: TsCFColorRangeValueKind; AValue: Double); + procedure SetupCenter(AColor: TsColor; AKind: TsCFColorRangeValueKind; AValue: Double); + procedure SetupStart(AColor: TsColor; AKind: TsCFColorRangeValueKind; AValue: Double); end; { DataBars } @@ -97,7 +101,11 @@ type function AddCellRule(ASheet: TsBasicWorksheet; ARange: TsCellRange; ACondition: TsCFCondition; AParam1, AParam2: Variant; ACellFormatIndex: Integer): Integer; overload; function AddColorRangeRule(ASheet: TsBasicWorksheet; ARange: TsCellRange; - AStartColor, ACenterColor, AEndColor: TsColor): Integer; + AStartColor, ACenterColor, AEndColor: TsColor): Integer; overload; + function AddColorRangeRule(ASheet: TsBasicWorksheet; ARange: TsCellRange; + AStartColor: TsColor; AStartKind: TsCFColorRangeValueKind; AStartValue: Double; + ACenterColor: TsColor; ACenterKind: TsCFColorRangeValueKind; ACenterValue: Double; + AEndColor: TsColor; AEndKind: TsCFColorRangeValueKind; AEndValue: Double): Integer; overload; function AddDataBarRule(ASheet: TsBasicWorksheet; ARange: TsCellRange): Integer; procedure Delete(AIndex: Integer); function Find(ASheet: TsBasicWorksheet; ARange: TsCellRange): Integer; @@ -130,16 +138,27 @@ begin raise Exception.Create('Source cannot be assigned to TCVDataBarRule'); end; +constructor TsCFColorRangeRule.Create; +begin + inherited; + SetupStart(scRed, crvkMin, 0.0); + SetupCenter(scYellow, crvkPercent, 50.0); + SetupEnd(scBlue, crvkMax, 0.0); + EndValueKind := crvkMax; + EndValue := 0; + EndColor := scBlue; +end; + procedure TsCFColorRangeRule.Assign(ASource: TsCFRule); begin if ASource is TsCFColorRangeRule then begin + StartValueKind := TsCFColorRangeRule(ASource).StartValueKind; + CenterValueKind := TsCFColorRangeRule(ASource).CenterValueKind; + EndValueKind := TsCFColorRangeRule(ASource).EndValueKind; StartValue := TsCFColorRangeRule(ASource).StartValue; CenterValue := TsCFColorRangeRule(ASource).CenterValue; EndValue := TsCFColorRangeRule(ASource).EndValue; - StartValueParam := TsCFColorRangeRule(ASource).StartValueParam; - CenterValueParam := TsCFColorRangeRule(ASource).CenterValueParam; - EndValueParam := TsCFColorRangeRule(ASource).EndValueParam; StartColor := TsCFColorRangeRule(ASource).StartColor; CenterColor := TsCFColorRangeRule(ASource).CenterColor; EndColor := TsCFColorRangeRule(ASource).EndColor; @@ -147,6 +166,31 @@ begin raise Exception.Create('Source cannot be assigned to TCVDataBarRule'); end; +procedure TsCFColorRangeRule.SetupCenter(AColor: TsColor; + AKind: TsCFColorrangeValueKind; AValue: Double); +begin + CenterValueKind := AKind; + CenterValue := AValue; + CenterColor := AColor; +end; + +procedure TsCFColorRangeRule.SetupEnd(AColor: TsColor; + AKind: TsCFColorRangeValueKind; AValue: Double); +begin + EndValueKind := AKind; + EndValue := AValue; + EndColor := AColor; +end; + +procedure TsCFColorRangeRule.SetupStart(AColor: TsColor; + AKind: TsCFColorrangeValueKind; AValue: Double); +begin + StartValueKind := AKind; + StartValue := AValue; + StartColor := AColor; +end; + + { TCFRule } @@ -278,6 +322,21 @@ begin Result := AddRule(ASheet, ARange, rule); end; +function TsConditionalFormatList.AddColorRangeRule(ASheet: TsBasicWorksheet; + ARange: TsCellRange; + AStartColor: TsColor; AStartKind: TsCFColorRangeValueKind; AStartValue: Double; + ACenterColor: TsColor; ACenterKind: TsCFColorRangeValueKind; ACenterValue: Double; + AEndColor: TsColor; AEndKind: TsCFColorRangeValueKind; AEndValue: Double): Integer; +var + rule: TsCFColorRangeRule; +begin + rule := TsCFColorRangeRule.Create; + rule.SetupStart(AStartColor, AStartKind, AStartValue); + rule.SetupCenter(ACenterColor, ACenterKind, ACenterValue); + rule.SetupEnd(AEndColor, AEndKind, AEndValue); + Result := AddRule(ASheet, ARange, rule); +end; + function TsConditionalFormatlist.AddDataBarRule(ASheet: TsBasicWorksheet; ARange: TsCellRange): Integer; var diff --git a/components/fpspreadsheet/source/common/fpsopendocument.pas b/components/fpspreadsheet/source/common/fpsopendocument.pas index ce5e5a282..95fe896d6 100644 --- a/components/fpspreadsheet/source/common/fpsopendocument.pas +++ b/components/fpspreadsheet/source/common/fpsopendocument.pas @@ -410,6 +410,12 @@ const 'is-error', 'is-no-error' // cfcContainsErrors, cfcNotContainsErrors ); + CF_COLORRANGE_VALUE_KIND: array[TsCFColorRangeValueKind] of string = ( + 'minimum', // crvkMin + 'maximum', // crvkMax + 'percentile', // crvkPercent + 'number' //crkValue + ); function CFOperandToStr(v: variant; AWorksheet: TsWorksheet): String; var @@ -5957,13 +5963,19 @@ begin cf_ColorRangeRule := TsCFColorRangeRule(cf.Rules[j]); AppendToStream(AStream, Format( '' + - '' + - '' + - '' + + '' + + '' + + '' + '', [ - ColorToHTMLColorStr(cf_ColorRangeRule.StartColor), - ColorToHTMLColorStr(cf_ColorRangeRule.CenterColor), - ColorToHTMLColorStr(cf_ColorRangeRule.EndColor) + cf_ColorRangeRule.StartValue, + CF_COLORRANGE_VALUE_KIND[cf_ColorRangeRule.StartValueKind], + ColorToHTMLColorStr(cf_ColorRangeRule.StartColor), + cf_ColorRangeRule.CenterValue, + CF_COLORRANGE_VALUE_KIND[cf_ColorRangeRule.CenterValueKind], + ColorToHTMLColorStr(cf_ColorRangeRule.CenterColor), + cf_ColorRangeRule.EndValue, + CF_COLORRANGE_VALUE_KIND[cf_ColorRangeRule.EndValueKind], + ColorToHTMLColorStr(cf_ColorRangeRule.EndColor) ])); end; end; diff --git a/components/fpspreadsheet/source/common/fpspreadsheet.pas b/components/fpspreadsheet/source/common/fpspreadsheet.pas index 0e07aa360..5ac30ab92 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet.pas +++ b/components/fpspreadsheet/source/common/fpspreadsheet.pas @@ -392,7 +392,12 @@ type function WriteConditionalCellFormat(ARange: TsCellRange; ACondition: TsCFCondition; AParam1, AParam2: Variant; ACellFormatIndex: Integer): Integer; overload; // color range - function WriteColorRange(ARange: TsCellRange; AStartColor, ACenterColor, AEndColor: TsColor): Integer; + function WriteColorRange(ARange: TsCellRange; AStartColor: TsColor = scRed; + ACenterColor: TsColor = scYellow; AEndColor: TsColor = scBlue): Integer; overload; + function WriteColorRange(ARange: TsCellRange; + AStartColor: TsColor; AStartKind: TsCFColorRangeValueKind; AStartValue: Double; + ACenterColor: TsColor; ACenterKind: TsCFColorRangeValueKind; ACenterValue: Double; + AEndColor: TsColor; AEndKind: TsCFColorRangeValueKind; AEndValue: Double): Integer; overload; // data bars function WriteDataBars(ARange: TsCellRange): Integer; diff --git a/components/fpspreadsheet/source/common/fpspreadsheet_cf.inc b/components/fpspreadsheet/source/common/fpspreadsheet_cf.inc index 41e793934..a868a72d5 100644 --- a/components/fpspreadsheet/source/common/fpspreadsheet_cf.inc +++ b/components/fpspreadsheet/source/common/fpspreadsheet_cf.inc @@ -69,13 +69,26 @@ end; Writes the conditional format "color range" -------------------------------------------------------------------------------} function TsWorksheet.WriteColorRange(ARange: TsCellRange; - AStartColor, ACenterColor, AEndColor: TsColor): Integer; + AStartColor: TsColor = scRed; ACenterColor: TsColor = scYellow; + AEndColor: TsColor = scBlue): Integer; begin Result := FWorkbook.FConditionalFormatList.AddColorRangeRule(Self, ARange, AStartColor, ACenterColor, AEndColor); StoreCFIndexInCells(Self, Result, ARange); end; +function TsWorksheet.WriteColorRange(ARange: TsCellRange; + AStartColor: TsColor; AStartKind: TsCFColorRangeValueKind; AStartValue: Double; + ACenterColor: TsColor; ACenterKind: TsCFColorRangeValueKind; ACenterValue: Double; + AEndColor: TsColor; AEndKind: TsCFColorRangeValueKind; AEndValue: Double): Integer; +begin + Result := FWorkbook.FConditionalFormatList.AddColorRangeRule(Self, ARange, + AStartColor, AStartKind, AStartValue, + ACenterColor, ACenterKind, ACenterValue, + AEndColor, AEndKind, AEndValue); + StoreCFIndexInCells(Self, Result, ARange); +end; + {@@ ---------------------------------------------------------------------------- Writes the conditional format "data bars" -------------------------------------------------------------------------------}