# Inputting part of array into Excel

**URL:** <https://www.ruby-forum.com/t/inputting-part-of-array-into-excel/110226>\
**Category:** Ruby\
**Created:** [July 24, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/inputting-part-of-array-into-excel/110226 "2007-07-24T13:52:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/4491bb/32.png) [@qwerty](https://www.ruby-forum.com/u/qwerty)\
**Post date:** [July 24, 2007, 1:52pm UTC](https://www.ruby-forum.com/t/inputting-part-of-array-into-excel/110226/1 "2007-07-24T13:52:00Z")

</div>

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](http://www.google.com)  
#[www.yahoo.com](http://www.yahoo.com)  
#[www.microsoft.com](http://www.microsoft.com)  
#[www.sun.com](http://www.sun.com)  
#[www.intel.com](http://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

---

<div class="post-metadata">

**Author:** ![qwerty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/4491bb/32.png) [@qwerty](https://www.ruby-forum.com/u/qwerty)\
**Post date:** [July 24, 2007, 2:58pm UTC](https://www.ruby-forum.com/t/inputting-part-of-array-into-excel/110226/2 "2007-07-24T14:58:29Z")

</div>

Qwerty Qwerty wrote:

> Newby Excel question

> The cells A1:A5 contain e.g. the following:
> 
> #[www.google.com](http://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](http://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

---

<div class="post-metadata">

**Author:** ![qwerty](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/4491bb/32.png) [@qwerty](https://www.ruby-forum.com/u/qwerty)\
**Post date:** [July 24, 2007, 3:30pm UTC](https://www.ruby-forum.com/t/inputting-part-of-array-into-excel/110226/3 "2007-07-24T15:30:10Z")

</div>

Thanks Todd. That did the trick!
