How to access the column/row info of existing content of a xlsx file

1.8k Views Asked by At

I want to get the info of the last column and row of an existing xlsx file so that I can append new contents right below the existing content. How do I do so with RubyXL? If that's not possible, what alternative gem would you recommend?

2

There are 2 best solutions below

2
On BEST ANSWER

As I wrote in my comment, I don't know if this is exactly what you are looking for:

require 'rubyXL'

workbook = RubyXL::Parser.parse("Workbook1.xlsx")
worksheet = workbook[0]

rows = worksheet.map {|row| row && row.cells.each { |cell| cell && cell.value != nil}}
p last_row = rows.size
p last_column = rows.compact.max_by{|row| row.size}.size
0
On

Let worksheet be the first (e.g. worksheet = workbook[0]).

You could use:

last_row = worksheet.count
last_col = worksheet.map{|i| i.cells.count}.max