From 4dd961b590cb039c76bfb029cc17fd5966752d33 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Wed, 28 May 2014 20:52:36 +0000 Subject: [PATCH] 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 --- .../examples/opendocdemo/opendocwrite.lpi | 4 +- components/fpspreadsheet/fpsopendocument.pas | 53 +++++++++++++++++- .../fpspreadsheet/tests/formattests.pas | 55 ++++++++++++------- 3 files changed, 87 insertions(+), 25 deletions(-) diff --git a/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpi b/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpi index fad273e30..616b29f08 100644 --- a/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpi +++ b/components/fpspreadsheet/examples/opendocdemo/opendocwrite.lpi @@ -1,4 +1,4 @@ - + @@ -44,7 +44,7 @@ - + diff --git a/components/fpspreadsheet/fpsopendocument.pas b/components/fpspreadsheet/fpsopendocument.pas index a8bd6873d..9d58f83ad 100755 --- a/components/fpspreadsheet/fpsopendocument.pas +++ b/components/fpspreadsheet/fpsopendocument.pas @@ -99,7 +99,9 @@ type private function WriteBackgroundColorStyleXMLAsString(const AFormat: TCell): String; function WriteBorderStyleXMLAsString(const AFormat: TCell): String; + function WriteHorAlignmentStyleXMLAsString(const AFormat: TCell): String; function WriteTextRotationStyleXMLAsString(const AFormat: TCell): String; + function WriteVertAlignmentStyleXMLAsString(const AFormat: TCell): String; function WriteWordwrapStyleXMLAsString(const AFormat: TCell): String; protected FPointSeparatorSettings: TFormatSettings; @@ -1355,16 +1357,30 @@ begin ' ' + LineEnding; // style:table-cell-properties - if (FFormattingStyles[i].UsedFormattingFields <> []) then begin + if (FFormattingStyles[i].UsedFormattingFields * + [uffBorder, uffBackgroundColor, uffWordWrap, uffTextRotation, uffVertAlign] <> []) + then begin Result := Result + ' ' + LineEnding; end; + // style:paragraph-properties + if (uffHorAlign in FFormattingStyles[i].UsedFormattingFields) and + (FFormattingStyles[i].HorAlignment <> haDefault) + then begin + Result := Result + + ' ' + LineEnding; + end; + + // End Result := Result + ' ' + LineEnding; @@ -1560,6 +1576,22 @@ begin Result := Result + 'fo:border-top="none" '; 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 written file from the textrotation setting in the format cell. Is called from WriteStyles (via WriteStylesXMLAsString). } @@ -1577,10 +1609,27 @@ begin 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 written file from the wordwrap setting in the format cell. Is called from WriteStyles (via WriteStylesXMLAsString). } -function TsSpreadOpenDocWriter.WriteWordwrapStyleXMLAsString(const AFormat: TCell): String; +function TsSpreadOpenDocWriter.WriteWordwrapStyleXMLAsString( + const AFormat: TCell): String; begin if (uffWordWrap in AFormat.UsedFormattingFields) then Result := 'fo:wrap-option="wrap" ' diff --git a/components/fpspreadsheet/tests/formattests.pas b/components/fpspreadsheet/tests/formattests.pas index 1dde2862c..fbcf03657 100644 --- a/components/fpspreadsheet/tests/formattests.pas +++ b/components/fpspreadsheet/tests/formattests.pas @@ -104,6 +104,7 @@ type procedure TestWriteRead_BIFF8_WordWrap; { ODS Tests } + procedure TestWriteRead_ODS_Alignment; procedure TestWriteRead_ODS_Border; procedure TestWriteRead_ODS_BorderStyles; procedure TestWriteRead_ODS_TextRotation; @@ -425,12 +426,13 @@ begin for horAlign in TsHorAlignment do begin col := 0; if AFormat = sfExcel2 then begin + // BIFF2 can only do horizontal alignment --> no need for vertical alignment. MyWorksheet.WriteUTF8Text(row, col, CELLTEXT); MyWorksheet.WriteHorAlignment(row, col, horAlign); MyCell := MyWorksheet.FindCell(row, col); if MyCell = nil then 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)); end else for vertAlign in TsVertAlignment do begin @@ -440,9 +442,9 @@ begin MyCell := MyWorksheet.FindCell(row, col); if MyCell = nil then 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)); - CheckEquals(true, horAlign = MyCell^.HorAlignment, + CheckEquals(ord(horAlign), ord(MyCell^.HorAlignment), 'Test unsaved horizontal alignment, cell ' + CellNotation(MyWorksheet,0,0)); inc(col); end; @@ -454,33 +456,39 @@ begin // Open the spreadsheet MyWorkbook := TsWorkbook.Create; MyWorkbook.ReadFromFile(TempFile, AFormat); - if AFormat = sfExcel2 then - MyWorksheet := MyWorkbook.GetFirstWorksheet - else - 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 - if AFormat = sfExcel2 then begin + if AFormat = sfExcel2 then begin + MyWorksheet := MyWorkbook.GetFirstWorksheet; + if MyWorksheet=nil then + fail('Error in test code. Failed to get named worksheet'); + for row :=0 to MyWorksheet.GetLastRowIndex do begin MyCell := MyWorksheet.FindCell(row, col); 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); - CheckEquals(horAlign = MyCell^.HorAlignment, true, - 'Test saved horizontal alignment mismatch, cell '+CellNotation(MyWorksheet,row,col)); - end else - for col := 0 to MyWorksheet.GetLastColIndex do begin + CheckEquals(ord(horAlign), ord(MyCell^.HorAlignment), + 'Test save horizontal alignment mismatch, cell '+CellNotation(MyWorksheet,row,col)); + end + 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); if MyCell = nil then fail('Error in test code. Failed to get cell.'); vertAlign := TsVertAlignment(col); - if vertAlign = vaDefault then vertAlign := vaBottom; - CheckEquals(true, vertAlign = MyCell^.VertAlignment, - 'Test saved vertical alignment mismatch, cell '+CellNotation(MyWorksheet,Row,Col)); + if (vertAlign = vaDefault) and (AFormat <> sfOpenDocument) then + vertAlign := vaBottom; + CheckEquals(ord(vertAlign), ord(MyCell^.VertAlignment), + 'Test saved vertical alignment mismatch, cell '+CellNotation(MyWorksheet,row,col)); horAlign := TsHorAlignment(row); - CheckEquals(true, horAlign = MyCell^.HorAlignment, - 'Test saved horizontal alignment mismatch, cell '+CellNotation(MyWorksheet,Row,Col)); + CheckEquals(ord(horAlign), ord(MyCell^.HorAlignment), + 'Test saved horizontal alignment mismatch, cell '+CellNotation(MyWorksheet,row,col)); end; + end; + MyWorkbook.Free; DeleteFile(TempFile); @@ -501,6 +509,11 @@ begin TestWriteReadAlignment(sfExcel8); end; +procedure TSpreadWriteReadFormatTests.TestWriteRead_ODS_Alignment; +begin + TestWriteReadAlignment(sfOpenDocument); +end; + { --- Border on/off tests --- }