weshatheleopard / rubyXL

Ruby lib for reading/writing/modifying .xlsx and .xlsm files
MIT License
1.27k stars 253 forks source link

Prevent too long cell contents from breaking Excel book #455

Closed takahiro-blab closed 4 months ago

takahiro-blab commented 4 months ago

Microsoft Excel has a limit of 32767 characters for cell content. In the current version of RubyXL, writing content in a cell that exceeds the limit will simply generate a broken book. "broken" means that Excel says it found unreadable content.

Reproducing code:

require 'rubyXL'

bad_data = 'A' * 32768
workbook = RubyXL::Workbook.new
workbook[0].add_cell(0,0,bad_data)
workbook.write('broken_book.xlsx') # Broken book for MS Excel will be saved.

So I added raising exception to Worksheet#add_cell when it is passed too long contents. I also wrote a test related to this.

However, how Worksheet#add_cell behaves when too long content is passed may be discussed. For example: instead of throwing an exception, truncate the content beyond the limit and add it to the cell.