From d0fe6490d8a90267453f671eee1bc21268790ab8 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Tue, 21 Jul 2020 21:34:39 +0000 Subject: [PATCH] fpspreadsheet: ODS reader supports conditional DataBar format. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@7554 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../source/common/fpsopendocument.pas | 83 ++++++++++++++++--- .../tests/conditionalformattests.pas | 27 +++++- 2 files changed, 98 insertions(+), 12 deletions(-) diff --git a/components/fpspreadsheet/source/common/fpsopendocument.pas b/components/fpspreadsheet/source/common/fpsopendocument.pas index 3fbd7314a..aba277611 100644 --- a/components/fpspreadsheet/source/common/fpsopendocument.pas +++ b/components/fpspreadsheet/source/common/fpsopendocument.pas @@ -446,6 +446,19 @@ begin end; end; +function StrToValueKind(s: String): TsCFValueKind; +var + vk: TsCFValuekind; +begin + for vk in TsCFValueKind do + if CF_VALUE_KIND[vk] = s then + begin + Result := vk; + exit; + end; + Result := vkNone; +end; + type { Table style items stored in TableStyleList of the reader } @@ -3956,19 +3969,12 @@ begin begin s := GetAttrValue(ANode, 'calcext:value'); SetLength(values, Length(values)+1); - if not TryStrToFloat(s, values[High(values)]) then + if not TryStrToFloat(s, values[High(values)], FPointSeparatorSettings) then values[High(values)] := 0; s := GetAttrValue(ANode, 'calcext:type'); SetLength(kinds, Length(kinds)+1); - case s of - '' : kinds[High(kinds)] := vkNone; - 'minimum': kinds[High(kinds)] := vkMin; - 'maximum': kinds[High(kinds)] := vkMax; - 'percent': kinds[High(kinds)] := vkPercent; - 'percentile': kinds[High(kinds)] := vkPercentile; - 'number': kinds[High(kinds)] := vkValue; - end; + kinds[High(kinds)] := StrToValueKind(s); s := GetAttrvalue(ANode, 'calcext:color'); if s <> '' then @@ -4001,11 +4007,68 @@ end; procedure TsSpreadOpenDocReader.ReadCFDataBars(ANode: TDOMNode; ASheet: TsBasicWorksheet; ARange: TsCellRange); +{ + + + } var sheet: TsWorksheet; + nodeName: String; + s: String; + values: array of double = nil; + kinds: array of TsCFValueKind = nil; + posColor, negColor: TsColor; + n: Integer; begin + if ANode = nil then + exit; + sheet := TsWorksheet(ASheet); - //... + + s := GetAttrValue(ANode, 'calcext:positive-color'); + if s <> '' then + posColor := HTMLColorStrToColor(s) + else + posColor := scNotDefined; + + s := GetAttrValue(ANode, 'calcext:negative-color'); + if s <> '' then + negColor := HTMLColorStrToColor(s) + else + negColor := scNotDefined; + + ANode := ANode.FirstChild; + while ANode <> nil do + begin + nodeName := ANode.NodeName; + if nodeName = 'calcext:formatting-entry' then + begin + s := GetAttrValue(ANode, 'calcext:value'); + SetLength(values, Length(values)+1); + if not TryStrToFloat(s, values[High(values)], FPointSeparatorSettings) then + values[High(values)] := 0; + + s := GetAttrValue(ANode, 'calcext:type'); + SetLength(kinds, Length(kinds)+1); + kinds[High(kinds)] := StrToValueKind(s); + end; + ANode := ANode.NextSibling; + end; + + // We only support a single color, ATM. + if (posColor = scNotDefined) and (negColor <> scNotDefined) then + posColor := negColor; + + n := MaxValue([Length(values), Length(kinds)]); + if n < 2 then + exit; + + sheet.WriteDataBars( + ARange, + posColor, + kinds[0], values[0], + kinds[1], values[1] + ); end; { Reads the cells in the given table. Loops through all rows, and then finds all diff --git a/components/fpspreadsheet/tests/conditionalformattests.pas b/components/fpspreadsheet/tests/conditionalformattests.pas index 42b159d8e..7a7532cae 100644 --- a/components/fpspreadsheet/tests/conditionalformattests.pas +++ b/components/fpspreadsheet/tests/conditionalformattests.pas @@ -147,6 +147,10 @@ type procedure TestWriteRead_CF_ColorRange_ODS_2C_Full; procedure TestWriteRead_CF_ColorRange_ODS_3C_Simple; procedure TestWriteRead_CF_ColorRange_ODS_2C_Simple; + + procedure TestWriteRead_CF_Databars_ODS_Full; + procedure TestWriteRead_CF_Databars_ODS_Simple; + end; implementation @@ -1356,6 +1360,9 @@ begin end; end; + +{ Excel XLSX } + procedure TSpreadWriteReadCFTests.TestWriteRead_CF_ColorRange_XLSX_3C_Full; begin TestWriteRead_CF_ColorRange(sfOOXML, true, true); @@ -1376,6 +1383,7 @@ begin TestWriteRead_CF_ColorRange(sfOOXML, false, false); end; + { OpenDocument } procedure TSpreadWriteReadCFTests.TestWriteRead_CF_ColorRange_ODS_3C_Full; @@ -1506,17 +1514,32 @@ begin end; end; + +{ Excel XLSX } + procedure TSpreadWriteReadCFTests.TestWriteRead_CF_Databars_XLSX_Full; begin - TestwriteRead_CF_DataBars(sfOOXML, true); + TestWriteRead_CF_DataBars(sfOOXML, true); end; procedure TSpreadWriteReadCFTests.TestWriteRead_CF_Databars_XLSX_Simple; begin - TestwriteRead_CF_DataBars(sfOOXML, false); + TestWriteRead_CF_DataBars(sfOOXML, false); end; +{ OpenDocument } + +procedure TSpreadWriteReadCFTests.TestWriteRead_CF_Databars_ODS_Full; +begin + TestwriteRead_CF_DataBars(sfOpenDocument, true); +end; + +procedure TSpreadWriteReadCFTests.TestWriteRead_CF_Databars_ODS_Simple; +begin + TestwriteRead_CF_DataBars(sfOpenDocument, false); +end; + initialization RegisterTest(TSpreadWriteReadCFTests);