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

Ruby spreadsheet : getting column count in xls file

有没有什么方法可以使用 ruby 计算电子表格文件中的列数?我使用的是最新版本的电子表格 gem.

根据您的需要和情况,似乎有多种可能的解决方案:

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

让我们知道什么对您有用,以及您通过使用它发现了什么。