1

我正在使用 roo gem 来解析 Excel (xlsx) 文件。但是,文件(工作簿)中总是有一些空工作表。有没有办法在 roo 中查看工作表是否为空nil

更新

wb = Roo::Excelx.new 'chapter_data.xlsx'

    wb.sheets.each do |sheet|
      wb.default_sheet = sheet
      unless wb.nil?
       code.....
      end
   end

wb.nil?不起作用它返回false 。

更新 我能够处理此问题的唯一方法是挽救无方法错误。我不喜欢以这种方式处理它,以寻找一种更好的方法来检查工作表是否为空。

begin
   header_row = wb.row(1)
 rescue NoMethodError => err
   # do nothing, this sheet is empty can't find a way to check if it is empty pos
 end
4

2 回答 2

7

找出检查工作表是否为空的最佳方法。

wb = Roo::Excelx.new 'chapter_data.xlsx'

wb.sheets.each do |sheet|
  wb.default_sheet = sheet
    if wb.first_row
      code.....
    end
end

The wb.first_row returns the index of the first non empty row.

于 2013-04-18T16:23:59.660 回答
0

我可以弄清楚如何在不使用 a 的情况下进行检查的唯一方法rescue是执行以下操作

unless wb.to_s == 'nil'
  code......
end

愚蠢的是我不能定期nil检查。但这有效。

于 2013-04-17T16:15:26.880 回答