June 24, 2013

Tester Friend : Fetching data from Excel in ruby using Spreadsheet gem

Herewith I have user the Spreadsheet gem for accessing the excel sheet using ruby language.

To print the entire lines in excel file the below script executes well.

==========================
Install the gem: gem install spreadsheet

require "rubygems"
require 'spreadsheet'  
    book= Spreadsheet.open('myexcel.xls')
    book.worksheets
    sheet1 = book.worksheet 0
             sheet1.each do |row|
             puts row
     end

=======================

Send a mail from your system to your gmail account using Ruby

Below is the script to send mail from ur local system to gmail account.

Hope the below mentioned code executes well in any local system. This scripts executes using ruby language.

==============================================================================

require 'rubygems'
require 'net/smtp'

#Senders and Recipients
from_name = 'jegan'
from_mail = 'jeganrajkumar@gmail.com'
to_name = 'jegan raj kumar'
to_mail = 'jeganrajkumar@gmail.com'

#Servers and Authentication
smtp_host   = 'localhost'
smtp_port   = 587
smtp_domain = 'smtp.gmail.com'
smtp_user   = 'jeganrajkumar@gmail.com'
smtp_pwd    = 'password'


#The subject and the message
subj = 'Sending Email with Ruby'

msg_body = "Content message of the body section \n"

#Compose the message for the email
msg = <<END_OF_MESSAGE
From: #{from_name} <#{from_mail}>
To: #{to_name} <#{to_mail}>
Subject: #{subj}

#{msg_body}
END_OF_MESSAGE
smtp = Net::SMTP.new(smtp_domain,smtp_port)
smtp.enable_starttls_auto if smtp.respond_to?(:enable_starttls_auto)
smtp.start(smtp_host,smtp_user,smtp_pwd, :login) do |smtp|
  smtp.send_message msg, from_mail, to_mail
end


===================================

Reason: whenever my automation script completes at last, I need to receive the test report via  mail. While searching for that I got this information. Hope it will be useful for you too.