[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Inputting part of array into Excel

Qwerty Qwerty

7/24/2007 11:52:00 AM

Newby Excel question

I am trying to input data from a column, iterate through the column, do
some tests and then output the results into an adjacent column. For
example I have a basic script below, which reads in an array from excel
and takes the first row and first column and iterates through the rows:

The cells A1:A5 contain e.g. the following:

#www.google.com
#www.yahoo.com
#www.microsoft.com
#www.sun.com
#www.intel.com

As each of the links is loaded I do a basic assert and depending on the
result would like to put "Passed" or "Failed" into the adjacent column
B1:B5
Using "puts" I can get it to do this, but I cannot figure out how to put
the result into Excel?
Any help appreciated? Thanks in advance.



excel = WIN32OLE::new('excel.Application') #connect to Excel
excel['Visible'] = true
workbook =
excel.Workbooks.Open('C:\IterateThroughArrayAndPutResult.xls') #
spreadsheet name and location
worksheet = workbook.Worksheets(1) #get hold of the first worksheet
$x = worksheet.Range("A1:A5")['value']#['text']# read in a 2d array
(with 1 column and 5 rows)
$x.each do |$row| # Iterate through each of the rows in turn.
$test_site = $row[0] # use this to identify the column for the data in
$row.
ie = Watir::IE.new_process #Start a new process of IE
ie.goto($test_site) # goto the first row in the first column and grap
the URL, put this into the browser
if assert(ie.button(:name, "doit")) # a basic test to see if text exists
on each of these websites
puts "Test Passed: Button exits. Actual Results match Expected Results."
worksheet.Range("B1:B5") ['Value'] = ['Passed'] # Enter the following
text into the cell
else
puts "Test Failed: Button does not exists."
worksheet.Range("B1:B5") ['Value'] = ['Failed'] # Enter the following
text into the cell
end
--
Posted via http://www.ruby-....

2 Answers

Todd Burch

7/24/2007 12:58:00 PM

0

Qwerty Qwerty wrote:
> Newby Excel question
>

> The cells A1:A5 contain e.g. the following:
>
> #www.google.com
> #www
>
> $x = worksheet.Range("A1:A5")['value']#['text']# read in a 2d array
> (with 1 column and 5 rows)
> $x.each do |$row| # Iterate through each of the rows in turn.
> $test_site = $row[0] # use this to identify the column for the data in
> $row.

First, inside your loop, why are you treating the string like an array?
I would code this:

i = 1 ;
$x.each do |site| # note there no need for a global variable
# the first value will be "www.google.com"
...
ie.goto(site)
....
result = nil ;
if (...true...) then result = "Passed" ;
else result = "Failed" ;

worksheet.Range("B#{i}").Value = result;
i += 1 ;
end ;


Todd
--
Posted via http://www.ruby-....

Qwerty Qwerty

7/24/2007 1:30:00 PM

0

Thanks Todd. That did the trick!
--
Posted via http://www.ruby-....