Automation 桥
OpenOffice.org 软件支持 Microsoft 的 Automation 技术。这为程序员提供了从外部程序控制办公软件的可能性。开发者可以从一系列高效的 IDE 和工具中作出选择。
Automation 与语言无关。但是,各个编译器或解释器必须支持 Automation。编译器将源代码转换成与 Automation 兼容的计算指令。例如,可以使用您语言中的字符串和数组类型,而无需考虑它们在 Automation 中的内部表示,即 BSTR 和 SAFEARRAY。可以用可执行程序 (Visual Basic、C++)或脚本 (JScript、VB Script) 表示控制 OpenOffice.org 的客户机程序。后者需要一个附加程序来运行脚本,如 Windows Scripting Host (WSH) 或 Internet Explorer。
尽管 UNO 与 Automation 和 COM 有一些相似之处,但 UNO 与它们不兼容。OpenOffice.org 通过部署一种由 Automation 桥 提供的桥接机制来使 UNO 与 Automation 配合工作。桥包含 UNO 服务,但是,要编写用于 OpenOffice.org 的 Automation 客户机,不必专门了解 UNO 服务。如果需要详细信息,请参阅 专业 UNO - UNO 语言绑定 - Automation 桥 - 桥服务。
不同的语言具有不同的功能。处理同一任务的方法存在差异,具体方法取决于使用的语言。提供了 Visual Basic、VB Script 和 JScript 示例。它们将显示一种语言何时需要特别处理或具有一个需要注意的属性。尽管 Automation 应该可以跨语言工作,但有一些细微之处需要通过桥或一种编码样式进行特别处理。例如,JScript 不能识别 out 参数,因此,必须使用 Array 对象。目前,已使用 C++、JScript、VBScript 和 Visual Basic 测试了桥,但也可以使用其他语言。
名称 Automation 桥 意味着使用了 Automation 技术。Automation 是通常称为 ActiveX 或 OLE 技术集中的一部分,因此,术语 OLE 桥会令人误解,因而应该避免使用。有时,桥称为 COM 桥,这也是错误的,因为桥处理的接口只有 IUnknown 和 Idispatch。
要求
Automation 技术只能在 Windows 平台(Windows 95、98、NT4、ME、2000 和 XP)上与 OpenOffice.org 一起使用。Macintosh OS 和 UNIX 上也有 COM 实现,但这些平台尚不支持 Automation。
使用 Automation 需要用类似于 COM 的方式创建对象,即使用像 VB 中的 CreateObject() 或 C 中的 CoCreateInstance() 之类的函数。这要求将 OpenOffice.org Automation 对象注册到 Windows 系统注册表。当在系统上安装办公软件时,就会执行此注册。如果没有进行注册,例如,由于二进制文件刚刚被复制到某个位置,则 Automation 客户机将不会正常工作或根本不工作。如果需要更多信息,请参阅 专业 UNO - UNO 语言绑定 - Automation 桥 - 服务管理器组件 。
快速浏览
以下示例说明如何通过 Automation 访问 OpenOffice.org 功能。请注意内插的注解。唯一的 Automation 特有调用是第一行中的 WScript.CreateObject(),其余调用都是 OpenOffice.org API 调用。列表末尾显示的是 Helper 函数 和 insertIntoCell()
'This is a VBScript example
'The service manager is always the starting point
'If there is no office running then an office is started up
Set objServiceManager= WScript.CreateObject("com.sun.star.ServiceManager")
'Create the CoreReflection service that is later used to create structs
Set objCoreReflection= objServiceManager.createInstance("com.sun.star.reflection.CoreReflection")
'Create the Desktop
Set objDesktop= objServiceManager.createInstance("com.sun.star.frame.Desktop")
'Open a new empty writer document
Dim args()
Set objDocument= objDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)
'Create a text object
Set objText= objDocument.getText
'Create a cursor object
Set objCursor= objText.createTextCursor
'Inserting some Text
objText.insertString objCursor, "The first line in the newly created text document." & vbLf, false
'Inserting a second line
objText.insertString objCursor, "Now we're in the second line", false
'Create instance of a text table with 4 columns and 4 rows
Set objTable= objDocument.createInstance( "com.sun.star.text.TextTable")
objTable.initialize 4, 4
'Insert the table
objText.insertTextContent objCursor, objTable, false
'Get first row
Set objRows= objTable.getRows
Set objRow= objRows.getByIndex( 0)
'Set the table background color
objTable.setPropertyValue "BackTransparent", false
objTable.setPropertyValue "BackColor", 13421823
'Set a different background color for the first row
objRow.setPropertyValue "BackTransparent", false
objRow.setPropertyValue "BackColor", 6710932
'Fill the first table row
insertIntoCell "A1","FirstColumn", objTable // insertIntoCell is a helper function, see below
insertIntoCell "B1","SecondColumn", objTable
insertIntoCell "C1","ThirdColumn", objTable
insertIntoCell "D1","SUM", objTable
objTable.getCellByName("A2").setValue 22.5
objTable.getCellByName("B2").setValue 5615.3
objTable.getCellByName("C2").setValue -2315.7
objTable.getCellByName("D2").setFormula"sum "
objTable.getCellByName("A3").setValue 21.5
objTable.getCellByName("B3").setValue 615.3
objTable.getCellByName("C3").setValue -315.7
objTable.getCellByName("D3").setFormula "sum "
objTable.getCellByName("A4").setValue 121.5
objTable.getCellByName("B4").setValue -615.3
objTable.getCellByName("C4").setValue 415.7
objTable.getCellByName("D4").setFormula "sum "
'Change the CharColor and add a Shadow
objCursor.setPropertyValue "CharColor", 255
objCursor.setPropertyValue "CharShadowed", true
'Create a paragraph break
'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant
objText.insertControlCharacter objCursor, 0 , false
'Inserting colored Text.
objText.insertString objCursor, " This is a colored Text - blue with shadow" & vbLf, false
'Create a paragraph break ( ControlCharacter::PARAGRAPH_BREAK).
objText.insertControlCharacter objCursor, 0, false
'Create a TextFrame.
Set objTextFrame= objDocument.createInstance("com.sun.star.text.TextFrame")
'Create a Size struct.
Set objSize= createStruct("com.sun.star.awt.Size") // helper function, see below
objSize.Width= 15000
objSize.Height= 400
objTextFrame.setSize( objSize)
' TextContentAnchorType.AS_CHARACTER = 1
objTextFrame.setPropertyValue "AnchorType", 1
'insert the frame
objText.insertTextContent objCursor, objTextFrame, false
'Get the text object of the frame
Set objFrameText= objTextFrame.getText
'Create a cursor object
Set objFrameTextCursor= objFrameText.createTextCursor
'Inserting some Text
objFrameText.insertString objFrameTextCursor, "The first line in the newly created text frame.", _
false
objFrameText.insertString objFrameTextCursor, _
vbLf & "With this second line the height of the frame raises.", false
'Create a paragraph break
'The second argument is a com::sun::star::text::ControlCharacter::PARAGRAPH_BREAK constant
objFrameText.insertControlCharacter objCursor, 0 , false
'Change the CharColor and add a Shadow
objCursor.setPropertyValue "CharColor", 65536
objCursor.setPropertyValue "CharShadowed", false
'Insert another string
objText.insertString objCursor, " That's all for now !!", false
On Error Resume Next
If Err Then
MsgBox "An error occurred"
End If
Sub insertIntoCell( strCellName, strText, objTable)
Set objCellText= objTable.getCellByName( strCellName)
Set objCellCursor= objCellText.createTextCursor
objCellCursor.setPropertyValue "CharColor",16777215
objCellText.insertString objCellCursor, strText, false
End Sub
Function createStruct( strTypeName)
Set classSize= objCoreReflection.forName( strTypeName)
Dim aStruct
classSize.createObject aStruct
Set createStruct= aStruct
End Function
必要时,此脚本会创建一个新文档并启动办公软件。该脚本还编写文字,创建并填充表格,使用不同的背景和笔的颜色。仅创建了一个 ActiveX 组件对象 com.sun.star.ServiceManager。然后使用该服务管理器创建其他对象,而这些对象又会提供别的对象。所有这些对象都提供功能,可通过调用相应函数和属性来使用这些功能。开发者需要了解哪些对象提供所需的功能以及如何获得这些对象。准备工作 一章中介绍了程序员可以使用的主要 OpenOffice.org 对象。
| Content on this page is licensed under the Public Documentation License (PDL). |