Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the row index after I do an .add_row using the axlsx gem?

Tags:

ruby

axlsx

I'm using the axlsx gem to create a XLSX file and have the following line in my code:

ws.add_row( "xyz" )

Is there a way to get the row index of the row I had just added? I might have to add a row later.

like image 480
Evilmuffin Avatar asked Sep 26 '16 17:09

Evilmuffin


1 Answers

I use these two methods for doing it:

wb = xlsx_package.workbook

wb.add_worksheet(name: 'sheet 1') do |sheet|
  sheet.add_row ['foo', 'bar']
  my_first_row = sheet.add_row ['aladdin', 'rules']
  my_second_row = sheet.add_row ['hello', 'world']

  # Method 1
  sheet.rows.index(my_first_row) # will return 1
  sheet.rows.index(my_second_row) # will return 2

  # Method 2
  my_first_row.row_index # will return 1
  my_second_row.row_index # will return 2
end

Regards

like image 117
mld.oscar Avatar answered Oct 13 '22 20:10

mld.oscar