Skip to content

Instantly share code, notes, and snippets.

@evizitei
Created March 13, 2010 04:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save evizitei/331103 to your computer and use it in GitHub Desktop.
Save evizitei/331103 to your computer and use it in GitHub Desktop.
require 'roo'
require 'csv'
class SpreadsheetParser
def self.parse(file)
name = file.path
if name =~ /\.csv/
CSV::Reader.parse(file).each do |row|
yield row
end
else
roo = build_roo_wrapper(name)
roo.default_sheet = roo.sheets.first
1.upto(roo.last_row) do |line|
row = []
1.upto(roo.last_column) do |column|
row << roo.cell(line,column)
end
yield row
end
end
end
protected
def self.build_roo_wrapper(name)
roo = nil
if name =~ /\.xlsx/
roo = Excelx.new(name)
elsif name =~ /\.xls/
roo = Excel.new(name)
elsif name =~ /\.ods/
roo = Openoffice.new(name)
else
raise "Cannot parse this file, unrecognized format"
end
roo
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment