Created
March 13, 2010 04:22
-
-
Save evizitei/331103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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