Search code examples
openoffice.orgopenoffice-basic

OpenOffice Basic macro vs openoffice API


I am planning to write openoffice macros using openoffice basic language. I am struggling to find the objects and methods related to openoffice documents. I googled and found some example codes and the API page openoffice.org/api but I could not locate where are the objects and methods in the api. For example read this line of code

oSheet=oDoc.Sheets.getByName("Sheet1")

There is no method/object in the name "sheets", getByName when i referred the API page (openoffice.org/api). Where should I look for?


Solution

  • Short answer:

    Sheets can be obtained by name, index or enumeration through the interface com.sun.star.sheet.XSpreadsheetDocument.

    oSheets = oDoc.getSheets().getByIndex(0)
    

    Explanation:

    Then what is oDoc.Sheets?

    To really understand the UNO API, it's best to use Java. Here is some code that you don't need to read, but I show it here because the results will be helpful.

    XSpreadsheetDocument xSpreadSheetDocument = UnoRuntime.queryInterface(
        XSpreadsheetDocument.class, xComponent);
    XSpreadsheets xSpreadsheets = xSpreadSheetDocument.getSheets();
    XPropertySet propSet = (XPropertySet)UnoRuntime.queryInterface(
        XPropertySet.class, xComponent);
    

    Using code like this, we can verify that getSheets() works in Java, but Sheets() does not. Nor is there any property named Sheets in the property set. In short, there is no Sheets in Java, so Sheets must not be part of the UNO API.

    Now, languages that are less strict than Java, such as Basic and Python, have various shortcuts to work with UNO. For one thing, they do not need queryInterface to get a specific object, because they don't care about the type.

    Another shortcut is that they provide properties that can be used as alternatives to the method of a particular interface. So, the method XSpreadsheetDocument.getSheets() gets a shortcut property Sheets. Similarly, the method XDocumentSupplier.getDocumentProperties() gets a shortcut property DocumentProperties. Both of these properties are members of oDoc when writing in Basic or Python.

    Finding Information:

    Searching through the API documentation is not easy. Before trying that, it's better to search for example code. Andrew Pitonyak's macro document contains a set of examples that cover many needs, and searching in Google will bring up example code from sites such as ask.libreoffice.org.

    Also, an introspection tool such as XrayTool or MRI is essential. Such tools give a list of all properties and methods available for an object, and since the list is flat, there is less need to dig into the interface of an inherited interface of another interface and so on.