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
This commit is contained in:
wp_xxyyzz
2014-04-25 09:32:34 +00:00
parent 1541796ecd
commit 4a16f9bb90
4 changed files with 97 additions and 7 deletions

View File

@ -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);

View File

@ -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

View File

@ -860,9 +860,12 @@ begin
if Assigned(lCell) then begin
xfData := TXFData(FXFList.items[xf]);
// Font index
// Font index, "bold" attribute
if xfData.FontIndex = 1 then
Include(lCell^.UsedFormattingFields, uffBold)
else
Include(lCell^.UsedFormattingFields, uffFont);
lCell^.FontIndex := xfData.FontIndex; //AFont;
lCell^.FontIndex := xfData.FontIndex;
// Horizontal justification
if AStyle and $07 <> 0 then begin

View File

@ -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;
// Alignment
lCell^.HorAlignment := XFData.HorAlignment;