From 0875ce96e87ca64e27f22dc5142eafc87a5a36f5 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Tue, 17 Jun 2014 21:56:49 +0000 Subject: [PATCH] fpspreadsheet: Reading/writing of negative currencies in red now working for ods git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3189 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/fpsgrid/fpsgrid.lpi | 12 +---- .../examples/opendocdemo/opendocwrite.lpi | 8 +-- .../examples/opendocdemo/opendocwrite.lpr | 23 ++++++++- .../fpspreadsheet/fpsnumformatparser.pas | 3 +- components/fpspreadsheet/fpsopendocument.pas | 49 +++++++++++++------ components/fpspreadsheet/fpsutils.pas | 8 +-- 6 files changed, 63 insertions(+), 40 deletions(-) diff --git a/components/fpspreadsheet/examples/fpsgrid/fpsgrid.lpi b/components/fpspreadsheet/examples/fpsgrid/fpsgrid.lpi index ddd339b6d..9f93b143b 100644 --- a/components/fpspreadsheet/examples/fpsgrid/fpsgrid.lpi +++ b/components/fpspreadsheet/examples/fpsgrid/fpsgrid.lpi @@ -48,9 +48,6 @@ - - - @@ -82,9 +79,6 @@ - - - @@ -111,7 +105,7 @@ - + @@ -119,7 +113,6 @@ - @@ -144,9 +137,6 @@ - - - diff --git a/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpi b/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpi index 616b29f08..dcd0869e2 100644 --- a/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpi +++ b/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpi @@ -39,7 +39,7 @@ - + @@ -55,11 +55,5 @@ - - - - - - diff --git a/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpr b/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpr index 77b78dee8..02b79dde4 100644 --- a/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpr +++ b/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpr @@ -201,7 +201,7 @@ begin MyWorksheet.WriteCurrency(row, 7, number7, nfCurrency, 2, '$'); MyWorksheet.WriteCurrency(row, 8, number8, nfCurrency, 2, '$'); inc(row); - MyWorksheet.WriteUTF8Text(row, 0, 'nfCurrencyRed, 2 decimals, +:$ 1000, -:($ 1000)'); + MyWorksheet.WriteUTF8Text(row, 0, 'nfCurrencyRed, 2 decimals, >0: $ 1000, <0: ($ 1000)'); MyWorksheet.WriteCurrency(row, 1, number1, nfCurrencyRed, 2, '$', pcfCSV, ncfBCSVB); MyWorksheet.WriteCurrency(row, 2, number2, nfCurrencyRed, 2, '$', pcfCSV, ncfBCSVB); MyWorksheet.WriteCurrency(row, 3, number3, nfCurrencyRed, 2, '$', pcfCSV, ncfBCSVB); @@ -210,6 +210,27 @@ begin MyWorksheet.WriteCurrency(row, 6, number6, nfCurrencyRed, 2, '$', pcfCSV, ncfBCSVB); MyWorksheet.WriteCurrency(row, 7, number7, nfCurrencyRed, 2, '$', pcfCSV, ncfBCSVB); MyWorksheet.WriteCurrency(row, 8, number8, nfCurrencyRed, 2, '$', pcfCSV, ncfBCSVB); + inc(row); + // Note: nfAccounting's not supported by ods, will be replaced by nfCurrency's + MyWorksheet.WriteUTF8Text(row, 0, 'nfAccounting, 2 decimals'); + MyWorksheet.WriteCurrency(row, 1, number1, nfAccounting, 2, '$'); + MyWorksheet.WriteCurrency(row, 2, number2, nfAccounting, 2, '$'); + MyWorksheet.WriteCurrency(row, 3, number3, nfAccounting, 2, '$'); + MyWorksheet.WriteCurrency(row, 4, number4, nfAccounting, 2, '$'); + MyWorksheet.WriteCurrency(row, 5, number5, nfAccounting, 2, '$'); + MyWorksheet.WriteCurrency(row, 6, number6, nfAccounting, 2, '$'); + MyWorksheet.WriteCurrency(row, 7, number7, nfAccounting, 2, '$'); + MyWorksheet.WriteCurrency(row, 8, number8, nfAccounting, 2, '$'); + inc(row); + MyWorksheet.WriteUTF8Text(row, 0, 'nfAccountingRed, 2 decimals, >0: EUR 1000, <0: -EUR 1000)'); + MyWorksheet.WriteCurrency(row, 1, number1, nfAccountingRed, 2, 'EUR', pcfCSV, ncfMCSV); + MyWorksheet.WriteCurrency(row, 2, number2, nfAccountingRed, 2, 'EUR', pcfCSV, ncfMCSV); + MyWorksheet.WriteCurrency(row, 3, number3, nfAccountingRed, 2, 'EUR', pcfCSV, ncfMCSV); + MyWorksheet.WriteCurrency(row, 4, number4, nfAccountingRed, 2, 'EUR', pcfCSV, ncfMCSV); + MyWorksheet.WriteCurrency(row, 5, number5, nfAccountingRed, 2, 'EUR', pcfCSV, ncfMCSV); + MyWorksheet.WriteCurrency(row, 6, number6, nfAccountingRed, 2, 'EUR', pcfCSV, ncfMCSV); + MyWorksheet.WriteCurrency(row, 7, number7, nfAccountingRed, 2, 'EUR', pcfCSV, ncfMCSV); + MyWorksheet.WriteCurrency(row, 8, number8, nfAccountingRed, 2, 'EUR', pcfCSV, ncfMCSV); // Creates a new worksheet MyWorksheet := MyWorkbook.AddWorksheet('My Worksheet 2'); diff --git a/components/fpspreadsheet/fpsnumformatparser.pas b/components/fpspreadsheet/fpsnumformatparser.pas index 3e41667a7..778b382d8 100644 --- a/components/fpspreadsheet/fpsnumformatparser.pas +++ b/components/fpspreadsheet/fpsnumformatparser.pas @@ -275,7 +275,8 @@ var uValue: String; begin uValue := Uppercase(AValue); - Result := (uValue = '$') or (uValue = 'USD') or + Result := (uValue = Uppercase(AnsiToUTF8(FWorkbook.FormatSettings.CurrencyString))) or + (uValue = '$') or (uValue = 'USD') or (uValue = '€') or (uValue = 'EUR') or (uValue = '£') or (uValue = 'GBP') or (uValue = '¥') or (uValue = 'JPY'); diff --git a/components/fpspreadsheet/fpsopendocument.pas b/components/fpspreadsheet/fpsopendocument.pas index 1f88bae18..076980cef 100755 --- a/components/fpspreadsheet/fpsopendocument.pas +++ b/components/fpspreadsheet/fpsopendocument.pas @@ -512,6 +512,16 @@ begin Result := Result + AIndent + ' ' + LineEnding; + nftRepeat: + begin + if FSections[ASection].Elements[el].TextValue = ' ' then + s := '' else + s := FSections[ASection].Elements[el].TextValue; + Result := Result + AIndent + + ' ' + s + '' + LineEnding; + inc(el); + end + else inc(el); end; // case @@ -1280,6 +1290,7 @@ procedure TsSpreadOpenDocReader.ReadNumFormats(AStylesNode: TDOMNode); styleindex: Integer; fmt: String; posfmt, negfmt, zerofmt: String; + nf: TsNumberFormat; begin posfmt := ''; negfmt := ''; @@ -1309,14 +1320,16 @@ procedure TsSpreadOpenDocReader.ReadNumFormats(AStylesNode: TDOMNode); end; fmt := FNumFormatList[styleindex].FormatString; + nf := FNumFormatList[styleindex].NumFormat; + if nf in [nfCurrency, nfCurrencyRed] then ANumFormat := nf; case condition[1] of - '<': begin - negfmt := fmt; + '>': begin + posfmt := fmt; if (Length(condition) > 1) and (condition[2] = '=') then zerofmt := fmt; end; - '>': begin - posfmt := fmt; + '<': begin + negfmt := fmt; if (Length(condition) > 1) and (condition[2] = '=') then zerofmt := fmt; end; @@ -1333,7 +1346,8 @@ procedure TsSpreadOpenDocReader.ReadNumFormats(AStylesNode: TDOMNode); if negfmt <> '' then AFormatStr := AFormatStr + ';' + negfmt; if zerofmt <> '' then AFormatStr := AFormatStr + ';' + zerofmt; - ANumFormat := nfCustom; + if not (ANumFormat in [nfCurrency, nfCurrencyRed]) then + ANumFormat := nfCustom; end; procedure ReadNumberStyle(ANumFormatNode: TDOMNode; ANumFormatName: String); @@ -1361,7 +1375,7 @@ procedure TsSpreadOpenDocReader.ReadNumFormats(AStylesNode: TDOMNode); Continue; end else if nodeName = 'number:number' then begin - s := GetAttrValue(node, 'number:decimal-places'); + s := GetAttrValue(node, 'decimal-places'); if s <> '' then decs := StrToInt(s) else decs := 0; grouping := GetAttrValue(node, 'number:grouping') = 'true'; nf := IfThen(grouping, nfFixedTh, nfFixed); @@ -1393,15 +1407,17 @@ procedure TsSpreadOpenDocReader.ReadNumFormats(AStylesNode: TDOMNode); end else if nodeName = 'style:text-properties' then begin s := GetAttrValue(node, 'fo:color'); - color := HTMLColorStrToColor(s); - idx := FWorkbook.AddColorToPalette(color); - { - if idx < 8 then - fmt := Format('[%s]%s', [FWorkbook.GetColorName(idx), fmt]) - else - fmt := Format('[Color%d]%s', [idx, fmt]); + if s <> '' then begin + hasColor := true; + { // currently not needed + color := HTMLColorStrToColor(s); + idx := FWorkbook.AddColorToPalette(color); + if idx < 8 then + fmt := Format('[%s]%s', [FWorkbook.GetColorName(idx), fmt]) + else + fmt := Format('[Color%d]%s', [idx, fmt]); } - hasColor := true; + end; end; node := node.NextSibling; end; @@ -1412,8 +1428,9 @@ procedure TsSpreadOpenDocReader.ReadNumFormats(AStylesNode: TDOMNode); if ANumFormatNode.NodeName = 'number:percentage-style' then nf := nfPercentage - else if ANumFormatNode.NodeName = 'number:currency-style' then begin - if hasColor then nf := nfCurrencyRed else nf := nfCurrency; + else if (ANumFormatNode.NodeName = 'number:currency-style') then begin + if not (nf in [nfCurrency, nfCurrencyRed]) then + nf := IfThen(hasColor, nfCurrencyred, nfCurrency); end; NumFormatList.AddFormat(ANumFormatName, fmt, nf); diff --git a/components/fpspreadsheet/fpsutils.pas b/components/fpspreadsheet/fpsutils.pas index b80061845..c17a4c592 100644 --- a/components/fpspreadsheet/fpsutils.pas +++ b/components/fpspreadsheet/fpsutils.pas @@ -652,7 +652,7 @@ const ('%0:s-"%1:s"', '%0:s-"%1:s"'), // 6: 1-$ ('%0:s"%1:s"-', '%0:s-"%1:s"'), // 7: 1$- ('-%0:s "%1:s"', '-* %0:s"%1:s"'), // 8: -1 $ - ('-"%1:s" %0:s', '-* "%1:s" -%0:s'), // 9: -$ 1 + ('-"%1:s" %0:s', '-* "%1:s" %0:s'), // 9: -$ 1 ('%0:s "%1:s"-', '%0:s- "%1:s"'), // 10: 1 $- ('"%1:s" %0:s-', '"%1:s"* %0:s-'), // 11: $ 1- ('"%1:s" -%0:s', '"%1:s"* -%0:s'), // 12: $ -1 @@ -662,12 +662,12 @@ const ); var decs: String; - cf, ncf: Byte; + pcf, ncf: Byte; p, n: String; accStyle: Boolean; negRed: Boolean; begin - cf := IfThen(APosCurrFormat < 0, AFormatSettings.CurrencyFormat, APosCurrFormat); + pcf := IfThen(APosCurrFormat < 0, AFormatSettings.CurrencyFormat, APosCurrFormat); ncf := IfThen(ANegCurrFormat < 0, AFormatSettings.NegCurrFormat, ANegCurrFormat); if ADecimals < 0 then ADecimals := AFormatSettings.CurrencyDecimals; @@ -679,7 +679,7 @@ begin accStyle := ANumberFormat in [nfAccounting, nfAccountingRed]; negRed := ANumberFormat in [nfCurrencyRed, nfAccountingRed]; - p := POS_FMT[cf, accStyle]; + p := POS_FMT[pcf, accStyle]; n := NEG_FMT[ncf, accStyle]; // add extra space for the sign of the number for perfect alignment in Excel if accStyle then