fpspreadsheet: Writing and reading of horizontal and vertical text alignments in ods files. Add test cases. Pass.

git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@3116 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
wp_xxyyzz
2014-05-28 20:52:36 +00:00
parent d6907ceb0b
commit 4dd961b590
3 changed files with 87 additions and 25 deletions

View File

@@ -1,4 +1,4 @@
<?xml version="1.0"?> <?xml version="1.0" encoding="UTF-8"?>
<CONFIG> <CONFIG>
<ProjectOptions> <ProjectOptions>
<Version Value="9"/> <Version Value="9"/>
@@ -44,7 +44,7 @@
</Units> </Units>
</ProjectOptions> </ProjectOptions>
<CompilerOptions> <CompilerOptions>
<Version Value="9"/> <Version Value="11"/>
<PathDelim Value="\"/> <PathDelim Value="\"/>
<SearchPaths> <SearchPaths>
<OtherUnitFiles Value=".."/> <OtherUnitFiles Value=".."/>

View File

@@ -99,7 +99,9 @@ type
private private
function WriteBackgroundColorStyleXMLAsString(const AFormat: TCell): String; function WriteBackgroundColorStyleXMLAsString(const AFormat: TCell): String;
function WriteBorderStyleXMLAsString(const AFormat: TCell): String; function WriteBorderStyleXMLAsString(const AFormat: TCell): String;
function WriteHorAlignmentStyleXMLAsString(const AFormat: TCell): String;
function WriteTextRotationStyleXMLAsString(const AFormat: TCell): String; function WriteTextRotationStyleXMLAsString(const AFormat: TCell): String;
function WriteVertAlignmentStyleXMLAsString(const AFormat: TCell): String;
function WriteWordwrapStyleXMLAsString(const AFormat: TCell): String; function WriteWordwrapStyleXMLAsString(const AFormat: TCell): String;
protected protected
FPointSeparatorSettings: TFormatSettings; FPointSeparatorSettings: TFormatSettings;
@@ -1355,16 +1357,30 @@ begin
' <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>' + LineEnding; ' <style:text-properties fo:font-weight="bold" style:font-weight-asian="bold" style:font-weight-complex="bold"/>' + LineEnding;
// style:table-cell-properties // style:table-cell-properties
if (FFormattingStyles[i].UsedFormattingFields <> []) then begin if (FFormattingStyles[i].UsedFormattingFields *
[uffBorder, uffBackgroundColor, uffWordWrap, uffTextRotation, uffVertAlign] <> [])
then begin
Result := Result + Result := Result +
' <style:table-cell-properties ' + ' <style:table-cell-properties ' +
WriteBorderStyleXMLAsString(FFormattingStyles[i]) + WriteBorderStyleXMLAsString(FFormattingStyles[i]) +
WriteBackgroundColorStyleXMLAsString(FFormattingStyles[i]) + WriteBackgroundColorStyleXMLAsString(FFormattingStyles[i]) +
WriteWordwrapStyleXMLAsString(FFormattingStyles[i]) + WriteWordwrapStyleXMLAsString(FFormattingStyles[i]) +
WriteTextRotationStyleXMLAsString(FFormattingStyles[i]) + WriteTextRotationStyleXMLAsString(FFormattingStyles[i]) +
WriteVertAlignmentStyleXMLAsString(FFormattingStyles[i]) +
'/>' + LineEnding; '/>' + LineEnding;
end; end;
// style:paragraph-properties
if (uffHorAlign in FFormattingStyles[i].UsedFormattingFields) and
(FFormattingStyles[i].HorAlignment <> haDefault)
then begin
Result := Result +
' <style:paragraph-properties ' +
WriteHorAlignmentStyleXMLAsString(FFormattingStyles[i]) +
'/>' + LineEnding;
end;
// End // End
Result := Result + Result := Result +
' </style:style>' + LineEnding; ' </style:style>' + LineEnding;
@@ -1560,6 +1576,22 @@ begin
Result := Result + 'fo:border-top="none" '; Result := Result + 'fo:border-top="none" ';
end; end;
{ Creates an XML string for inclusion of the horizontal alignment into the
written file from the horizontal alignment setting in the format cell.
Is called from WriteStyles (via WriteStylesXMLAsString). }
function TsSpreadOpenDocWriter.WriteHorAlignmentStyleXMLAsString(
const AFormat: TCell): String;
begin
Result := '';
if not (uffHorAlign in AFormat.UsedFormattingFields) then
exit;
case AFormat.HorAlignment of
haLeft : Result := 'fo:text-align="start" ';
haCenter : Result := 'fo:text-align="center" ';
haRight : Result := 'fo:text-align="end" ';
end;
end;
{ Creates an XML string for inclusion of the textrotation style option into the { Creates an XML string for inclusion of the textrotation style option into the
written file from the textrotation setting in the format cell. written file from the textrotation setting in the format cell.
Is called from WriteStyles (via WriteStylesXMLAsString). } Is called from WriteStyles (via WriteStylesXMLAsString). }
@@ -1577,10 +1609,27 @@ begin
end; end;
end; end;
{ Creates an XML string for inclusion of the vertical alignment into the
written file from the vertical alignment setting in the format cell.
Is called from WriteStyles (via WriteStylesXMLAsString). }
function TsSpreadOpenDocWriter.WriteVertAlignmentStyleXMLAsString(
const AFormat: TCell): String;
begin
Result := '';
if not (uffVertAlign in AFormat.UsedFormattingFields) then
exit;
case AFormat.VertAlignment of
vaTop : Result := 'style:vertical-align="top" ';
vaCenter : Result := 'style:vertical-align="middle" ';
vaBottom : Result := 'style:vertical-align="bottom" ';
end;
end;
{ Creates an XML string for inclusion of the wordwrap option into the { Creates an XML string for inclusion of the wordwrap option into the
written file from the wordwrap setting in the format cell. written file from the wordwrap setting in the format cell.
Is called from WriteStyles (via WriteStylesXMLAsString). } Is called from WriteStyles (via WriteStylesXMLAsString). }
function TsSpreadOpenDocWriter.WriteWordwrapStyleXMLAsString(const AFormat: TCell): String; function TsSpreadOpenDocWriter.WriteWordwrapStyleXMLAsString(
const AFormat: TCell): String;
begin begin
if (uffWordWrap in AFormat.UsedFormattingFields) then if (uffWordWrap in AFormat.UsedFormattingFields) then
Result := 'fo:wrap-option="wrap" ' Result := 'fo:wrap-option="wrap" '

