Class: RailsAdminImport::Formats::XLSXImporter

Inherits:
FileImporter
  • Object
show all
Defined in:
lib/rails_admin_import/formats/xlsx_importer.rb

Instance Attribute Summary

Attributes inherited from FileImporter

#error, #filename, #import_model

Instance Method Summary collapse

Methods inherited from FileImporter

#each, #valid?

Constructor Details

#initialize(import_model, params) ⇒ XLSXImporter

Returns a new instance of XLSXImporter.



11
12
13
14
# File 'lib/rails_admin_import/formats/xlsx_importer.rb', line 11

def initialize(import_model, params)
  super
  @header_converter = RailsAdminImport.config.header_converter
end

Instance Method Details

#each_recordObject

A method that yields a hash of attributes for each record to import



17
18
19
20
21
22
23
24
25
# File 'lib/rails_admin_import/formats/xlsx_importer.rb', line 17

def each_record
  doc = SimpleXlsxReader.open(filename)
  sheet = doc.sheets.first
  @headers = convert_headers(sheet.headers)
  sheet.data.each do |row|
    attr = convert_to_attributes(row)
    yield attr unless attr.all? { |field, value| value.blank? }
  end
end