Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ruby / Yukihiro Matsumoto_Programming Ruby.doc
Скачиваний:
121
Добавлен:
06.06.2015
Размер:
2.71 Mб
Скачать

Windows Automation

If groveling around in the low-level Windows API doesn't interest you, Windows automation might---you can use Ruby as a client for Windows Automation thanks to a Ruby extension called WIN32OLE, written by Masaki Suketa. The examples in this section are taken from those provided in theWIN32OLEdistribution.

Windows automation allows an automation controller (a client) to issue commands and queries against an automation server, such as Microsoft Excel, Word, PowerPoint, and so on.

You can execute a method of an automation server by calling a method of the same name from a WIN32OLEobject. For instance, you can create a newWIN32OLEclient that launches a fresh copy of Internet Explorer and commands it to visit the home page.

ie = WIN32OLE.new('InternetExplorer.Application')

ie.visible = true

ie.gohome

Methods that aren't known to WIN32OLE(such asvisibleorgohome) are passed on to theWIN32OLE#invokemethod, which sends the proper commands to the server. TheWIN32OLEreference beginning on page 505 describes the class in detail, but we'll go over a few of its features here.

Getting and Setting Properties

You can set and get propertiesfrom the server using normal Ruby hash notation. For example, to set theRotationproperty in an Excel chart, you might write

excel = WIN32OLE.new("excel.application")

excelchart = excel.Charts.Add()

...

excelchart['Rotation'] = 45

puts excelchart['Rotation']

An OLE object's parameters are automatically set up as attributes of the WIN32OLEobject. This means that you can set a parameter by assigning to an object attribute.

excelchart.rotation = 45

r = excelchart.rotation

Because these attributes are conventional Ruby accessor methods, attribute names cannot start with a capital letter. In this example, we have to use rotationinstead ofRotation.

Named Arguments

Other automation client languages such as Visual Basic have the concept of named arguments. Suppose you had a Visual Basic routine with the signature:

Song(artist, title, length):    rem Visual Basic

Instead of calling it with all three arguments in the order specified, you could use named arguments.

Song title := 'Get It On':      rem Visual Basic

This is equivalent to the call Song(nil, 'Get It On', nil).

In Ruby, you can use this feature by passing a hash with the named arguments.

Song.new( 'title' => 'Get It On' )

For each

Where Visual Basic has a ``for each'' statement to iterate over a collection of items in a server, a WIN32OLEobject has aneachmethod (which takes a block) to accomplish the same thing.

An Example

The following example, using Microsoft Excel, illustrates most of these concepts. First, we create a new WIN32OLEobject attached to Excel and set some cell values. Next we select a range of cells and create a chart. We set theTypeproperty in theexcelchartobject to make it a 3D chart. Next we'll loop through and change the chart rotation, 10° at a time. We'll add a few charts, and we'll useeachto step through and print them out. Finally, we'll close down the Excel application and exit.

require 'win32ole'

# -4100 is the value for the Excel constant xl3DColumn.

ChartTypeVal = -4100;

# Creates OLE object to Excel

excel = WIN32OLE.new("excel.application")

# Create and rotate the chart

excel['Visible'] = TRUE;

workbook = excel.Workbooks.Add();

excel.Range("a1")['Value'] = 3;

excel.Range("a2")['Value'] = 2;

excel.Range("a3")['Value'] = 1;

excel.Range("a1:a3").Select();

excelchart = workbook.Charts.Add();

excelchart['Type'] = ChartTypeVal;

30.step(180, 10) do |rot|

    excelchart['Rotation'] = rot

end

excelchart2 = workbook.Charts.Add();

excelchart3 = workbook.Charts.Add();

charts = workbook.Charts

charts.each { |i| puts i }

excel.ActiveWorkbook.Close(0);

excel.Quit();

Соседние файлы в папке Ruby