From 078274920d9b0c97d8e47071444109e443970140 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Thu, 18 May 2017 10:50:22 +0000 Subject: [PATCH] fpspreadsheet: Fill unused BIFF5 and BIFF8 palette entries by default colors to avoid Excel XP show an incomplete color dialog. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@5863 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- .../fpspreadsheet/source/common/xlsbiff2.pas | 11 ++++++++++ .../fpspreadsheet/source/common/xlsbiff5.pas | 14 +++++++++++++ .../fpspreadsheet/source/common/xlsbiff8.pas | 11 ++++++++-- .../fpspreadsheet/source/common/xlscommon.pas | 20 +++++++++---------- components/fpspreadsheet/tests/errortests.pas | 12 ++--------- 5 files changed, 45 insertions(+), 23 deletions(-) diff --git a/components/fpspreadsheet/source/common/xlsbiff2.pas b/components/fpspreadsheet/source/common/xlsbiff2.pas index 03fbc221f..71f24d5d1 100644 --- a/components/fpspreadsheet/source/common/xlsbiff2.pas +++ b/components/fpspreadsheet/source/common/xlsbiff2.pas @@ -111,6 +111,7 @@ type procedure AddBuiltinNumFormats; override; function FunctionSupported(AExcelCode: Integer; const AFuncName: String): Boolean; override; + procedure PopulatePalette(AWorkbook: TsWorkbook); override; procedure WriteBlank(AStream: TStream; const ARow, ACol: Cardinal; ACell: PCell); override; procedure WriteBool(AStream: TStream; const ARow, ACol: Cardinal; @@ -1308,6 +1309,16 @@ begin end; end; +procedure TsSpreadBIFF2Writer.PopulatePalette(AWorkbook: TsWorkbook); +begin + FPalette.Clear; + FPalette.AddBuiltinColors(false); + // The next instruction creates an error log entry if the workbook contains + // more colors than the default 8. This is because BIFF2 can only have a + // palette with 8 colors. + FPalette.CollectFromWorkbook(AWorkbook); +end; + (* {@@ ---------------------------------------------------------------------------- Builds up the list of number formats to be written to the biff2 file. diff --git a/components/fpspreadsheet/source/common/xlsbiff5.pas b/components/fpspreadsheet/source/common/xlsbiff5.pas index ae438212b..3a71a6bea 100644 --- a/components/fpspreadsheet/source/common/xlsbiff5.pas +++ b/components/fpspreadsheet/source/common/xlsbiff5.pas @@ -99,6 +99,7 @@ type protected function FunctionSupported(AExcelCode: Integer; const AFuncName: String): Boolean; override; procedure InternalWriteToStream(AStream: TStream); + procedure PopulatePalette(AWorkbook: TsWorkbook); override; { Record writing methods } procedure WriteBOF(AStream: TStream; ADataType: Word); function WriteBoundsheet(AStream: TStream; AWorkSheet: TsWorksheet): Int64; @@ -1229,6 +1230,19 @@ begin SetLength(sheetPos, 0); end; +procedure TsSpreadBIFF5Writer.PopulatePalette(AWorkbook: TsWorkbook); +var + i: Integer; +begin + FPalette.Clear; + FPalette.AddBuiltinColors(true); + FPalette.CollectFromWorkbook(AWorkbook); + // Fill up Excel colors of the standard palette to avoid empty color + // place holders in Excel's colordialog. + for i := 16 to High(PALETTE_BIFF5) do + FPalette.AddUniqueColor(PALETTE_BIFF5[i]); +end; + {@@ ---------------------------------------------------------------------------- Writes an Excel BIFF5 file to the disc diff --git a/components/fpspreadsheet/source/common/xlsbiff8.pas b/components/fpspreadsheet/source/common/xlsbiff8.pas index 34e6ffe38..d5aee35f3 100644 --- a/components/fpspreadsheet/source/common/xlsbiff8.pas +++ b/components/fpspreadsheet/source/common/xlsbiff8.pas @@ -136,7 +136,7 @@ type protected function GetPrintOptions: Word; override; procedure InternalWriteToStream(AStream: TStream); - procedure PopulatePalette; override; + procedure PopulatePalette(AWorkbook: TsWorkbook); override; { Record writing methods } procedure WriteBOF(AStream: TStream; ADataType: Word); @@ -2226,10 +2226,17 @@ begin SetLength(sheetPos, 0); end; -procedure TsSpreadBIFF8Writer.PopulatePalette; +procedure TsSpreadBIFF8Writer.PopulatePalette(AWorkbook: TsWorkbook); +var + i: Integer; begin FPalette.Clear; FPalette.AddBuiltinColors(true); + FPalette.CollectFromWorkbook(AWorkbook); + // Fill up Excel colors of the standard palette to avoid empty color + // place holders in Excel's colordialog. + for i := 16 to High(PALETTE_BIFF8) do + FPalette.AddUniqueColor(PALETTE_BIFF8[i]); end; {@@ ---------------------------------------------------------------------------- diff --git a/components/fpspreadsheet/source/common/xlscommon.pas b/components/fpspreadsheet/source/common/xlscommon.pas index a2c06af4a..3860d7464 100644 --- a/components/fpspreadsheet/source/common/xlscommon.pas +++ b/components/fpspreadsheet/source/common/xlscommon.pas @@ -534,7 +534,7 @@ type function GetLastColIndex(AWorksheet: TsWorksheet): Word; function GetPrintOptions: Word; virtual; function PaletteIndex(AColor: TsColor): Word; - procedure PopulatePalette; virtual; + procedure PopulatePalette(AWorkbook: TsWorkbook); virtual; // Helper function for writing the BIFF header procedure WriteBIFFHeader(AStream: TStream; ARecID, ARecSize: Word); @@ -3074,8 +3074,7 @@ begin // Color palette FPalette := TsPalette.Create; - PopulatePalette; - FPalette.CollectFromWorkbook(AWorkbook); + PopulatePalette(AWorkbook); end; destructor TsSpreadBIFFWriter.Destroy; @@ -3239,15 +3238,14 @@ begin Result := word(idx); end; -procedure TsSpreadBIFFWriter.PopulatePalette; +{@@ ---------------------------------------------------------------------------- + Populates the color palette of the BIFF writer. Must be overridden by + descendants. +-------------------------------------------------------------------------------} +procedure TsSpreadBIFFWriter.PopulatePalette(AWorkbook: TsWorkbook); begin - with FPalette do - begin - Clear; - AddBuiltinColors(false); // 0..7 - // Note: These colors cannot be edited by Excel. The format specific - // writer must duplicate these items (except for BIFF2). - end; + Unused(AWorkbook); + FPalette.Clear; end; {@@ ---------------------------------------------------------------------------- diff --git a/components/fpspreadsheet/tests/errortests.pas b/components/fpspreadsheet/tests/errortests.pas index 73e3407bf..e2520f13b 100644 --- a/components/fpspreadsheet/tests/errortests.pas +++ b/components/fpspreadsheet/tests/errortests.pas @@ -65,14 +65,6 @@ const (Format: sfOOXML; MaxRowCount: 1048576; MaxColCount: 16384; MaxCellLen: $FFFFFFFF), (Format: sfOpenDocument; MaxRowCount: 1048576; MaxColCount: 1024; MaxCellLen: $FFFFFFFF) ); - (* -type - TTestFormat = (sfExcel2, sfExcel5, sfExcel8, sfExcelXML, sfOOXML, sfOpenDocument); -const // XLS2 XLS5 XLS8 XLSXML OOXML ODS - MAX_ROW_COUNT: array[TTestFormat] of Cardinal = (65536, 65536, 65536, 65536, 1048576, 1048576); - MAX_COL_COUNT: array[TTestFormat] of Cardinal = ( 256, 256, 256, 256, 16384, 1024); - MAX_CELL_LEN : array[TTestFormat] of Cardinal = ( 255, 255, 32767, 32767,$FFFFFFFF,$FFFFFFFF); - *) var MyWorkbook: TsWorkbook; MyWorksheet: TsWorksheet; @@ -155,10 +147,10 @@ begin // Prepare a full palette palette := TsPalette.Create; try - // Create random palette of 65 unique entries - 1 too many for Excel5/8 + // Create random palette of 65 unique entries -> 1 too many for Excel5/8 // and a lot too many for BIFF2 palette.AddBuiltinColors; - for i:=8 to 65 do + for i:=palette.Count to 65 do begin repeat newColor := random(256) + random(256) shl 8 + random(256) shl 16;