Class: DataImport::XlsxParser

Inherits:
BaseParser
  • Object
show all
Defined in:
app/services/data_import/xlsx_parser.rb

Overview

Service object: xlsx parser.

Instance Method Summary collapse

Instance Method Details

#column_namesObject



23
24
25
26
27
28
29
# File 'app/services/data_import/xlsx_parser.rb', line 23

def column_names
  if first_row_is_header?
    worksheet.row(1)
  else
    worksheet.row(1).each_with_index.map { |_x, i| "Column#{i}" }
  end
end

#each_with_indexObject

Iterate through value rows, adjusting index to zero based when first row is header



14
15
16
17
18
19
20
21
# File 'app/services/data_import/xlsx_parser.rb', line 14

def each_with_index
  worksheet.each_with_index do |row, index|
    unless index.zero? && first_row_is_header?
      fixed_index = first_row_is_header? ? index - 1 : index
      yield(row, fixed_index)
    end
  end
end

#sizeObject



36
37
38
# File 'app/services/data_import/xlsx_parser.rb', line 36

def size
  worksheet.last_row || 0
end

#validateObject



31
32
33
34
# File 'app/services/data_import/xlsx_parser.rb', line 31

def validate
  # To be done
  []
end

#workbookObject



4
5
6
7
# File 'app/services/data_import/xlsx_parser.rb', line 4

def workbook
  require 'roo'
  @workbook ||= Roo::Spreadsheet.open(@file_path)
end

#worksheetObject



9
10
11
# File 'app/services/data_import/xlsx_parser.rb', line 9

def worksheet
  @worksheet ||= workbook.sheet(0)
end