From a1fe9499085bfc97d455f0f8fa475a6149d9a3a0 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 29 Oct 2014 22:36:03 +0000 Subject: [PATCH] fpspreadsheet: Fix unit test for currencies, pass now for all file format (some kind of tweaking for Excel2 and fpc formatting inconsistencies though). git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3699 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/spready/spready.lpi | 4 ---- components/fpspreadsheet/fpscsv.pas | 2 +- .../fpspreadsheet/fpsnumformatparser.pas | 14 ++---------- components/fpspreadsheet/fpsopendocument.pas | 5 ++++- components/fpspreadsheet/fpspreadsheet.pas | 2 +- .../fpspreadsheet/tests/formattests.pas | 22 ++++++++++++++----- .../fpspreadsheet/tests/spreadtestgui.lpi | 1 + components/fpspreadsheet/xlsbiff2.pas | 8 +++---- components/fpspreadsheet/xlscommon.pas | 8 +++---- 9 files changed, 34 insertions(+), 32 deletions(-) diff --git a/components/fpspreadsheet/examples/spready/spready.lpi b/components/fpspreadsheet/examples/spready/spready.lpi index 636f792a1..6094368b7 100644 --- a/components/fpspreadsheet/examples/spready/spready.lpi +++ b/components/fpspreadsheet/examples/spready/spready.lpi @@ -116,7 +116,6 @@ - @@ -132,19 +131,16 @@ - - - diff --git a/components/fpspreadsheet/fpscsv.pas b/components/fpspreadsheet/fpscsv.pas index 6d134f6a4..22b97f371 100644 --- a/components/fpspreadsheet/fpscsv.pas +++ b/components/fpspreadsheet/fpscsv.pas @@ -359,7 +359,7 @@ begin if IsNumber(AText, dblValue, nf, decs, currSym, warning) then begin if currSym <> '' then - FWorksheet.WriteCurrency(ARow, ACol, dblValue, nfCurrency, -1, currSym) + FWorksheet.WriteCurrency(ARow, ACol, dblValue, nfCurrency, decs, currSym) else FWorksheet.WriteNumber(ARow, ACol, dblValue, nf, decs); if warning <> '' then diff --git a/components/fpspreadsheet/fpsnumformatparser.pas b/components/fpspreadsheet/fpsnumformatparser.pas index d2c2abae9..421d24e8a 100644 --- a/components/fpspreadsheet/fpsnumformatparser.pas +++ b/components/fpspreadsheet/fpsnumformatparser.pas @@ -261,21 +261,11 @@ begin end; function TsNumFormatParser.AnalyzeCurrency(const AValue: String): Boolean; -var - uValue: String; begin if (FWorkbook = nil) or (FWorkbook.FormatSettings.CurrencyString = '') then Result := false else Result := CurrencyRegistered(AValue); - { - uValue := UTF8Uppercase(AValue); - Result := (uValue = UTF8Uppercase(FWorkbook.FormatSettings.CurrencyString)) or - (uValue = '$') or (uValue = 'USD') or - (uValue = '€') or (uValue = 'EUR') or - (uValue = '£') or (uValue = 'GBP') or - (uValue = '¥') or (uValue = 'JPY'); - } end; { Creates a formatstring for all sections. @@ -1112,7 +1102,7 @@ begin (FSections[ASection].Elements[AIndex].Token = AToken); end; -{ Limits the decimals to 0 or 2, as required by Excel } +{ Limits the decimals to 0 or 2, as required by Excel2. } procedure TsNumFormatParser.LimitDecimals; var i, j: Integer; @@ -1126,7 +1116,7 @@ end; { Localizes the thousand- and decimal separator symbols by replacing them with the FormatSettings value of the workbook. A recreated format string will be - localized as required by Excel2 } + localized as required by Excel2. } procedure TsNumFormatParser.Localize; var i, j: Integer; diff --git a/components/fpspreadsheet/fpsopendocument.pas b/components/fpspreadsheet/fpsopendocument.pas index 46f414f03..2daa1fbe8 100755 --- a/components/fpspreadsheet/fpsopendocument.pas +++ b/components/fpspreadsheet/fpsopendocument.pas @@ -1668,10 +1668,13 @@ procedure TsSpreadOpenDocReader.ReadNumFormats(AStylesNode: TDOMNode); end else if nodeName = 'number:number' then begin + { if ANumFormatName = 'number:currency-style' then s := GetAttrValue(node, 'decimal-places') else - s := GetAttrValue(node, 'number:decimal-places'); + } + s := GetAttrValue(node, 'number:decimal-places'); + if s = '' then 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); diff --git a/components/fpspreadsheet/fpspreadsheet.pas b/components/fpspreadsheet/fpspreadsheet.pas index 12cf36c6a..b85d6fd62 100755 --- a/components/fpspreadsheet/fpspreadsheet.pas +++ b/components/fpspreadsheet/fpspreadsheet.pas @@ -198,7 +198,7 @@ const ncfCMV = 2; // $-1000 ncfCVM = 3; // $1000- ncfBVCB = 4; // (1000$) - ccfMVC = 5; // -1000$ + ncfMVC = 5; // -1000$ ncfVMC = 6; // 1000-$ ncfVCM = 7; // 1000$- ncfMVSC = 8; // -1000 $ diff --git a/components/fpspreadsheet/tests/formattests.pas b/components/fpspreadsheet/tests/formattests.pas index ad4256607..1485c8639 100644 --- a/components/fpspreadsheet/tests/formattests.pas +++ b/components/fpspreadsheet/tests/formattests.pas @@ -171,10 +171,16 @@ var begin // Set up norm - MUST match spreadsheet cells exactly - // The workbook uses a slightly modified copy of the DefaultFormatSettings - // We create a copy here in order to better define the predicted strings. myWorkbook := TsWorkbook.Create; try + // There are some inconsistencies in fpc number-to-string conversions regarding + // thousand separators and usage of - sign for very small numbers. + // Therefore, we force the currency format to a given specification and build + // the expected string accordingly. + MyWorkbook.FormatSettings.CurrencyString := '€'; // use € for checking UTF8 issues + // To get matching results also for Excel2 let't use its currency-value sequence. + MyWorkbook.FormatSettings.Currencyformat := pcfCV; // €100 + Myworkbook.FormatSettings.NegCurrFormat := ncfBCVB; // (€100) fs := MyWorkbook.FormatSettings; finally myWorkbook.Free; @@ -197,7 +203,7 @@ begin SollNumberFormats[5] := nfExp; SollNumberDecimals[5] := 2; SollNumberFormats[6] := nfPercentage; SollNumberDecimals[6] := 0; SollNumberFormats[7] := nfPercentage; SollNumberDecimals[7] := 2; - SollNumberFormats[8] := nfCurrency; SollNumberDecimals[8] := 1; // This should be 0, but there is a bug in fpc, issue #0026944) + SollNumberFormats[8] := nfCurrency; SollNumberDecimals[8] := 0; SollNumberFormats[9] := nfCurrency; SollNumberDecimals[9] := 2; SollNumberstrings[0, 0] := CurrToStrF(-1000.1, ffCurrency, 0, fs); @@ -212,8 +218,8 @@ begin SollNumberStrings[i, 5] := FormatFloat('0.00E+00', SollNumbers[i], fs); SollNumberStrings[i, 6] := FormatFloat('0', SollNumbers[i]*100, fs) + '%'; SollNumberStrings[i, 7] := FormatFloat('0.00', SollNumbers[i]*100, fs) + '%'; - SollNumberStrings[i, 8] := CurrToStrF(SollNumbers[i], ffCurrency, SollNumberDecimals[8], fs); - SollNumberStrings[i, 9] := CurrToStrF(SollNumbers[i], ffCurrency, SollNumberDecimals[9], fs); + SollNumberStrings[i, 8] := FormatCurr('"€"#,##0;("€"#,##0)', SollNumbers[i], fs); + SollNumberStrings[i, 9] := FormatCurr('"€"#,##0.00;("€"#,##0.00)', SollNumbers[i], fs); end; // Date/time values @@ -324,6 +330,9 @@ begin // Write out all test values MyWorkbook := TsWorkbook.Create; try + MyWorkbook.FormatSettings.CurrencyString := '€'; // use € for checking UTF8 issues + MyWorkbook.FormatSettings.Currencyformat := pcfCV; // €100 + Myworkbook.FormatSettings.NegCurrFormat := ncfBCVB; // (€100) if (AFormat = sfCSV) then begin case AVariant of @@ -359,6 +368,9 @@ begin // Open the spreadsheet MyWorkbook := TsWorkbook.Create; try + MyWorkbook.FormatSettings.CurrencyString := '€'; // use € for checking UTF8 issues + MyWorkbook.FormatSettings.Currencyformat := pcfCV; // €100 + Myworkbook.FormatSettings.NegCurrFormat := ncfBCVB; // (€100) MyWorkbook.ReadFromFile(TempFile, AFormat); if AFormat in [sfExcel2, sfCSV] then MyWorksheet := MyWorkbook.GetFirstWorksheet diff --git a/components/fpspreadsheet/tests/spreadtestgui.lpi b/components/fpspreadsheet/tests/spreadtestgui.lpi index a84f32567..fcb695355 100644 --- a/components/fpspreadsheet/tests/spreadtestgui.lpi +++ b/components/fpspreadsheet/tests/spreadtestgui.lpi @@ -97,6 +97,7 @@ + diff --git a/components/fpspreadsheet/xlsbiff2.pas b/components/fpspreadsheet/xlsbiff2.pas index ee17b31c3..4e15ebc38 100755 --- a/components/fpspreadsheet/xlsbiff2.pas +++ b/components/fpspreadsheet/xlsbiff2.pas @@ -270,10 +270,10 @@ begin AddFormat( 2, '0.00', nfFixed); AddFormat( 3, '#,##0', nfFixedTh); AddFormat( 4, '#,##0.00', nfFixedTh); - AddFormat( 5, '"'+cs+'"#,##0_);("'+cs+'"#,##0)', nfCurrency); - AddFormat( 6, '"'+cs+'"#,##0_);[Red]("'+cs+'"#,##0)', nfCurrencyRed); - AddFormat( 7, '"'+cs+'"#,##0.00_);("'+cs+'"#,##0.00)', nfCurrency); - AddFormat( 8, '"'+cs+'"#,##0.00_);[Red]("'+cs+'"#,##0.00)', nfCurrency); + AddFormat( 5, '"'+cs+'"#,##0;("'+cs+'"#,##0)', nfCurrency); + AddFormat( 6, '"'+cs+'"#,##0;[Red]("'+cs+'"#,##0)', nfCurrencyRed); + AddFormat( 7, '"'+cs+'"#,##0.00;("'+cs+'"#,##0.00)', nfCurrency); + AddFormat( 8, '"'+cs+'"#,##0.00;[Red]("'+cs+'"#,##0.00)', nfCurrency); AddFormat( 9, '0%', nfPercentage); AddFormat(10, '0.00%', nfPercentage); AddFormat(11, '0.00E+00', nfExp); diff --git a/components/fpspreadsheet/xlscommon.pas b/components/fpspreadsheet/xlscommon.pas index 11df6478e..2e1c03b39 100644 --- a/components/fpspreadsheet/xlscommon.pas +++ b/components/fpspreadsheet/xlscommon.pas @@ -568,10 +568,10 @@ begin AddFormat( 2, '0.00', nfFixed); AddFormat( 3, '#,##0', nfFixedTh); AddFormat( 4, '#,##0.00', nfFixedTh); - AddFormat( 5, '"'+cs+'"#,##0_);("'+cs+'"#,##0)', nfCurrency); - AddFormat( 6, '"'+cs+'"#,##0_);[Red]("'+cs+'"#,##0)', nfCurrencyRed); - AddFormat( 7, '"'+cs+'"#,##0.00_);("'+cs+'"#,##0.00)', nfCurrency); - AddFormat( 8, '"'+cs+'"#,##0.00_);[Red]("'+cs+'"#,##0.00)', nfCurrencyRed); + AddFormat( 5, '"'+cs+'"#,##0;("'+cs+'"#,##0)', nfCurrency); + AddFormat( 6, '"'+cs+'"#,##0;[Red]("'+cs+'"#,##0)', nfCurrencyRed); + AddFormat( 7, '"'+cs+'"#,##0.00;("'+cs+'"#,##0.00)', nfCurrency); + AddFormat( 8, '"'+cs+'"#,##0.00;[Red]("'+cs+'"#,##0.00)', nfCurrencyRed); AddFormat( 9, '0%', nfPercentage); AddFormat(10, '0.00%', nfPercentage); AddFormat(11, '0.00E+00', nfExp);