2

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?

beauXjames
  • 8,222
  • 3
  • 49
  • 66
Jackson Gong
  • 75
  • 1
  • 9
  • What docs did you follow? – Jagdeep Singh May 07 '18 at 05:09
  • Jagdeep: I read the readme and the worksheet page of the manual. I also checked .methods to see which method looks promising, but to no avail. Do you know how to do this? Thanks! – Jackson Gong May 07 '18 at 05:58
  • I mean can you post the link for me and others to see? – Jagdeep Singh May 07 '18 at 06:39
  • With this `table = worksheet.map {|row| row && row.cells.each { |cell| cell.value != nil}}` you can build an array reflecting the sheet content, then find where data ends and add your content. If that's your question. Or you can add more details to your post. – iGian May 07 '18 at 07:35

2 Answers2

5

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
iGian
  • 11,023
  • 3
  • 21
  • 36
0

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
Hobbes
  • 978
  • 2
  • 9
  • 15