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.
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.
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
sheet.merge_cells('A%i:D%i' % [row.row_index+1,row.row_index+1])
. Ruby starts to count with 0, Excel starts with 1. –
Amathiste © 2022 - 2024 — McMap. All rights reserved.
ws.rows.last.index
right after theadd_row
to get the index of that row. Please note that the index of a row starts at zero because ruby uses zero indexing but excel uses indexing starting at 1 so depending on how you intend to use this you may need to add +1 to the index. – Rowelws.rows.last.row_index
? – Lauzonindex
but now you are correct in 3.0.0. pre it should berow_index
– Rowel