Groovy UNO Extension

From Apache OpenOffice Wiki
Jump to: navigation, search


This is an Apache Groovy language extension of the Java UNO API's and should not be confused with GroovyForOpenOffice which is an OpenOffice extension added to OpenOffice itself. The artifact of this extension is a Java jar file that when used in a Groovy script or class adds convenience methods to the regular Java UNO API's.

The goal of the Groovy UNO Extension is to allow UNO programming that is less verbose than using the Java UNO API's alone.

These methods are implemented using Groovy Extension Modules. An extension module allows you to add new methods to existing classes, including classes which are precompiled, like classes from the JDK or in this case Java UNO classes. These new methods, unlike those defined through a metaclass or using a category, are available globally.

Aside from a few general methods, initial efforts have been on enhancing the spreadsheet API's and future work will be on enhancing the other applications.

Getting Started

The Extension is a Gradle based project and information about checking it out of Apache SVN can be found on the OpenOffice_Gradle_Integration page.


The best way to explain the differences between the Java UNO API's and using Groovy with and without the extension is with some example code. Many of the examples are spreadsheet examples are from that is included with the AOO SDK.
Get an XComponentLoader
Java way (assumes XComponentContext xComponentContext reference)

XMultiComponentFactory mxRemoteServiceManager = null
    XComponentLoader aLoader = null
    mxRemoteServiceManager = xComponentContext.getServiceManager()
    aLoader = UnoRuntime.queryInterface(
        XComponentLoader.class, mxRemoteServiceManager.createInstanceWithContext(
        "", self))

Groovy Extension way

XComponentLoader aLoader = mxRemoteContext.componentLoader

The UnoRuntime.queryInterface(ReturnObject.class, FromObject) method can be replaced with the new FromObject.guno(ReturnObject.class) method.
Java way (assumes we have a reference to XSpreadsheetDocument myDoc..)

XSpreadsheets xSheets = myDoc.getSheets() ;
XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(XIndexAccess.class, xSheets);
xSheet = (XSpreadsheet) UnoRuntime.queryInterface(XSpreadsheet.class, oIndexSheets.getByIndex(0));

Groovy Extension way

XSpreadsheets xSheets = myDoc.sheets
XIndexAccess oIndexSheets = xSheets.guno(XIndexAccess.class)
xSheet = oIndexSheets.getByIndex(0).guno(XSpreadsheet.class)

Property Access
getPropertyValue and setPropertyValue methods can now be getAt and putAt.

Get the first sheet in the spreadsheet document and insert data into a cell
The example leaves out the try/catch for brevity and assumes we have a reference to XSpreadsheetDocument myDoc..
Java way

XSpreadsheets xSheets = myDoc.getSheets() ;
XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(
    XIndexAccess.class, xSheets);
xSheet = (XSpreadsheet) UnoRuntime.queryInterface(
   XSpreadsheet.class, oIndexSheets.getByIndex(0));
xCell = xSheet.getCellByPosition(1,0);

Groovy Extension way

XSpreadsheet xSheet = myDoc.getSheetByIndex(0)

Using Groovy without the extension allows removing the Interface cast on the right side and not using semi-colons.
The following examples are in that style and other than that, similar to the Java way.

Setting the Cell Style property
The extension adds a setter method for CellStyle allowing what looks like property access to cellStyle. (ToDo add getter method)
Without Extension

XPropertySet xCellProps = UnoRuntime.queryInterface(XPropertySet.class, xCell)
xCellProps.setPropertyValue("CellStyle", "Result")

With extension

   xCell.cellStyle = "Result"

Using Enum Types
The extension adds getter and setter methods for CellVertJustify allowing what looks like property access to vertJustify.
Without extension


With extension

xCell.vertJustify =

Setting the active sheet
Without Extension but using SpreadsheetDocHelper.groovy included with the OpenOffice_Gradle_Integration aoo-client template.

XModel xSpreadsheetModel = sdHelper.getModel()
XController xSpreadsheetController = xSpreadsheetModel.getCurrentController()
XSpreadsheetView xSpreadsheetView = UnoRuntime.queryInterface(XSpreadsheetView.class, xSpreadsheetController)

With extension and a SpreadsheetDocHelper.groovy method to get the XSpreadsheetView directly

XSpreadsheetView xSpreadsheetView = sdHelper.getSpreadsheetView()

Get cell ranges
Without extension

XCellRangesQuery xCellQuery = UnoRuntime.queryInterface(XCellRangesQuery.class, xSpreadsheet)
XSheetCellRanges xFormulaCells = xCellQuery.queryContentCells((short)CellFlags.FORMULA)

With extension

XSheetCellRanges xFormulaCells = xSpreadsheet.getCellRanges(CellFlags.FORMULA)

Use of XEnumerationAccess
Without extension we get an Enumeration and use it iterate through Cells

XEnumerationAccess xFormulas = xFormulaCells.getCells()
XEnumeration xFormulaEnum = xFormulas.createEnumeration()
while (xFormulaEnum.hasMoreElements()) {
    Object formulaCell = xFormulaEnum.nextElement()
    xCell = UnoRuntime.queryInterface(XCell.class, formulaCell)
    XCellAddressable xCellAddress = UnoRuntime.queryInterface(XCellAddressable.class, xCell)
    println("Formula cell in column " +
        xCellAddress.getCellAddress().Column + ", row " + xCellAddress.getCellAddress().Row
        + " contains " + xCell.getFormula())

With extension we can use a List provided by a new method and a closure to iterate through each cell

XCell[] cellList = xFormulaCells.cellList
cellList.each() {println("Formula cell in column ${it.address.Column}, " + 
    "row ${it.address.Row} contains ${it.formula}")

Create a new cell range container, add all cells that are filled, and iterate through them
Without Extension xDocFactory = UnoRuntime.queryInterface(, xSpreadsheetDocument) xRangeCont = UnoRuntime.queryInterface(,
xRangeCont.addRangeAddresses(xCellRanges.rangeAddresses, false)
print("All filled cells: ") xCellsEA = xRangeCont.getCells() xEnum = xCellsEA.createEnumeration()          
while (xEnum.hasMoreElements()) {
    Object aCellObj = xEnum.nextElement() xAddr = UnoRuntime.queryInterface(, aCellObj) aAddr = xAddr.getCellAddress()
    println(sdHelper.getCellAddressString(aAddr.Column, aAddr.Row) + " ")

With extension

XSheetCellRangeContainer xRangeCont = xSpreadsheetDocument.rangeContainer
XSheetCellRanges xCellRanges = xSpreadsheet.getCellRanges(1023)
XCell[] cellList2 = xRangeCont.cellList
print("All filled cells: ")
cellList2.each() {println("Formula cell in column ${it.address.Column}, " + 
    "row ${it.address.Row} contains ${it.formula}")

Release Candidate Testing

For release candidate testing procedure see Groovy_Uno_Release_Testing

Release Candidate Preparation

For release candidate preparation see Groovy_Uno_Release_Preparation

Personal tools