Difference between revisions of "Groovy UNO Extension"

From Apache OpenOffice Wiki
Jump to: navigation, search
(Overview: added text)
m (formatting)
Line 31: Line 31:
 
The following examples are in that style and other than that, similar to the Java way.
 
The following examples are in that style and other than that, similar to the Java way.
  
'''Setting the Cell Style property'''
+
'''Setting the Cell Style property'''<br />
 
The extension adds a setter method for CellStyle allowing what looks like property access to cellStyle. ToDo add getter method<br />
 
The extension adds a setter method for CellStyle allowing what looks like property access to cellStyle. ToDo add getter method<br />
 
Without Extension
 
Without Extension
Line 41: Line 41:
  
  
'''Using Enum Types'''
+
'''Using Enum Types'''<br />
 
The extension adds getter and setter methods for CellVertJustify allowing what looks like property access to vertJustify.<br />
 
The extension adds getter and setter methods for CellVertJustify allowing what looks like property access to vertJustify.<br />
 
Without extension
 
Without extension
Line 49: Line 49:
  
  
'''Setting the active sheet'''
+
'''Setting the active sheet'''<br />
 
Without Extension but using SpreadsheetDocHelper.groovy included with the [[OpenOffice_Gradle_Integration]] aoo-client template.
 
Without Extension but using SpreadsheetDocHelper.groovy included with the [[OpenOffice_Gradle_Integration]] aoo-client template.
 
     XModel xSpreadsheetModel = sdHelper.getModel()
 
     XModel xSpreadsheetModel = sdHelper.getModel()
Line 61: Line 61:
  
  
'''Get cell ranges'''
+
'''Get cell ranges'''<br />
 
Without extension
 
Without extension
 
     XCellRangesQuery xCellQuery = UnoRuntime.queryInterface(XCellRangesQuery.class, xSpreadsheet)
 
     XCellRangesQuery xCellQuery = UnoRuntime.queryInterface(XCellRangesQuery.class, xSpreadsheet)
Line 69: Line 69:
 
     XSheetCellRanges xFormulaCells = xSpreadsheet.getCellRanges(CellFlags.FORMULA)
 
     XSheetCellRanges xFormulaCells = xSpreadsheet.getCellRanges(CellFlags.FORMULA)
  
'''Use of XEnumerationAccess'''
+
'''Use of XEnumerationAccess'''<br />
 
Without extension
 
Without extension
 
     XEnumerationAccess xFormulas = xFormulaCells.getCells()
 
     XEnumerationAccess xFormulas = xFormulaCells.getCells()
Line 89: Line 89:
 
     }
 
     }
  
'''Create a new cell range container'''
+
'''Create a new cell range container'''<br />
 
Without Extension
 
Without Extension
 
     com.sun.star.lang.XMultiServiceFactory xDocFactory = UnoRuntime.queryInterface(
 
     com.sun.star.lang.XMultiServiceFactory xDocFactory = UnoRuntime.queryInterface(
Line 101: Line 101:
  
  
'''Query the list of filled cells from a range container'''
+
'''Query the list of filled cells from a range container'''<br />
 
Without Extension
 
Without Extension
print("All filled cells: ")
+
    print("All filled cells: ")
 
     com.sun.star.container.XEnumerationAccess xCellsEA = xRangeCont.getCells()
 
     com.sun.star.container.XEnumerationAccess xCellsEA = xRangeCont.getCells()
 
     com.sun.star.container.XEnumeration xEnum = xCellsEA.createEnumeration()           
 
     com.sun.star.container.XEnumeration xEnum = xCellsEA.createEnumeration()           

Revision as of 01:18, 6 January 2016

Overview

The goal of the Groovy UNO Extension is to provide convenience methods and 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.

Getting Started

ToDo

Usage

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 SCalc.java 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);
   xCell.setFormula("Sample");


Groovy Extension way

   XSpreadsheet xSheet = myDoc.getSheetByIndex(0)
   xSheet.setTextToCell(1,0,"Sample")

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

   xCellProps.setPropertyValue("VertJustify", com.sun.star.table.CellVertJustify.TOP)

With extension

   xCell.vertJustify = com.sun.star.table.CellVertJustify.TOP


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)
   xSpreadsheetView.setActiveSheet(xSpreadsheet)

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

   XSpreadsheetView xSpreadsheetView = sdHelper.getSpreadsheetView()
   xSpreadsheetView.setActiveSheet(xSpreadsheet)


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

   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

   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
Without Extension

   com.sun.star.lang.XMultiServiceFactory xDocFactory = UnoRuntime.queryInterface(
       com.sun.star.lang.XMultiServiceFactory.class, xSpreadsheetDocument)
   com.sun.star.sheet.XSheetCellRangeContainer xRangeCont = UnoRuntime.queryInterface(
       com.sun.star.sheet.XSheetCellRangeContainer.class,
       xDocFactory.createInstance("com.sun.star.sheet.SheetCellRanges"));

With extension

   XSheetCellRangeContainer xRangeCont = xSpreadsheetDocument.rangeContainer


Query the list of filled cells from a range container
Without Extension

   print("All filled cells: ")
   com.sun.star.container.XEnumerationAccess xCellsEA = xRangeCont.getCells()
   com.sun.star.container.XEnumeration xEnum = xCellsEA.createEnumeration()          
   while (xEnum.hasMoreElements()) {
       Object aCellObj = xEnum.nextElement()
       com.sun.star.sheet.XCellAddressable xAddr = UnoRuntime.queryInterface(
           com.sun.star.sheet.XCellAddressable.class, aCellObj)
       com.sun.star.table.CellAddress aAddr = xAddr.getCellAddress()
       println(sdHelper.getCellAddressString(aAddr.Column, aAddr.Row) + " ")
   }

With extension and using a closure to iterate through the 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