Groovy UNO Extension

From Apache OpenOffice Wiki
Revision as of 11:16, 5 January 2016 by Cmarcum (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search



Getting Started



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. A lot of the examples are from that is included with the AOO SDK.
Leaving out try/catch for brevity and assumes we have a reference to XSpreadsheetDocument myDoc..

Get the first sheet in the spreadsheet document and insert data into a cell

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 other not using semi-colons.
The folowing examples are in that style and other than that, similar to the Java way.

Setting the Cell Style property 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 MORE CODE TO GET MODEL ... XModel xSpreadsheetModel = sdHelper.getModel()

XController xSpreadsheetController = xSpreadsheetModel.getCurrentController()

XSpreadsheetView xSpreadsheetView = UnoRuntime.queryInterface(XSpreadsheetView.class,


XSpreadsheetView xSpreadsheetView = sdHelper.getSpreadsheetView()


with extension XSpreadsheetView xSpreadsheetView = sdHelper.getSpreadsheetView()


get cell ranges XCellRangesQuery xCellQuery = UnoRuntime.queryInterface(XCellRangesQuery.class, xSpreadsheet)

XSheetCellRanges xFormulaCells = xCellQuery.queryContentCells((short)CellFlags.FORMULA)

with extension XSheetCellRanges xFormulaCells = xSpreadsheet.getCellRanges(CellFlags.FORMULA)

// example for use of XEnumerationAccess 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 " +
       + ", row " + xCellAddress.getCellAddress().Row
       + " contains " + xCell.getFormula())

with extension 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 xDocFactory = UnoRuntime.queryInterface(, xSpreadsheetDocument)
 xRangeCont = UnoRuntime.queryInterface(,

with extension XSheetCellRangeContainer xRangeCont = xSpreadsheetDocument.rangeContainer

// Query the list of filled cells from a range container

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

// example for using a closure to iterate through list of cells 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}")


Personal tools