Ticket #834 (accepted task)

Opened 7 years ago

Last modified 6 years ago

Spreadsheet bindings for results table

Reported by: curtis Owned by: dscho
Priority: minor Milestone: imagej-2.5.0
Component: Plugins Version:
Severity: non-issue Keywords:
Cc: Blocked By:
Blocking:

Description

We can use  JACOB to provide an IJ2 plugin that integrates the ImageJ results table with an Excel spreadsheet. We can do the same for LibreOffice using  its UNO API.

Change History

comment:1 Changed 7 years ago by dscho

  • Status changed from new to accepted

A cursory look at LibreOffice's API makes me think that it might not be trivial...

comment:2 Changed 6 years ago by dscho

Another cursory look (as sort of reward for finally integrating the ImageJ Launcher into IJ2) shows that the documentation is now much better and the API -- while a reminescence of the complexity of COM -- is not half that bad.

The only caveat seems to be that LibreOffice must be running and the Java support turned on in Tools>Options>LibreOffice>Java.

A simple example for adding and populating a spreadsheet is then:

/**
 * Example class to open a spreadsheet in LibreOffice using the UNO Runtime
 *
 * On Debian, you need the ure and the libreoffice-java-common packages.
 */

import com.sun.star.beans.PropertyValue;
import com.sun.star.frame.XComponentLoader;
import com.sun.star.lang.XComponent;
import com.sun.star.lang.XMultiComponentFactory;
import com.sun.star.sheet.XSheetCellCursor;
import com.sun.star.sheet.XSpreadsheetDocument;
import com.sun.star.sheet.XSpreadsheet;
import com.sun.star.sheet.XSpreadsheets;
import com.sun.star.table.XCell;
import com.sun.star.uno.Any;
import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.XComponentContext;

public class SSpreadsheet  {

        public static void main(String args[]) throws Exception {
                XComponentContext context =
                        com.sun.star.comp.helper.Bootstrap.bootstrap();

                XSpreadsheetDocument document = openSpreadsheet(context);
                XSpreadsheets sheets = document.getSheets();
                Any any = (Any)sheets.getByName("Sheet1");
                XSpreadsheet sheet = (XSpreadsheet)any.getObject();

                XSheetCellCursor cursor = sheet.createCursor();
                XCell cell = sheet.getCellByPosition(0, 1);
                cell.setValue(2.0);
                cell = sheet.getCellByPosition(1, 0);
                cell.setFormula("Hello, you!");
        }


        public static XSpreadsheetDocument
                        openSpreadsheet(XComponentContext context)
                        throws Exception {
                XMultiComponentFactory serviceManager =
                        context.getServiceManager();
                Object desktop = serviceManager
                        .createInstanceWithContext("com.sun.star.frame.Desktop",
                                context);
                XComponentLoader loader = (XComponentLoader)UnoRuntime
                        .queryInterface(XComponentLoader.class, desktop);
                XComponent component = loader
                        .loadComponentFromURL("private:factory/scalc", "_blank",
                                0, new PropertyValue[0]);
                return (XSpreadsheetDocument)UnoRuntime
                        .queryInterface(XSpreadsheetDocument.class, component);
        }
}

This class can be compiled against unoil.jar, ridl.jar and juh.jar (it needs all three of them). On Debian systems, unoil.jar comes from the libreoffice-java-common package while ridl.jar and juh.jar come from the ure package.

A nice collection of LibreOffice/Java examples can be found here:
 http://api.libreoffice.org/examples/java/

Last edited 6 years ago by dscho (previous) (diff)
Note: See TracTickets for help on using tickets.