View File

@@ -104,6 +104,7 @@ type
procedure TestWriteRead_BIFF8_WordWrap; procedure TestWriteRead_BIFF8_WordWrap;
{ ODS Tests } { ODS Tests }
procedure TestWriteRead_ODS_Alignment;
procedure TestWriteRead_ODS_Border; procedure TestWriteRead_ODS_Border;
procedure TestWriteRead_ODS_BorderStyles; procedure TestWriteRead_ODS_BorderStyles;
procedure TestWriteRead_ODS_TextRotation; procedure TestWriteRead_ODS_TextRotation;
@@ -425,12 +426,13 @@ begin
for horAlign in TsHorAlignment do begin for horAlign in TsHorAlignment do begin
col := 0; col := 0;
if AFormat = sfExcel2 then begin if AFormat = sfExcel2 then begin
// BIFF2 can only do horizontal alignment --> no need for vertical alignment.
MyWorksheet.WriteUTF8Text(row, col, CELLTEXT); MyWorksheet.WriteUTF8Text(row, col, CELLTEXT);
MyWorksheet.WriteHorAlignment(row, col, horAlign); MyWorksheet.WriteHorAlignment(row, col, horAlign);
MyCell := MyWorksheet.FindCell(row, col); MyCell := MyWorksheet.FindCell(row, col);
if MyCell = nil then if MyCell = nil then
fail('Error in test code. Failed to get cell.'); fail('Error in test code. Failed to get cell.');
CheckEquals(horAlign = MyCell^.HorAlignment, true, CheckEquals(ord(horAlign), ord(MyCell^.HorAlignment),
'Test unsaved horizontal alignment, cell ' + CellNotation(MyWorksheet,0,0)); 'Test unsaved horizontal alignment, cell ' + CellNotation(MyWorksheet,0,0));
end else end else
for vertAlign in TsVertAlignment do begin for vertAlign in TsVertAlignment do begin
@@ -440,9 +442,9 @@ begin
MyCell := MyWorksheet.FindCell(row, col); MyCell := MyWorksheet.FindCell(row, col);
if MyCell = nil then if MyCell = nil then
fail('Error in test code. Failed to get cell.'); fail('Error in test code. Failed to get cell.');
CheckEquals(true, vertAlign = MyCell^.VertAlignment, CheckEquals(ord(vertAlign),ord(MyCell^.VertAlignment),
'Test unsaved vertical alignment, cell ' + CellNotation(MyWorksheet,0,0)); 'Test unsaved vertical alignment, cell ' + CellNotation(MyWorksheet,0,0));
CheckEquals(true, horAlign = MyCell^.HorAlignment, CheckEquals(ord(horAlign), ord(MyCell^.HorAlignment),
'Test unsaved horizontal alignment, cell ' + CellNotation(MyWorksheet,0,0)); 'Test unsaved horizontal alignment, cell ' + CellNotation(MyWorksheet,0,0));
inc(col); inc(col);
end; end;
@@ -454,33 +456,39 @@ begin
// Open the spreadsheet // Open the spreadsheet
MyWorkbook := TsWorkbook.Create; MyWorkbook := TsWorkbook.Create;
MyWorkbook.ReadFromFile(TempFile, AFormat); MyWorkbook.ReadFromFile(TempFile, AFormat);
if AFormat = sfExcel2 then if AFormat = sfExcel2 then begin
MyWorksheet := MyWorkbook.GetFirstWorksheet MyWorksheet := MyWorkbook.GetFirstWorksheet;
else if MyWorksheet=nil then
MyWorksheet := GetWorksheetByName(MyWorkBook, AlignmentSheet); fail('Error in test code. Failed to get named worksheet');
if MyWorksheet=nil then for row :=0 to MyWorksheet.GetLastRowIndex do begin
fail('Error in test code. Failed to get named worksheet');
for row := 0 to MyWorksheet.GetLastRowIndex do
if AFormat = sfExcel2 then begin
MyCell := MyWorksheet.FindCell(row, col); MyCell := MyWorksheet.FindCell(row, col);
if MyCell = nil then if MyCell = nil then
fail('Error in test code. Failded to get cell.'); fail('Error in test code. Failed to get cell.');
horAlign := TsHorAlignment(row); horAlign := TsHorAlignment(row);
CheckEquals(horAlign = MyCell^.HorAlignment, true, CheckEquals(ord(horAlign), ord(MyCell^.HorAlignment),
'Test saved horizontal alignment mismatch, cell '+CellNotation(MyWorksheet,row,col)); 'Test save horizontal alignment mismatch, cell '+CellNotation(MyWorksheet,row,col));
end else end
for col := 0 to MyWorksheet.GetLastColIndex do begin end
else begin
MyWorksheet := GetWorksheetByName(MyWorkBook, AlignmentSheet);
if MyWorksheet=nil then
fail('Error in test code. Failed to get named worksheet');
for row :=0 to MyWorksheet.GetLastRowIndex do
for col := 0 to MyWorksheet.GetlastColIndex do begin
MyCell := MyWorksheet.FindCell(row, col); MyCell := MyWorksheet.FindCell(row, col);
if MyCell = nil then if MyCell = nil then
fail('Error in test code. Failed to get cell.'); fail('Error in test code. Failed to get cell.');
vertAlign := TsVertAlignment(col); vertAlign := TsVertAlignment(col);
if vertAlign = vaDefault then vertAlign := vaBottom; if (vertAlign = vaDefault) and (AFormat <> sfOpenDocument) then
CheckEquals(true, vertAlign = MyCell^.VertAlignment, vertAlign := vaBottom;
'Test saved vertical alignment mismatch, cell '+CellNotation(MyWorksheet,Row,Col)); CheckEquals(ord(vertAlign), ord(MyCell^.VertAlignment),
'Test saved vertical alignment mismatch, cell '+CellNotation(MyWorksheet,row,col));
horAlign := TsHorAlignment(row); horAlign := TsHorAlignment(row);
CheckEquals(true, horAlign = MyCell^.HorAlignment, CheckEquals(ord(horAlign), ord(MyCell^.HorAlignment),
'Test saved horizontal alignment mismatch, cell '+CellNotation(MyWorksheet,Row,Col)); 'Test saved horizontal alignment mismatch, cell '+CellNotation(MyWorksheet,row,col));
end; end;
end;
MyWorkbook.Free; MyWorkbook.Free;
DeleteFile(TempFile); DeleteFile(TempFile);
@@ -501,6 +509,11 @@ begin
TestWriteReadAlignment(sfExcel8); TestWriteReadAlignment(sfExcel8);
end; end;
procedure TSpreadWriteReadFormatTests.TestWriteRead_ODS_Alignment;
begin
TestWriteReadAlignment(sfOpenDocument);
end;
{ --- Border on/off tests --- } { --- Border on/off tests --- }