Class: ExcelParser::Transform

Inherits:
Object
  • Object
show all
Defined in:
lib/excel_parser.rb

Class Method Summary collapse

Class Method Details

.from_file(file) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/excel_parser.rb', line 8

def from_file(file)
  raise "no file provided" if file.nil?
  options = {:clean => true}
  xls = if file.end_with?('.xls')
        Roo::Excel.new(file, options)           # creates an Excel Spreadsheet instance
      elsif file.end_with?('.xlsx') 
        Roo::Excelx.new(file, options)          # creates an Excel Spreadsheet instance for Excel .xlsx files
      else
        raise "unsupported format, sorry. Please use .xls or .xlsx files."
      end
  
  all_sheets = []
  
  xls.each_with_pagename do |name, sheet|
    one_sheet = []
  
    next if sheet.first_row.nil? || sheet.last_row.nil? || sheet.first_column.nil? || sheet.last_column.nil?
    (sheet.first_row..sheet.last_row).each do |row|
      row_data = []
      (sheet.first_column..sheet.last_column).each do |column|
        row_data << sheet.cell(row, column)
      end
      one_sheet << row_data
    end
    all_sheets << [name, one_sheet]
  end
  return all_sheets
end