Class: DataImport::XlsxParser
- Inherits:
-
BaseParser
- Object
- BaseParser
- DataImport::XlsxParser
- Defined in:
- app/services/data_import/xlsx_parser.rb
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
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_index ⇒ Object
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 |
#size ⇒ Object
34 35 36 |
# File 'app/services/data_import/xlsx_parser.rb', line 34 def size worksheet.last_row || 0 end |
#validate ⇒ Object
29 30 31 32 |
# File 'app/services/data_import/xlsx_parser.rb', line 29 def validate # To be done [] end |
#workbook ⇒ Object
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 |
#worksheet ⇒ Object
7 8 9 |
# File 'app/services/data_import/xlsx_parser.rb', line 7 def worksheet @worksheet ||= workbook.sheet(0) end |