Using Column headers to parse excel sheets using roo - Ruby

You can grab the entire header row as an array and hash the entire row key'd on the header row.

oo = Openoffice.new("simple_spreadsheet.ods") 
oo.default_sheet = oo.sheets.first 
header = oo.row(1) 
2.upto(oo.last_row) do |line|  
  row_data =  Hash[header.zip oo.row(line)]
  ...
end

You could also use row_data[line] to nest the hashes for later use.


A cleaner/clearer version of the above is

oo = Openoffice.new("simple_spreadsheet.ods") 
oo.default_sheet = file.sheets.first 
header = oo.first_row 
2.upto(oo.last_row) do |line|  
  row_data =  Hash[*header.zip(row).flatten]
  ...
end

the original took me a bit to understand because especially as i thought hash was a local variable named hash instead of the class Hash