Using Column headers to parse excel sheets using roo - Ruby
Asked Answered
B

4

7

Can we use column headers to specify the column number from which we are parsing the excel sheet using roo gem? My current code is like this now:

oo = Openoffice.new("simple_spreadsheet.ods")
oo.default_sheet = oo.sheets.first
(2..oo.last_row).each do |line|
  date       = oo.cell(line,'A')
  start_time = oo.cell(line,'B')
  end_time   = oo.cell(line,'C')
  pause      = oo.cell(line,'D')
  ...
end

I would like to parse from column headers instead of specifying columns as 'A' 'B' 'C' ... Can I acheive this using Roo?

Bissell answered 20/7, 2011 at 17:44 Comment(3)
as I know only digits or alphabet can be used to access a cell.Gwenette
Why does the roo website hate my eyes so much?Rhonarhonchus
@mu is too short..yeah..u r right..marvellous colors they got there :PBissell
I
3

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.

Increasing answered 22/7, 2011 at 15:11 Comment(2)
could you tell me how to write to xlsx and xls file using rooChery
You can't, at the moment roo only supports read for Excel. You can use axlsx gem to do writing. I know - it's not ideal, but so far I cant' find a gem that does both things well.Voiceful
B
3

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

Backbreaking answered 13/4, 2012 at 19:37 Comment(0)
A
2

This will use the header row as the keys. The helpful parts are transpose and strip.

def self.excel_to_hash(folder_name, file_name, tab_name)
    # Takes an excel file name and a tab name, and returns an array of stripped, transposed rows
    # Sample call:  my_data = excel_to_hash File.join(Rails.root,'db/data/data_to_import.xlsx'), 'models'
    rows = []
    file = File.open(File.join(folder_name, file_name), mode = 'r')
    excel = Excelx.new(file.path, nil, :ignore)
    excel.default_sheet = excel.sheets.index(tab_name) + 1
    header = excel.row(1)
    (2..excel.last_row).each do |i|
      next unless excel.row(i)[0]
      row = Hash[[header, excel.row(i)].transpose]      
      row.each_key{|x| row[x] = row[x].to_s.strip if row[x]}
      rows << row
    end
    return rows
  end

valid through Roo gem 1.10.2

Annalist answered 23/7, 2013 at 7:0 Comment(0)
C
0

This works for me

 require 'roo'
    # open excel file
    excel_file = Roo::Spreadsheet.open(file_path)
      # iterate on each sheet
      excel_file.each_with_pagename do |name, sheet|
        # iterate on each sheet
        sheet.parse(headers: true, pad_cells: true) do |row|
          # data should be access by column header if we have column header Name we can access like this
          row['Name']
        end
      end
    end
Convolve answered 18/10, 2022 at 13:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.