How to check Excel first sheet's first row withRruby

Viewed 29

I have an xlsx file and I want to check first sheet's first_row. But I get this error: invalid value for Integer(): "". If I edit the sheet name the error goes. I checked the solution:https://github.com/roo-rb/roo/issues/478 and new version of roo gem. But the solution didn't work for me.

The code is:

 def read_excel
   spread_sheet = Roo::Spreadsheet.open(file_path).sheet(0)
   if spread_sheet.first_row.blank? # throws the error (invalid value for Integer(): "")
     fail Errors::ExcelImport::EmptyFile
   end
   spread_sheet
 end

Roo version is : 2.7.1 and Ruby version is 2.7.2

1 Answers

Updated

I found a bug in Roo Gem. My system is working correct but the Gem throws error. I have to handle it in my system. I found a solution like that:

  def read_excel
    spread_sheet = Roo::Spreadsheet.open(file_path).sheet(0)
    begin
      if spread_sheet.first_row.blank?
        fail Errors::ExcelImport::EmptyFile
      end
      spread_sheet
    rescue StandardError
      fail Errors::ExcelImport::InvalidSheetName
    end

  end

I created a new error and I show the error to users. If users edit sheet name the bug is fixing.

Related