From 8ed5356be209b363ffeeae599fa2a0f2b8f1e059 Mon Sep 17 00:00:00 2001 From: wp_xxyyzz Date: Fri, 20 May 2016 21:00:20 +0000 Subject: [PATCH] fpspreadsheet: Move check for sheet name length to writer -> Sheet names can be any length now. But saving to Excel will abort if there are more than 31 characters. git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@4656 8e941d3f-bd1b-0410-a28a-d453659cc2b4 --- components/fpspreadsheet/fpspreadsheet.pas | 4 +++- components/fpspreadsheet/fpsreaderwriter.pas | 17 ++++++++++++++++- components/fpspreadsheet/fpsstrings.pas | 2 ++ components/fpspreadsheet/fpstypes.pas | 1 + .../fpspreadsheet/languages/fpsstrings.de.po | 4 ++++ .../fpspreadsheet/languages/fpsstrings.fi.po | 4 ++++ .../fpspreadsheet/languages/fpsstrings.hu.po | 4 ++++ .../fpspreadsheet/languages/fpsstrings.po | 4 ++++ .../fpspreadsheet/languages/fpsstrings.ru.po | 4 ++++ components/fpspreadsheet/xlscommon.pas | 1 + components/fpspreadsheet/xlsxooxml.pas | 1 + 11 files changed, 44 insertions(+), 2 deletions(-) diff --git a/components/fpspreadsheet/fpspreadsheet.pas b/components/fpspreadsheet/fpspreadsheet.pas index 39c657275..c6f2f3682 100755 --- a/components/fpspreadsheet/fpspreadsheet.pas +++ b/components/fpspreadsheet/fpspreadsheet.pas @@ -7861,10 +7861,12 @@ begin if (AName = '') then exit; + { wp: the length restriction has been moved to the writer... + // Length must be less than 31 characters if UTF8Length(AName) > 31 then exit; - + } // Name must not contain any of the INVALID_CHARS for i:=0 to High(INVALID_CHARS) do if UTF8Pos(INVALID_CHARS[i], AName) > 0 then diff --git a/components/fpspreadsheet/fpsreaderwriter.pas b/components/fpspreadsheet/fpsreaderwriter.pas index 8495d1935..c3ed02dcf 100644 --- a/components/fpspreadsheet/fpsreaderwriter.pas +++ b/components/fpspreadsheet/fpsreaderwriter.pas @@ -184,7 +184,7 @@ type implementation uses - Math, + Math, LazUTF8, fpsStrings, fpsUtils, fpsNumFormat, fpsStreams, fpsRegFileFormats; @@ -208,6 +208,7 @@ begin FLimitations.MaxColCount := 256; FLimitations.MaxRowCount := 65536; FLimitations.MaxPaletteSize := MaxInt; + FLimitations.MaxSheetnameLength := MaxInt; end; {@@ ---------------------------------------------------------------------------- @@ -230,6 +231,8 @@ end; procedure TsBasicSpreadWriter.CheckLimitations; var lastCol, lastRow: Cardinal; + i: Integer; + sheet: TsWorksheet; begin Workbook.GetLastRowColIndex(lastRow, lastCol); @@ -240,6 +243,18 @@ begin // Check column count if lastCol >= FLimitations.MaxColCount then Workbook.AddErrorMsg(rsMaxColsExceeded, [lastCol+1, FLimitations.MaxColCount]); + + // Check worksheet names + for i:=0 to Workbook.GetWorksheetCount-1 do + begin + sheet := Workbook.GetWorksheetByIndex(i); + if UTF8Length(sheet.Name) > FLimitations.MaxSheetNameLength then + // Worksheet name is too long. + // We abort saving here because it is not safe to chop the sheet name + // to its allowed length - it may be used as a reference in formulas. + raise Exception.CreateFmt(rsWriteError_WorksheetNameTooLong, + [sheet.Name, FLimitations.MaxSheetNameLength]); + end; end; diff --git a/components/fpspreadsheet/fpsstrings.pas b/components/fpspreadsheet/fpsstrings.pas index 55a2b355d..4fedbd7aa 100644 --- a/components/fpspreadsheet/fpsstrings.pas +++ b/components/fpspreadsheet/fpsstrings.pas @@ -37,6 +37,8 @@ resourcestring 'the best-matching palette colors.'; rsTruncateTooLongCellText = 'Text value exceeds the %d character limit in ' + 'cell %s and has been truncated.'; + rsWriteError_WorksheetNameTooLong = 'File cannot be written because ' + + 'the name of worksheet "%0:s" is too long (max %1:d characters).'; // Cells rsInvalidCharacterInCell = 'Invalid character(s) in cell %s.'; diff --git a/components/fpspreadsheet/fpstypes.pas b/components/fpspreadsheet/fpstypes.pas index 2512788ab..3d9a31f24 100644 --- a/components/fpspreadsheet/fpstypes.pas +++ b/components/fpspreadsheet/fpstypes.pas @@ -44,6 +44,7 @@ type MaxRowCount: Cardinal; MaxColCount: Cardinal; MaxPaletteSize: Integer; + MaxSheetNameLength: Integer; end; const diff --git a/components/fpspreadsheet/languages/fpsstrings.de.po b/components/fpspreadsheet/languages/fpsstrings.de.po index 33a0b3ebf..48120d448 100644 --- a/components/fpspreadsheet/languages/fpsstrings.de.po +++ b/components/fpspreadsheet/languages/fpsstrings.de.po @@ -412,6 +412,10 @@ msgstr "Arbeitsblatt \"%s\" nicht gefunden" msgid "Worksheet not found." msgstr "Arbeitsblatt nicht gefunden." +#: fpsstrings.rswriteerror_worksheetnametoolong +msgid "File cannot be written because the name of worksheet \"%0:s\" is too long (max %1:d characters)." +msgstr "Die Datei kann nicht geschrieben werden, weil der Name des Arbeitsblattes \"%0s:s\" zu lang ist (höchstens %1:d Zeichen)." + #: fpsstrings.rsyellow msgid "yellow" msgstr "gelb" diff --git a/components/fpspreadsheet/languages/fpsstrings.fi.po b/components/fpspreadsheet/languages/fpsstrings.fi.po index 25704af00..91e4f31cb 100644 --- a/components/fpspreadsheet/languages/fpsstrings.fi.po +++ b/components/fpspreadsheet/languages/fpsstrings.fi.po @@ -401,6 +401,10 @@ msgstr "" msgid "Worksheet not found." msgstr "" +#: fpsstrings.rswriteerror_worksheetnametoolong +msgid "File cannot be written because the name of worksheet \"%0:s\" is too long (max %1:d characters)." +msgstr "" + #: fpsstrings.rsyellow msgid "yellow" msgstr "keltainen" diff --git a/components/fpspreadsheet/languages/fpsstrings.hu.po b/components/fpspreadsheet/languages/fpsstrings.hu.po index 29c5c8d71..aeaa42672 100644 --- a/components/fpspreadsheet/languages/fpsstrings.hu.po +++ b/components/fpspreadsheet/languages/fpsstrings.hu.po @@ -411,6 +411,10 @@ msgstr "A(z) \"%s\" munkalap nem található." msgid "Worksheet not found." msgstr "A munkalap nem található." +#: fpsstrings.rswriteerror_worksheetnametoolong +msgid "File cannot be written because the name of worksheet \"%0:s\" is too long (max %1:d characters)." +msgstr "" + #: fpsstrings.rsyellow msgid "yellow" msgstr "sárga" diff --git a/components/fpspreadsheet/languages/fpsstrings.po b/components/fpspreadsheet/languages/fpsstrings.po index ad99bafe8..291755da3 100644 --- a/components/fpspreadsheet/languages/fpsstrings.po +++ b/components/fpspreadsheet/languages/fpsstrings.po @@ -401,6 +401,10 @@ msgstr "" msgid "Worksheet not found." msgstr "" +#: fpsstrings.rswriteerror_worksheetnametoolong +msgid "File cannot be written because the name of worksheet \"%0:s\" is too long (max %1:d characters)." +msgstr "" + #: fpsstrings.rsyellow msgid "yellow" msgstr "" diff --git a/components/fpspreadsheet/languages/fpsstrings.ru.po b/components/fpspreadsheet/languages/fpsstrings.ru.po index 4dc47206e..400b746f8 100644 --- a/components/fpspreadsheet/languages/fpsstrings.ru.po +++ b/components/fpspreadsheet/languages/fpsstrings.ru.po @@ -401,6 +401,10 @@ msgstr "Лист таблицы \"%s\" не найден." msgid "Worksheet not found." msgstr "Лист таблицы не найден" +#: fpsstrings.rswriteerror_worksheetnametoolong +msgid "File cannot be written because the name of worksheet \"%0:s\" is too long (max %1:d characters)." +msgstr "" + #: fpsstrings.rsyellow msgid "yellow" msgstr "жёлтый" diff --git a/components/fpspreadsheet/xlscommon.pas b/components/fpspreadsheet/xlscommon.pas index 19bb8f0da..0add2e920 100644 --- a/components/fpspreadsheet/xlscommon.pas +++ b/components/fpspreadsheet/xlscommon.pas @@ -2853,6 +2853,7 @@ begin FLimitations.MaxColCount := 256; FLimitations.MaxRowCount := 65536; FLimitations.MaxPaletteSize := 64; + FLimitations.MaxSheetNameLength := 31; // Initial base date in case it won't be set otherwise. // Use 1900 to get a bit more range between 1900..1904. diff --git a/components/fpspreadsheet/xlsxooxml.pas b/components/fpspreadsheet/xlsxooxml.pas index 5c0f267b0..28b2f6beb 100755 --- a/components/fpspreadsheet/xlsxooxml.pas +++ b/components/fpspreadsheet/xlsxooxml.pas @@ -2220,6 +2220,7 @@ begin // http://en.wikipedia.org/wiki/List_of_spreadsheet_software#Specifications FLimitations.MaxColCount := 16384; FLimitations.MaxRowCount := 1048576; + FLimitations.MaxSheetNameLength := 31; end;