class Importer::ExcelReader

Uses the Roo gem to read in .xls files

Public Class Methods

new(importer, format) click to toggle source
Calls superclass method Importer::DataReader::new
# File lib/iron/import/excel_reader.rb, line 6
def initialize(importer, format)
  super(importer, format)
  supports_file!
end

Public Instance Methods

include_sheet?(scopes, name, index) click to toggle source

When true, the given sheet name or zero-based index is a match with our id.

# File lib/iron/import/excel_reader.rb, line 55
def include_sheet?(scopes, name, index)
  return true if scopes.nil? || scopes.empty?
  scopes.each do |scope|
    if scope.is_a?(Fixnum)
      return true if scope.to_i == index+1
    else
      return true if scope.to_s.downcase == name.downcase
    end
  end
  false
end
init_source(mode, source) click to toggle source
# File lib/iron/import/excel_reader.rb, line 11
def init_source(mode, source)
  if mode == :file
    if @format == :xls
      @spreadsheet = Roo::Excel.new(source, :file_warning => :ignore)
      true
    elsif @format == :xlsx
      @spreadsheet = Roo::Excelx.new(source, :file_warning => :ignore)
      true
    else
      add_error("Unknown format for Excel file: :#{@format}")
      false
    end
  else
    add_error("Unsupported #{@format.to_s.upcase} mode: #{mode}")
    false
  end
rescue Exception => e
  add_exception(e)
  false
end
load_raw(scopes, &block) click to toggle source
# File lib/iron/import/excel_reader.rb, line 32
def load_raw(scopes, &block)
  @spreadsheet.sheets.each_with_index do |name, index|
    # See if this sheet's name or index matches the requested sheet definition
    if include_sheet?(scopes, name, index)
      # Extract our raw data
      raw_rows = []
      @spreadsheet.sheet(name).each_with_index do |row, line|
        raw_rows << row
      end
      # Yield our raw rows for this sheet
      found = block.call(raw_rows)
      # If we've found a working sheet, stop
      return if found
    end
  end

rescue Exception => e
  # Not sure why we'd get here, but we strive for error-freedom here, yessir.
  add_exception(e)
end