From 3b08241b0b130841e68a350604ae21d38e1d9704 Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Mon, 18 May 2020 09:52:37 +0200 Subject: [PATCH] Fix excel header parsing --- app/services/spreadsheet_parser.rb | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/services/spreadsheet_parser.rb b/app/services/spreadsheet_parser.rb index 52ae3370f..aedd01b0a 100644 --- a/app/services/spreadsheet_parser.rb +++ b/app/services/spreadsheet_parser.rb @@ -39,20 +39,20 @@ class SpreadsheetParser rows = spreadsheet_enumerator(sheet) header = [] columns = [] - i = 1 - rows.each do |row_values| - # Creek XLSX parser returns Hash of the row, Roo - Array - row = parse_row(row_values, sheet) - header = row if i == 1 && row - columns = row if i == 2 && row - i += 1 - break if i > 2 + rows.take(2).each_with_index do |row_values, i| + row = parse_row(row_values, sheet, header: i.zero?) + if row && i.zero? + header = row + else + columns = row + end end + return header, columns end - def self.parse_row(row, sheet) - if sheet.is_a?(Roo::Excelx) + def self.parse_row(row, sheet, header: false) + if sheet.is_a?(Roo::Excelx) && !header row.map do |cell| if cell.is_a?(Roo::Excelx::Cell::Number) && cell.format == 'General' cell&.value&.to_d