Search code examples
ruby-on-railsrubyrubyxl

How I get the row number in a iteration using RubyXL?


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


Solution

  • 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