Class: Importer::XlsReader

Inherits:
DataReader show all
Defined in:
lib/iron/import/xls_reader.rb

Instance Attribute Summary

Attributes inherited from DataReader

#format

Instance Method Summary collapse

Methods inherited from DataReader

for_format, for_path, for_stream, #load, #parse_value, path_from_stream, verify_roo!

Constructor Details

#initialize(importer) ⇒ XlsReader

Returns a new instance of XlsReader.



5
6
7
# File 'lib/iron/import/xls_reader.rb', line 5

def initialize(importer)
  super(importer, :xlsx)
end

Instance Method Details

#load_file(path) ⇒ Object



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
36
37
38
# File 'lib/iron/import/xls_reader.rb', line 9

def load_file(path)
  spreadsheet = Roo::Excel.new(path, :file_warning => :ignore)
  if spreadsheet
    # Get our list of sheet definitions, and run all the sheets in the spreadsheet
    remaining_sheets = @importer.sheets.values
    spreadsheet.sheets.each_with_index do |name, index|
      # Look for a sheet definition that matches this sheet's name/index
      sheet = remaining_sheets.detect {|s| s.match_sheet?(name, index) }
      if sheet
        # Remove from our list of remaining sheets
        remaining_sheets.delete(sheet)
        # Extract our raw data
        raw_rows = []
        spreadsheet.sheet(name).each_with_index do |row, line|
          raw_rows << row
        end
        # Let the sheet sort it out
        sheet.parse_raw_data(raw_rows)
      end
    end
    return true
  else
    @importer.add_error("Unable to read Excel file at path #{path}")
    return false
  end
  
rescue Exception => e
  @importer.add_error("Error reading file #{path}: #{e}")
  false
end