Class: DataImport::XlsxParser
- Inherits:
-
BaseParser
- Object
- BaseParser
- DataImport::XlsxParser
- Defined in:
- app/services/data_import/xlsx_parser.rb
Overview
Service object: xlsx parser.
Instance Method Summary collapse
- #column_names ⇒ Object
-
#each_with_index ⇒ Object
Iterate through value rows, adjusting index to zero based when first row is header.
- #size ⇒ Object
- #validate ⇒ Object
- #workbook ⇒ Object
- #worksheet ⇒ Object
Instance Method Details
#column_names ⇒ Object
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_index ⇒ Object
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 |
#size ⇒ Object
36 37 38 |
# File 'app/services/data_import/xlsx_parser.rb', line 36 def size worksheet.last_row || 0 end |
#validate ⇒ Object
31 32 33 34 |
# File 'app/services/data_import/xlsx_parser.rb', line 31 def validate # To be done [] end |
#workbook ⇒ Object
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 |
#worksheet ⇒ Object
9 10 11 |
# File 'app/services/data_import/xlsx_parser.rb', line 9 def worksheet @worksheet ||= workbook.sheet(0) end |