Skip to content

Commit 88e1138

Browse files
datbthPaxa
authored andcommitted
Validate worksheet name using Libxlsxwriter
1 parent 8f0c1cc commit 88e1138

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

Gemfile.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ GEM
4242
ruby-progressbar
4343
nokogiri (1.13.3-x86_64-darwin)
4444
racc (~> 1.4)
45+
nokogiri (1.13.3-x86_64-linux)
46+
racc (~> 1.4)
4547
racc (1.6.0)
4648
rake (13.0.6)
4749
ruby-progressbar (1.11.0)
@@ -57,6 +59,7 @@ GEM
5759

5860
PLATFORMS
5961
x86_64-darwin-20
62+
x86_64-linux
6063

6164
DEPENDENCIES
6265
axlsx!

lib/fast_excel.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ def self.print_ffi_obj(value)
125125
end
126126

127127

128+
ERROR_ENUM = Libxlsxwriter.enum_type(:error)
128129
COLOR_ENUM = Libxlsxwriter.enum_type(:defined_colors)
129130
EXTRA_COLORS = {
130131
alice_blue: 0xF0F8FF,
@@ -366,10 +367,11 @@ def number_format(pattern)
366367

367368
def add_worksheet(sheetname = nil)
368369
if !sheetname.nil?
369-
if sheetname.length > Libxlsxwriter::SHEETNAME_MAX
370-
raise ArgumentError, "Worksheet name '#{sheetname}' exceeds Excel's limit of #{Libxlsxwriter::SHEETNAME_MAX} characters"
371-
elsif @sheet_names.include?(sheetname)
372-
raise ArgumentError, "Worksheet name '#{sheetname}' is already in use"
370+
error = validate_worksheet_name(sheetname)
371+
if error != :no_error
372+
error_code = ERROR_ENUM.find(error)
373+
error_str = error_code ? Libxlsxwriter.strerror(error_code) : ''
374+
raise ArgumentError, "Invalid worksheet name '#{sheetname}': (#{error_code} - #{error}) #{error_str}"
373375
end
374376
end
375377

lib/fast_excel/binding/workbook.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ def get_worksheet_by_name(name)
147147
# @param [String] sheetname
148148
# @return [Symbol from _enum_error_]
149149
def validate_worksheet_name(sheetname)
150-
Libxlsxwriter.workbook_validate_worksheet_name(self, sheetname)
150+
Libxlsxwriter.workbook_validate_sheet_name(self, sheetname)
151151
end
152152

153153
# @return [nil]
@@ -319,12 +319,12 @@ class Workbook < FFI::Struct
319319
# @scope class
320320
attach_function :workbook_get_worksheet_by_name, :workbook_get_worksheet_by_name, [Workbook, :string], Worksheet
321321

322-
# @method workbook_validate_worksheet_name(workbook, sheetname)
322+
# @method workbook_validate_sheet_name(workbook, sheetname)
323323
# @param [Workbook] workbook
324324
# @param [String] sheetname
325325
# @return [Symbol from _enum_error_]
326326
# @scope class
327-
attach_function :workbook_validate_worksheet_name, :workbook_validate_worksheet_name, [Workbook, :string], :error
327+
attach_function :workbook_validate_sheet_name, :workbook_validate_sheet_name, [Workbook, :string], :error
328328

329329
# @method workbook_free(workbook)
330330
# @param [Workbook] workbook

test/validations_test.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
end
1212

1313
assert_equal(ArgumentError, error.class)
14-
assert_equal("Worksheet name 'Payments Report' is already in use", error.message)
14+
assert_equal("Invalid worksheet name 'Payments Report': (16 - error_sheetname_already_used) Worksheet name is already in use.", error.message)
1515
end
1616

1717
it "should not raise error when worksheet name is null" do
@@ -33,7 +33,7 @@
3333
end
3434

3535
assert_equal(ArgumentError, error.class)
36-
assert_equal("Worksheet name 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345' exceeds Excel's limit of 31 characters", error.message)
36+
assert_equal("Invalid worksheet name 'ABCDEFGHIJKLMNOPQRSTUVWXYZ012345': (13 - error_sheetname_length_exceeded) Worksheet name exceeds Excel's limit of 31 characters.", error.message)
3737
end
3838

3939
it "should not raise error when the sheet name is at maximum length" do
@@ -44,4 +44,15 @@
4444

4545
assert_equal("ABCDEFGHIJKLMNOPQRSTUVWXYZ01234", worksheet[:name])
4646
end
47+
48+
it "should validate using Libxlsxwriter validation" do
49+
workbook = FastExcel.open(constant_memory: true)
50+
error = assert_raises do
51+
worksheet = workbook.add_worksheet('a?')
52+
worksheet.write_value(1, 1, 'a') # without the validation, this method will crash the process
53+
end
54+
55+
assert_equal(ArgumentError, error.class)
56+
assert_equal("Invalid worksheet name 'a?': (14 - error_invalid_sheetname_character) Worksheet name cannot contain invalid characters: '[ ] : * ? / \\'", error.message)
57+
end
4758
end

0 commit comments

Comments
 (0)