Skip to content

Commit

Permalink
Add raising exceptions to Worksheet#add_cell with too long cell conte…
Browse files Browse the repository at this point in the history
…nts.
  • Loading branch information
takahiro-blab authored and weshatheleopard committed Apr 11, 2024
1 parent c9400de commit 38c924c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rubyXL/worksheet.rb
@@ -1,5 +1,7 @@
module RubyXL
module LegacyWorksheet
TEXT_LENGTH_LIMIT_IN_CELL = 32767 # 2 ** 15 - 1

include Enumerable

def initialize(params = {})
Expand Down Expand Up @@ -50,9 +52,15 @@ def add_cell(row_index = 0, column_index = 0, data = '', formula = nil, overwrit
case data
when Numeric then c.raw_value = data
when String then
if TEXT_LENGTH_LIMIT_IN_CELL < data.length
raise ArgumentError, "The maximum length of cell contents (text) is #{TEXT_LENGTH_LIMIT_IN_CELL} characters"
end
c.raw_value = data
c.datatype = RubyXL::DataType::RAW_STRING
when RubyXL::RichText then
if TEXT_LENGTH_LIMIT_IN_CELL < data.to_s.length
raise ArgumentError, "The maximum length of cell contents (text) is #{TEXT_LENGTH_LIMIT_IN_CELL} characters"
end
c.is = data
c.datatype = RubyXL::DataType::INLINE_STRING
when Time, Date, DateTime then
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/cell_spec.rb
Expand Up @@ -66,6 +66,30 @@
# We expect exactly the same date
expect(cell.value.to_time.utc.iso8601).to eq(expected_datetime.iso8601)
end

it 'should raise against too long String' do
ok_data = 'A' * 32767 # The limit is 32767

expect {
@worksheet.add_cell(0,1, ok_data) # 32767 -> OK
}.not_to raise_error
expect {
# 1 longer than the limit, so an exception must be thrown.
@worksheet.add_cell(0,2, ok_data + 'B')
}.to raise_error(ArgumentError)
end

it 'should raise against too long RichText' do
ok_data = 'A' * 32767 # The limit is 32767

expect {
@worksheet.add_cell(0,1, RubyXL::RichText.new(:t => RubyXL::Text.new(:value => ok_data))) # 32767 -> OK
}.not_to raise_error
expect {
# 1 longer than the limit, so an exception must be thrown.
@worksheet.add_cell(0,2, RubyXL::RichText.new(:t => RubyXL::Text.new(:value => ok_data + 'B')))
}.to raise_error(ArgumentError)
end
end

describe '.change_fill' do
Expand Down

0 comments on commit 38c924c

Please sign in to comment.