简体   繁体   中英

Ruby spreadsheet : getting column count in xls file

Is there any way to count the number of columns in a spreadsheet file using ruby? I am using the latest version of spreadsheet gem.

It looks like there are a number of possible solutions based on your needs and situation:

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 source: https://github.com/zdavatz/spreadsheet/blob/master/lib/spreadsheet/worksheet.rb#L96-L99

Let us know what works for you and what you find out by playing with it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM