From: David Mullet Date: 2006-08-10T20:31:05+09:00 Subject: Re: Entering data into Excel, in specific iterated rows/colu reed.adam@gmail.com wrote: > I have a script that spiders a page looking for elements. In this > specific case, I'm looking for links (innerText and href). > > I have this script working so far to output data to a text file, but > I'd like to output it to an Excel file for organization. I'm getting > stuck however trying to think of the correct method to put all of the > link.innerText values into A1, A2, A3, etc and the corresponding > link.href into B1, B2, etc. > > Here is the code I have so far (with the normal text file code in place > of the excel code I have yet to think of): > > ef grab_links(url) > $ie.goto(url) > excel = WIN32OLE.new("excel.application") > excel.visible = true > workbook = excel.workbooks.add > workbook.saveas("#{Project}_links.xls") > workbook.close > > linkfile.puts "\n#{url}" > $ie.links.each{|link| > linkfile.puts("#{link.innerText} - #{link.href}") unless > link.innerText.empty? > } > > linkfile.puts "\n\n" > end > > One big file with it formatted like this would be fine, or even a > different worksheet for each URL would be great. I'm not really > concered with that. Can you guys give me hand? > > Thanks! xl = WIN32OLE.new("Excel.Application") xl.Visible = 1 wb = xl.Workbooks.Add ws = wb.Worksheets(1) row = 0 $ie.links.each do |link| next if link.innerText.empty? row = row + 1 ws.Cells(row, 1).Value = link.innerText ws.Cells(row, 2).Value = link.href end [ Not tested, and posted before I've had my morning tea... ] Mully -- Posted via http://www.ruby-forum.com/.