ruby-on-railsrubyxl

how to use rubyxl to access the first row


workbook = RubyXL::Parser.parse(params[:file].path)  
worksheet = workbook[0]
puts worksheet.sheet_data[0][0]

But I am getting this as output

<RubyXL::Cell(0,0): "0", datatype="s", style_index=1>

My excel sheet is of this form

    name    coupon
    gates   gates1234
    jobs    jobs1234

I want to access rows one by one .. any help will be appreciated.

I also made a sample app for this post, if you want to run it ..

https://github.com/vamsipavanmahesh/example-excel-reader


Solution

  • worksheet.sheet_data[0]
    gives you a Ruby object representing the first row.

    worksheet.sheet_data[0][0]
    gives you a Ruby object representing the first cell on the first row. But to get the actual contents of that cell, you need
    worksheet.sheet_data[0][0].value

    The worksheet is organised as an array of cells. You have to deal with the cells one by one in each row, if you are processing the rows sequentially.