2

Using RubyXL I want to know what row number my iteration is.

    workbook = RubyXL::Parser.parse("./file.xlsx")

    worksheet = workbook[0]

    worksheet.each do |row|
    test0 = row[0].value
    line = ????
    puts "Line number #{line} - Value = #{test0}"
    end

Ricardo
  • 23
  • 5

3 Answers3

0

You can use each_with_index method while looping to get the current row number of the iteration

worksheet.each_with_index do |row, index|
    test0 = row[0].value
    line = index
    puts "Line number #{line} - Value = #{test0}"
end
Ravi Teja Gadi
  • 1,755
  • 2
  • 8
  • 15
0

You can use #each_with_index and write it like this:

workbook = RubyXL::Parser.parse("./file.xlsx")

workbook.first.each_with_index do |row, index|
  puts "Line number #{index} - Value = #{row[0].value}"
end
Viktor
  • 2,623
  • 3
  • 19
  • 28
0

Call row.r to get the current row number.

Not sure when this was added but you can now do the following:

worksheet.each do |row|
  puts "Row Number #{ row.r }"
end

Just keep in mind that the first row number is 1 and is usually the column headers/names.

Joshua Pinter
  • 45,245
  • 23
  • 243
  • 245