diff options
| author | Geremia Taglialatela <[email protected]> | 2022-09-21 00:34:43 +0200 |
|---|---|---|
| committer | Stefan Daschek <[email protected]> | 2022-09-21 01:25:53 +0200 |
| commit | abd64701cee4f5326c18241c24f872fb96268094 (patch) | |
| tree | b84a5b23962513ce9832ff7b249529d8d6e62765 /lib | |
| parent | 20c7572361fa9b0ac79cc9f44ae0626aa8565cd8 (diff) | |
| download | caxlsx-abd64701cee4f5326c18241c24f872fb96268094.tar.gz caxlsx-abd64701cee4f5326c18241c24f872fb96268094.zip | |
Add worksheet name max length to a constant
Close #163
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/axlsx/util/constants.rb | 5 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 5 |
2 files changed, 7 insertions, 3 deletions
diff --git a/lib/axlsx/util/constants.rb b/lib/axlsx/util/constants.rb index 76db47bb..3f30216f 100644 --- a/lib/axlsx/util/constants.rb +++ b/lib/axlsx/util/constants.rb @@ -274,6 +274,9 @@ module Axlsx # cellXfs id for default date styling STYLE_DATE = 2 + # worksheet maximum name length + WORKSHEET_MAX_NAME_LENGTH = 31 + # error messages RestrictionValidor ERR_RESTRICTION = "Invalid Data: %s. %s must be one of %s.".freeze @@ -290,7 +293,7 @@ module Axlsx ERR_SHEET_NAME_EMPTY = "Your worksheet name is empty. Worksheet name can't be empty. Please assign name of your sheet or don't use name option at all.".freeze # error message for sheets that use a name which is longer than 31 bytes - ERR_SHEET_NAME_TOO_LONG = "Your worksheet name '%s' is too long. Worksheet names must be 31 characters (bytes) or less".freeze + ERR_SHEET_NAME_TOO_LONG = "Your worksheet name '%s' is too long. Worksheet names must be #{WORKSHEET_MAX_NAME_LENGTH} characters (bytes) or less".freeze # error message for sheets that use a name which include invalid characters ERR_SHEET_NAME_CHARACTER_FORBIDDEN = "Your worksheet name '%s' contains a character which is not allowed by MS Excel and will cause repair warnings. Please change the name of your sheet.".freeze diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index d782fefe..b51e2c00 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -303,7 +303,8 @@ module Axlsx end # The name of the worksheet - # The name of a worksheet must be unique in the workbook, and must not exceed 31 characters + # The name of a worksheet must be unique in the workbook, and must not exceed the number + # of characters defined in Axlsx::WORKSHEET_MAX_NAME_LENGTH # @param [String] name def name=(name) validate_sheet_name name @@ -684,7 +685,7 @@ module Axlsx # ignore first character (BOM) after encoding to utf16 because Excel does so, too. raise ArgumentError, (ERR_SHEET_NAME_EMPTY) if name.empty? character_length = name.encode("utf-16")[1..-1].encode("utf-16").bytesize / 2 - raise ArgumentError, (ERR_SHEET_NAME_TOO_LONG % name) if character_length > 31 + raise ArgumentError, (ERR_SHEET_NAME_TOO_LONG % name) if character_length > WORKSHEET_MAX_NAME_LENGTH raise ArgumentError, (ERR_SHEET_NAME_CHARACTER_FORBIDDEN % name) if '[]*/\?:'.chars.any? { |char| name.include? char } name = Axlsx::coder.encode(name) sheet_names = @workbook.worksheets.reject { |s| s == self }.map { |s| s.name } |
