Class: DataImport::XlsxParser

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

Instance Method Summary collapse

Instance Method Details

#column_namesObject



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

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



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

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



34
35
36
# File 'app/services/data_import/xlsx_parser.rb', line 34

def size
  worksheet.last_row || 0
end

#validateObject



29
30
31
32
# File 'app/services/data_import/xlsx_parser.rb', line 29

def validate
  # To be done
  []
end

#workbookObject



2
3
4
5
# File 'app/services/data_import/xlsx_parser.rb', line 2

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

#worksheetObject



7
8
9
# File 'app/services/data_import/xlsx_parser.rb', line 7

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