How to point to proper worksheet in excel

M

Marcin Tyman

I have following code:

@xl = WIN32OLE.new('Excel.Application')
@xl['Visible'] = TRUE
@wb = @xl.Workbooks.Add
@ws = @xb.Worksheets("XXXXX")

The problem is that last line throws exception: moethod_missing:
Worksheets

Why? I had read examples from net and did everything according to them.


Another question is how to create a chart in excel and:
add several series to it
named it
assign names to axis
assign names to the series
set chart type.
 
D

David Mullet

Marcin said:
I have following code:

@xl = WIN32OLE.new('Excel.Application')
@xl['Visible'] = TRUE
@wb = @xl.Workbooks.Add
@ws = @xb.Worksheets("XXXXX")

The problem is that last line throws exception: moethod_missing:
Worksheets

Why? I had read examples from net and did everything according to them.

As Paul mentioned, you had a typo, where "@xb" should be "@wb".

A newly-created workbook (which you created via the Workbooks.Add
method) will have the default worksheet names (ie, Sheet1", "Sheet2")...

@ws = @wb.Worksheets("Sheet1")

You'll find details about working with Excel's workbook and worksheet
objects, with Ruby, here:

http://rubyonwindows.blogspot.com/2007/03/automating-excel-with-ruby-workbook.html

http://rubyonwindows.blogspot.com/2007/04/automating-excel-with-ruby-worksheet.html

http://rubyonwindows.blogspot.com/search/label/excel
Another question is how to create a chart in excel and:
add several series to it
named it
assign names to axis
assign names to the series
set chart type.

See my reply to your other post here specifically about Excel charts.

David

http://rubyonwindows.blogspot.com
 
L

lrlebron

I have following code:

@xl = WIN32OLE.new('Excel.Application')
@xl['Visible'] = TRUE
@wb = @xl.Workbooks.Add
@ws = @xb.Worksheets("XXXXX")

The problem is that last line throws exception: moethod_missing:
Worksheets

Why? I had read examples from net and did everything according to them.

Another question is how to create a chart in excel and:
add several series to it
named it
assign names to axis
assign names to the series
set chart type.

Here's an exzmple that may help

require 'win32ole'
excel = WIN32OLE::new('excel.Application')
excel['Visible'] = true
workbook = excel.Workbooks.Open(filename)
worksheet = workbook.Worksheets(1) #get hold of the first worksheet

You can also find a lot of information on using Ruby with Excel here
http://rubyonwindows.blogspot.com/

Hope this helps.

Luis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,786
Messages
2,569,626
Members
45,324
Latest member
ChristenCo

Latest Threads

Top