From 4a16f9bb90b0ba6fe58cbf3f059987d7b91d2765 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Fri, 25 Apr 2014 09:32:34 +0000 Subject: [PATCH] fpspreadsheet: Add unit test for "bold" attribute in BIFF2 and BIFF8. Fix "bold" being incorrectly restored by reading of both formats. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@2966 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../examples/excel2demo/excel2write.lpr | 2 +- components/fpspreadsheet/tests/fonttests.pas | 85 +++++++++++++++++++ components/fpspreadsheet/xlsbiff2.pas | 9 +- components/fpspreadsheet/xlsbiff8.pas | 8 +- 4 files changed, 97 insertions(+), 7 deletions(-) diff --git a/components/fpspreadsheet/examples/excel2demo/excel2write.lpr b/components/fpspreadsheet/examples/excel2demo/excel2write.lpr index b5d7a66ca..432d4c874 100644 --- a/components/fpspreadsheet/examples/excel2demo/excel2write.lpr +++ b/components/fpspreadsheet/examples/excel2demo/excel2write.lpr @@ -30,7 +30,7 @@ begin // Write some number cells MyWorksheet.WriteNumber(0, 0, 1.0); - // MyWorksheet.WriteFont(0, 0, 'Arial', 11, [fssBold, fssItalic], scBlack); + MyWorksheet.WriteUsedFormatting(0, 0, [uffBold]); MyWorksheet.WriteNumber(0, 1, 2.0); MyWorksheet.WriteNumber(0, 2, 3.0); diff --git a/components/fpspreadsheet/tests/fonttests.pas b/components/fpspreadsheet/tests/fonttests.pas index 413fd5acd..ac2f725b9 100644 --- a/components/fpspreadsheet/tests/fonttests.pas +++ b/components/fpspreadsheet/tests/fonttests.pas @@ -33,15 +33,18 @@ type // Set up expected values: procedure SetUp; override; procedure TearDown; override; + procedure TestWriteReadBold(AFormat: TsSpreadsheetFormat); procedure TestWriteReadFont(AFormat: TsSpreadsheetFormat; AFontName: String); published // BIFF2 test cases + procedure TestWriteReadBoldBIFF2; procedure TestWriteReadFontBIFF2_Arial; procedure TestWriteReadFontBIFF2_TimesNewRoman; procedure TestWriteReadFontBIFF2_CourierNew; // BIFF8 test cases + procedure TestWriteReadBoldBIFF8; procedure TestWriteReadFontBIFF8_Arial; procedure TestWriteReadFontBIFF8_TimesNewRoman; procedure TestWriteReadFontBIFF8_CourierNew; @@ -109,6 +112,88 @@ begin inherited TearDown; end; +procedure TSpreadWriteReadFontTests.TestWriteReadBold(AFormat: TsSpreadsheetFormat); +var + MyWorksheet: TsWorksheet; + MyWorkbook: TsWorkbook; + row, col: Integer; + MyCell: PCell; + TempFile: string; //write xls/xml to this file and read back from it + currValue: String; + expectedValue: String; +begin + TempFile:=GetTempFileName; + {// Not needed: use workbook.writetofile with overwrite=true + if fileexists(TempFile) then + DeleteFile(TempFile); + } + MyWorkbook := TsWorkbook.Create; + MyWorkSheet:= MyWorkBook.AddWorksheet(FontSheet); + + // Write out a cell without "bold"formatting style + row := 0; + col := 0; + MyWorksheet.WriteUTF8Text(row, col, 'not bold'); + MyCell := MyWorksheet.FindCell(row, col); + if MyCell = nil then + fail('Error in test code. Failed to get cell.'); + CheckEquals(uffBold in MyCell^.UsedFormattingFields, false, + 'Test unsaved bold attribute, cell '+CellNotation(MyWorksheet,Row,Col)); + + // Write out a cell with "bold"formatting style + inc(row); + MyWorksheet.WriteUTF8Text(row, col, 'bold'); + MyWorksheet.WriteUsedFormatting(row, col, [uffBold]); + MyCell := MyWorksheet.FindCell(row, col); + if MyCell = nil then + fail('Error in test code. Failded to get cell.'); + CheckEquals(uffBold in MyCell^.UsedFormattingFields, true, + 'Test unsaved bold attribute, cell '+CellNotation(MyWorksheet,Row, Col)); + + MyWorkBook.WriteToFile(TempFile, AFormat, true); + MyWorkbook.Free; + + // Open the spreadsheet, as biff8 + MyWorkbook := TsWorkbook.Create; + MyWorkbook.ReadFromFile(TempFile, AFormat); + if AFormat = sfExcel2 then + MyWorksheet := MyWorkbook.GetFirstWorksheet // only 1 sheet for BIFF2 + else + MyWorksheet := GetWorksheetByName(MyWorkBook, FontSheet); + if MyWorksheet=nil then + fail('Error in test code. Failed to get named worksheet'); + + // Try to read cell without "bold" + row := 0; + col := 0; + MyCell := MyWorksheet.FindCell(row, col); + if MyCell = nil then + fail('Error in test code. Failed to get cell.'); + CheckEquals(uffBold in MyCell^.UsedFormattingFields, false, + 'Test saved bold attribute, cell '+CellNotation(MyWorksheet,row,col)); + + // Try to read cell with "bold" + inc(row); + MyCell := MyWorksheet.FindCell(row, col); + if MyCell = nil then + fail('Error in test code. Failed to get cell.'); + CheckEquals(uffBold in MyCell^.UsedFormattingFields, true, + 'Test saved bold attribute, cell '+CellNotation(MyWorksheet,row,col)); + + MyWorkbook.Free; + DeleteFile(TempFile); +end; + +procedure TSpreadWriteReadFontTests.TestWriteReadBoldBIFF2; +begin + TestWriteReadBold(sfExcel2); +end; + +procedure TSpreadWriteReadFontTests.TestWriteReadBoldBIFF8; +begin + TestWriteReadBold(sfExcel8); +end; + procedure TSpreadWriteReadFontTests.TestWriteReadFont(AFormat: TsSpreadsheetFormat; AFontName: String); var diff --git a/components/fpspreadsheet/xlsbiff2.pas b/components/fpspreadsheet/xlsbiff2.pas index 443d6c35c..d17070042 100755 --- a/components/fpspreadsheet/xlsbiff2.pas +++ b/components/fpspreadsheet/xlsbiff2.pas @@ -860,9 +860,12 @@ begin if Assigned(lCell) then begin xfData := TXFData(FXFList.items[xf]); - // Font index - Include(lCell^.UsedFormattingFields, uffFont); - lCell^.FontIndex := xfData.FontIndex; //AFont; + // Font index, "bold" attribute + if xfData.FontIndex = 1 then + Include(lCell^.UsedFormattingFields, uffBold) + else + Include(lCell^.UsedFormattingFields, uffFont); + lCell^.FontIndex := xfData.FontIndex; // Horizontal justification if AStyle and $07 <> 0 then begin diff --git a/components/fpspreadsheet/xlsbiff8.pas b/components/fpspreadsheet/xlsbiff8.pas index 560c90bb5..e8f28a167 100755 --- a/components/fpspreadsheet/xlsbiff8.pas +++ b/components/fpspreadsheet/xlsbiff8.pas @@ -2109,10 +2109,12 @@ begin XFData := TXFRecordData(FXFList.Items[XFIndex]); // Font - if XFData.FontIndex > 0 then begin + if XFData.FontIndex = 1 then + Include(lCell^.UsedFormattingFields, uffBold) + else + if XFData.FontIndex > 0 then Include(lCell^.UsedFormattingFields, uffFont); - lCell^.FontIndex := XFData.FontIndex; - end; + lCell^.FontIndex := XFData.FontIndex; // Alignment lCell^.HorAlignment := XFData.HorAlignment;