[英]Roo Gem Check for Empty Worksheet
I am using the roo gem to parse Excel (xlsx) files. 我正在使用roo gem来解析Excel(xlsx)文件。 However, there are always some empty sheets in the file (workbook). 但是,文件(工作簿)中总是有一些空白表。 Is there a way in roo to see if the sheet is empty or nil
? 在roo中有没有办法查看工作表是空的还是nil
?
Update 更新资料
wb = Roo::Excelx.new 'chapter_data.xlsx'
wb.sheets.each do |sheet|
wb.default_sheet = sheet
unless wb.nil?
code.....
end
end
The wb.nil?
wb.nil?
doesn't work it returns false. 不起作用,它返回false。
Update The only way I have been able to handle this issue is to rescue the no method error. 更新我能够处理此问题的唯一方法是挽救无方法错误。 I don't like handling it this way looking for a better way to check if the sheet is empty. 我不喜欢以这种方式处理它,以寻找一种更好的方法来检查工作表是否为空。
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
Figured out the best way to check if the sheet is empty. 找出检查表是否为空的最佳方法。
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. wb.first_row
返回第一个非空行的索引。
The only way that I could figure out how to check this without using a rescue
was to do the following 我可以弄清楚如何在不进行rescue
情况下进行检查的唯一方法是执行以下操作
unless wb.to_s == 'nil'
code......
end
Stupid that I couldn't do a regular nil
check. 愚蠢的是我无法定期进行nil
检查。 But this works. 但这有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.