Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spreadsheet gem: can't create empty workbook

I'm using https://github.com/zdavatz/spreadsheet version 0.8.3

I've got code that iterates over a collection and creates a worksheet in the same workbook for each item. When the collection is empty, the code that executes ends up looking like:

io = StringIO.new
book = Spreadsheet::Workbook.new
book.write(io)

The last line raises:

TypeError: can't convert nil into Integer
    spreadsheet/excel/writer/workbook.rb:636:in `pack'
    spreadsheet/excel/writer/workbook.rb:636:in `write_window1'
    spreadsheet/excel/writer/workbook.rb:419:in `write_from_scratch'
    spreadsheet/excel/writer/workbook.rb:644:in `write_workbook'
    spreadsheet/writer.rb:12:in `write'
    spreadsheet/workbook.rb:124:in `write'

Changing my code to this fixes things:

io = StringIO.new
book = Spreadsheet::Workbook.new
book.create_worksheet if book.worksheets.empty?
book.write(io)

But I'd rather not have to include the call to create_worksheet. My questions are:

a) am I doing anything wrong in my code?

b) is this a bug in the gem or is the crash expected?

like image 208
Alex Ghiculescu Avatar asked Oct 21 '22 14:10

Alex Ghiculescu


1 Answers

I emailed the maintainer of the project and they indicated that this is expected.

like image 76
Alex Ghiculescu Avatar answered Oct 28 '22 00:10

Alex Ghiculescu