Class: TableImporter::Excel

Inherits:
RooSpreadsheetSource show all
Defined in:
lib/table_importer/excel.rb

Constant Summary

Constants inherited from Source

Source::SEPARATORS

Instance Method Summary collapse

Methods inherited from RooSpreadsheetSource

#convert_headers, #get_chunks, #get_headers, #get_lines, #get_preview_lines

Methods inherited from Source

#clean_chunks, #default_headers, #get_chunks, #get_column_separator, #get_headers, #get_lines, #get_preview_lines, #get_record_separator, #get_sep_count, #sort_separators

Constructor Details

#initialize(data) ⇒ Excel

Returns a new instance of Excel.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/table_importer/excel.rb', line 5

def initialize(data)
  begin
    @type = File.extname(data[:content]) == ".xls" ? "xls" : "xlsx"
    @headers_present = data[:headers_present]
    @file = get_file(data[:content].path)
    @compulsory_headers = data[:compulsory_headers]
    @delete_empty_columns = (File.size(data[:content].path) < 100000)
    @mapping = data[:user_headers]
    raise TableImporter::EmptyFileImportError.new if !@file.first_row
    @headers = @headers_present ? @file.row(1).map.with_index { |header, index| header.present? ? header.to_sym : "column_#{index}"} : default_headers
  rescue NoMethodError
    raise TableImporter::HeaderMismatchError.new
  end
end

Instance Method Details

#get_file(path) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/table_importer/excel.rb', line 20

def get_file(path)
  begin
    if @type == "xls"
      Roo::Excel.new(path).sheet(0)
    elsif @type == "xlsx"
      Roo::Excelx.new(path).sheet(0)
    end
  rescue TypeError
    raise TableImporter::IncorrectFileError.new
  end
end

#get_typeObject



32
33
34
# File 'lib/table_importer/excel.rb', line 32

def get_type
  "xls"
end