I am using the roo-gem in ruby to get excel sheet cell values.
I have a file 'ruby.rb' with:
require 'spreadsheet'
require 'roo'
xls = Roo::Spreadsheet.open('test_work.xls')
xls.each do |row|
p row
end
my output in the terminal when I run ruby 'ruby.rb' is:
["id", "header2", "header3", "header4"]
["val1", "val2", "val3", "val4"]
["val1", "val2", "val3", "val4"]
when I add:
require 'spreadsheet'
require 'roo'
xls = Roo::Spreadsheet.open('test_work.xls')
xls.each do |row|
two_dimensional = []
two_dimensional << row
p two_dimensional
end
I get:
[["id", "header2", "header3", "header4"]]
[["val1", "val2", "val3", "val4"]]
[["val1", "val2", "val3", "val4"]]
What I want is:
[["id", "header2", "header3", "header4"],
["val1", "val2", "val3", "val4"],
["val1", "val2", "val3", "val4"]]
How would I go about doing this.
Thanks!
Just declare the array outside the each
block. You're resetting it to []
every time the block is run. In that case, you will only append to one array.
two_dimensional = []
xls = Roo::Spreadsheet.open('test_work.xls')
xls.each do |row|
two_dimensional << row
p two_dimensional
end