元素访问
我们在章节 2.4 准备工作 - 如何在 OpenOffice.org 中获得对象 中已了解到也可以通过元素访问方法来提供 对象集。最重要的三种元素访问接口是 com.sun.star.container.XNameContainer、 com.sun.star.container.XIndexContainer 和 com.sun.star.container.XEnumeration。
这三种元素访问接口示范了 OpenOffice.org API 的细颗粒状接口如何允许一致的对象设计。
这三种接口都继承自 XElementAccess
,例如它们都包括以下方法:
type getElementType() boolean hasElements()
来找到有关元素集的基本信息。hasElements()
方法可以确定元素集中是否含有元素以及含有的类型。在 Java 和 C++ 中,除 Java UNO 和 C++ UNO 引用外,可以通过 com.sun.star.uno.Type
来获取有关 UNO 类型的信息。
com.sun.star.container.XIndexContainer 和 com.sun.star.container.XNameContainer 接口具有平行设计。两种接口都用 UML 表示法表示。
XIndexAccess/XNameAccess
接口用于获取元素。XIndexReplace/XNameReplace
接口允许您替换现有元素而无需更改元素集中的元素数,而 XIndexContainer/XNameContainer
接口允许您通过插入和删除元素来增加和减少元素数。
很多已命名或索引的对象集不支持 XIndexContainer
或 XNameContainer
的整个继承分层,因为由每个子类添加的功能对所有元素集不是始终具有逻辑性。
XEumerationAccess
接口的工作方式与 XElementAccess
接口下已命名和被索引的容器的工作方式不同。
XEnumerationAccess
不提供 XNameAccess
和 XIndexAccess
等单独元素,但它会创建对象的枚举,此枚举类型有方法转到存在的下一个元素。
对象集有时支持所有的元素访问方法,有些对象集也支持仅名称、索引或枚举访问。请始终在 API 引用中查看各种类型,以确定哪些访问方法可用。
例如,com.sun.star.sheet.XSpreadsheetDocument 接口的 getSheets()
方法被指定为返回继承
自 XNameContainer
的 com.sun.star.sheet.XSpreadsheets 接口。此外,API 引用还告诉您所提供的对象支持 com.sun.star.sheet.Spreadsheets 服务,该服务可定义包括 XSpreadsheets
在内的其他元素访问接口
下面提供了如何使用XEnumerationAccess
、 XIndexAccess
和 XEnumerationAccess
的示例。
名称访问
按名称分发元素的基本接口是 com.sun.star.container.XNameAccess 接口。它包含三种方法:
any getByName( [in] string name) sequence< string > getElementNames() boolean hasByName( [in] string name)
在上面的 FirstLoadComponent 示例中,方法 getSheets()
返回了一个
com.sun.star.sheet.XSpreadsheets 接口,该接口继承自 XNameAccess
。因此,您可以使用 getByName()
按名称从 XSpreadsheets
容器中获取 "MySheet" 工作表:
XSpreadsheets xSpreadsheets = xSpreadsheetDocument.getSheets(); Object sheet = xSpreadsheets.getByName("MySheet"); XSpreadsheet xSpreadsheet = (XSpreadsheet)UnoRuntime.queryInterface( XSpreadsheet.class, sheet); // use XSpreadsheet interface to get the cell A1 at position 0,0 and enter 42 as value XCell xCell = xSpreadsheet.getCellByPosition(0, 0);
由于 getByName()
返回一个 any,所以在您调用电子表格对象中的方法之前,需要使用
AnyConverter.toObject()
和/或 UnoRuntime.queryInterface()<(code>。
索引访问
按索引分发元素的接口是 com.sun.star.container.XIndexAccess 接口。它包含两种方法:
any getByIndex( [in] long index) long getCount()
FirstLoadComponent 示例允许演示 <code>XIndexAccess。API 引用告诉我们由 getSheets()
返回的服务是 com.sun.star.sheet.Spreadsheet 服务,它不仅支持 com.sun.star.sheet.XSpreadsheets 接口,而且还支持 XIndexAccess
接口。因此,通过查询
XIndexAccess
接口(通过 xSpreadsheets
变量),本可以按索引来访问工作表,而不是仅仅按名称来访问:
XIndexAccess xSheetIndexAccess = (XIndexAccess)UnoRuntime.queryInterface( XIndexAccess.class, xSpreadsheets); Object sheet = XSheetIndexAccess.getByIndex(0);
枚举访问
接口 com.sun.star.container.XEnumerationAccess 可以创建适用于整个对象集的枚举。它使用一个方法:
com.sun.star.container.XEnumeration createEnumeration()
从 createEnumeration()
中获得的枚举对象支持接口
com.sun.star.container.XEnumeration。使用此接口,可以连续不断地将元素分离出枚举,直到没有元素为止。XEnumeration
提供以下方法:
booleanhasMoreElements() any nextElement()
这意味着将构建循环,如:
while (xCells.hasMoreElements()) { Object cell = xCells.nextElement(); // do something with cell }
例如,在电子表格中可以找到含有公式的单元格。生成的单元格集作为 XEnumerationAccess
提供。
查询含有公式的单元格的接口是 com.sun.star.sheet.XCellRangesQuery,它定义了(除其他方法以外)一种方法
XSheetCellRanges queryContentCells(short cellFlags)
此方法用于查询含有 com.sun.star.sheet.CellFlags 常数组中定义的内容的单元格。单元格标记中的一个标记是 FORMULA。我们从 queryContentCells() 收到包含
com.sun.star.sheet.XSheetCellRanges 接口的对象,此接口包含以下方法:
XEnumerationAccessgetCells() StringgetRangeAddressesAsString() sequence< com.sun.star.table.CellRangeAddress > getRangeAddresses()
getCells()
方法可以使用 XEnumerationAccess<code> 列出 FirstLoadComponent 示例的电子表格文档中所有公式单元格和含有的公式。
XCellRangesQuery xCellQuery = (XCellRangesQuery)UnoRuntime.queryInterface( XCellRangesQuery.class, sheet); XSheetCellRanges xFormulaCells = xCellQuery.queryContentCells( (short)com.sun.star.sheet.CellFlags.FORMULA); XEnumerationAccess xFormulas = xFormulaCells.getCells(); XEnumeration xFormulaEnum = xFormulas.createEnumeration(); while (xFormulaEnum.hasMoreElements()) { Object formulaCell = xFormulaEnum.nextElement(); // do something with formulaCell xCell = (XCell)UnoRuntime.queryInterface(XCell.class, formulaCell); XCellAddressable xCellAddress = (XCellAddressable)UnoRuntime.queryInterface( XCellAddressable.class, xCell); System.out.print("Formula cell in column " + xCellAddress.getCellAddress().Column + ", row " + xCellAddress.getCellAddress().Row + " contains " + xCell.getFormula()); }
Content on this page is licensed under the Public Documentation License (PDL). |