Ruby电子表格:在xls文件中获取列数

时间:2016-10-11 20:38:15

标签: ruby excel spreadsheet xls

有没有办法使用ruby计算电子表格文件中的列数?我正在使用最新版本的电子表格gem。

1 个答案:

答案 0 :(得分:2)

根据您的需求和情况,看起来有很多可能的解决方案:

book = Spreadsheet.open('/path/to/an/excel-file.xls')
sheet1 = book.worksheet(0)

# get the number of columns in the first row
sheet1.row(0).size

# get the maximum number of columns in all the rows
sheet1.rows.max_by(&:size)

# use the dimension logic from the gem.  It looks like this ignores empty columns at the beginning of the sheet
sheet1.column_count

column_count来源: https://github.com/zdavatz/spreadsheet/blob/master/lib/spreadsheet/worksheet.rb#L96-L99

让我们知道什么对您有用,以及您通过玩它找到了什么。

相关问题