Reading Excel Sheets using "Roo" gem in ruby on rails

This gem allows you to read the contents of
  • Open-office spreadsheets (.ods)
  • Excel spreadsheets (.xls)
  • Google (online) spreadsheets
  • Excel's new file format .xlsx

How to use this gem :
1) Install 

         [sudo] gem install roo

2) Create an instance of the excel sheet file        
  
       require 'rubygems'
       require 'roo'       
       s = Roo::Openoffice.new("myspreadsheet.ods")
       s = Roo::Excel.new("myspreadsheet.xls")
       s = Roo::Google.new("myspreadsheet_at_google")
       s = Roo::Excelx.new("myspreadsheet.xlsx")
    
3)  Reading from a particular sheet of the spreadsheet:
     Lets say your spreadsheet has 4 sheets and you want to read the 2nd sheet
      s.default_sheet = s. sheets[2]

   *  Sheets is an array containing the names of all the sheets inside the spread sheet. 
   *  If we do not set the default sheet option, the first sheet of the spreadsheet is taken as the         default sheet.

4) Reading each cell of the sheet
       s.cell(1,1)                                   # returns the first column of the first row
      s.cell(A,1)                                   # returns the same cell
      s.cell(1,A)                                   # returns the same cell
      s.cell(1, A, s.sheets[1])           # returns the same cell of the second sheet in the                                                                                        spreadsheet.

 * Almost all methods have an optional argument 'sheet'.  If we do not specify this parameter,     the default sheet is used.

5) Other Important Methods

s.first_row                                           # returns the number of first un-empty row

s.last_row                                            # returns the number of last un-empty row

s.first_column                                   # returns the number of first un-empty column

s.last_column                                    # returns the number of last un-empty column

s.celltype(1,1)                                    # returns the cell type of the specified cell. It may be                                                                                 string, date,float,datetime, percentage,formula.

Comments

  1. Your blog is in a convincing manner, thanks for sharing such an information with lots of your effort and time
    ruby on rails training India
    ruby on rails training Hyderabad

    ReplyDelete

Post a Comment

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Bootstrap Select Picker append add new item if search not exist

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails