Module: SpreadsheetImporter::Import
- Defined in:
- lib/spreadsheet_importer/import.rb
Class Method Summary collapse
- .from_csv(file_path, options = {}, &block) ⇒ Object
- .from_spreadsheet(spreadsheet, options = {}, &block) ⇒ Object
- .from_xlsx(file_path, options = {}, &block) ⇒ Object
Class Method Details
.from_csv(file_path, options = {}, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/spreadsheet_importer/import.rb', line 13 def self.from_csv(file_path, = {}, &block) # Detect the encoding of the file and normalize it to UTF-8 csv = File.read(file_path) encoding = CharlockHolmes::EncodingDetector.detect(csv)[:encoding] csv = CharlockHolmes::Converter.convert csv, encoding, 'UTF-8' # Get rid of the UTF-16LE BOM since Charlock doesn't do this for us csv.slice!(0) if csv[0].ord == 65279 # Determine whether the column separator is a tab or comma col_sep = csv.count("\t") > 0 ? "\t" : "," spreadsheet = CSV.parse(csv, :col_sep => col_sep, :headers => true, :header_converters => :downcase) from_spreadsheet(spreadsheet, , &block) end |
.from_spreadsheet(spreadsheet, options = {}, &block) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/spreadsheet_importer/import.rb', line 29 def self.from_spreadsheet(spreadsheet, = {}, &block) = {:start_row => 1, :schema => nil}.merge() ([:start_row] - 1).times { spreadsheet.shift } # Remove intro rows spreadsheet = [:schema].conform(spreadsheet) if [:schema] # If a Conformist schema is provided, use that to prepare rows errors = [] rowcount = 0 spreadsheet.each do |row| rowcount += 1 begin block.call(row) print '.' rescue => e progress_indicator = '!' errors << "Row #{rowcount}: #{e.message}" print '!' end end rowcount += [:start_row] if rowcount > 0 return {:imported => rowcount - errors.count, :errors => errors, :total => rowcount} end |
.from_xlsx(file_path, options = {}, &block) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'lib/spreadsheet_importer/import.rb', line 3 def self.from_xlsx(file_path, = {}, &block) = {:sheet_name => nil}.merge() spreadsheet = [] Roo::Excelx.new(file_path, :file_warning => :ignore).each_with_pagename do |name, sheet| spreadsheet.concat sheet.to_a unless [:sheet_name] && name.downcase.strip != [:sheet_name].downcase.strip end from_spreadsheet(spreadsheet, , &block) end |