diff options
| author | Noel Peden <[email protected]> | 2022-02-07 08:30:19 -0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2022-02-07 08:30:19 -0800 |
| commit | 1def0a01e9af324ecf202e6542fa77920dc11662 (patch) | |
| tree | 1f8367b7f7bf29bfacc2ae593d3e9870fcaaafd7 | |
| parent | be9b3e6ee9dca0e0377ae8f3b14d007ad494b7e6 (diff) | |
| parent | c027feff583a09d5d6803e666bdbfca87653329d (diff) | |
| download | caxlsx-1def0a01e9af324ecf202e6542fa77920dc11662.tar.gz caxlsx-1def0a01e9af324ecf202e6542fa77920dc11662.zip | |
Merge pull request #116 from caxlsx/empty-sheet-name
Validate name option to be non-empty string when passed.
| -rw-r--r-- | CHANGELOG.md | 1 | ||||
| -rw-r--r-- | lib/axlsx/util/constants.rb | 3 | ||||
| -rw-r--r-- | lib/axlsx/workbook/worksheet/worksheet.rb | 1 | ||||
| -rw-r--r-- | test/workbook/tc_workbook.rb | 5 |
4 files changed, 10 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 55366dd2..bedd7ba2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ CHANGELOG - **Unreleased** - [PR #75](https://github.com/caxlsx/caxlsx/pull/85) - Added manageable markers for scatter series + - [PR #116](https://github.com/caxlsx/caxlsx/pull/116) - Validate name option to be non-empty string when passed. - [PR #117](https://github.com/caxlsx/caxlsx/pull/117) - Allow passing an Array of border hashes to the `border` style. Change previous behaviour where `border_top`, `border_*` styles would not be applied unless `border` style was also defined. - [PR #122](https://github.com/caxlsx/caxlsx/pull/122) - Improve error messages when incorrect ranges are provided to `Worksheet#[]` - [PR #123](https://github.com/caxlsx/caxlsx/pull/123) - Fix invalid xml when pivot table created with more than one column in data field. Solves [Issue #110](https://github.com/caxlsx/caxlsx/issues/110) diff --git a/lib/axlsx/util/constants.rb b/lib/axlsx/util/constants.rb index 3d67088c..76db47bb 100644 --- a/lib/axlsx/util/constants.rb +++ b/lib/axlsx/util/constants.rb @@ -286,6 +286,9 @@ module Axlsx # error message for RangeValidator ERR_RANGE = "Invalid Data. %s must be between %s and %s, (inclusive:%s) you gave: %s".freeze + # error message for sheets that use explicit empty string name + 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 diff --git a/lib/axlsx/workbook/worksheet/worksheet.rb b/lib/axlsx/workbook/worksheet/worksheet.rb index adb9839f..0942daa2 100644 --- a/lib/axlsx/workbook/worksheet/worksheet.rb +++ b/lib/axlsx/workbook/worksheet/worksheet.rb @@ -682,6 +682,7 @@ module Axlsx def validate_sheet_name(name) DataTypeValidator.validate :worksheet_name, String, name # 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_CHARACTER_FORBIDDEN % name) if '[]*/\?:'.chars.any? { |char| name.include? char } diff --git a/test/workbook/tc_workbook.rb b/test/workbook/tc_workbook.rb index c2f8ebd6..f8d0b1f7 100644 --- a/test/workbook/tc_workbook.rb +++ b/test/workbook/tc_workbook.rb @@ -46,6 +46,11 @@ class TestWorkbook < Test::Unit::TestCase assert_equal('foo', @wb.sheet_by_name('foo').name) end + + def test_worksheet_empty_name + assert_raise(ArgumentError) {@wb.add_worksheet(:name=>'')} + end + def test_date1904 assert_equal(Axlsx::Workbook.date1904, @wb.date1904) @wb.date1904 = :false |
