How should I obtain a reference to an already existing .xlsx workbook when running my script?
I'm using Ruby gem Axlsx and here is my code:
require 'axlsx'
p = Axlsx::Package.new
wb_file = File.open("simple.xlsx", "a+")
wb = p.workbook(wb_file) # STICKING POINT: can't get a handle on an already existing workbook
# wb = p.workbook("simple.xls")
# worksheet = Worksheet.new(self, options)
sheet = p.workbook.sheet_by_name("Basic Worksheet")
sheet.add_row ["11", "22", "33"]
p.serialize('simple2.xlsx')
I'm getting this exception:
ArgumentError: wrong number of arguments (1 for 0)
from /home/mindaugas/.rbenv/versions/2.2.1/lib/ruby/gems/2.2.0/gems/axlsx-2.0.1/lib/axlsx/package.rb:65:in `workbook'
from (irb):9
from /home/mindaugas/.rbenv/versions/2.2.1/bin/irb:11:in `<main>'
It's because the method does not accept arguments.
def workbook
@workbook || @workbook = Workbook.new
yield @workbook if block_given?
@workbook
end
How could I obtain the the existing workbook in order to manipulate it?
Should I extend the gem (do some hacks)?
Maybe it is possible to somehow assign it to the @workbook
instance variable so it would not return a Workbook.new
.
It's not possible to open existing workbooks with the Axlsx library (source: https://github.com/randym/axlsx/issues/283).
There's another Ruby library that supports editing existing files: https://github.com/weshatheleopard/rubyXL.