OOo/LibreOffice UNO / Java: How to get calling spreadsheet cell of a calc function?

It looks like you want to register a listener to a spreadsheet component. To satisfy your goal, you could add the listener to the spreadsheet object it self, or to another nested object that implements an interface that supports an add.+EventListener() method.

Below is a pair (broadcaster/listener) that I can think you could use in your project:
XDocumentEventBroadcaster/XDocumentEventListener

The UNO event model is explained here: https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Event_Model

Below are examples of how these listeners are used.

    //////////////////////////////////////////////////////////////////// 
    // Add document window listeners. 
    //////////////////////////////////////////////////////////////////// 

    System.out.println("WriterDoc: Add window listeners."); 

    // Example of adding a document displose listener so the application 
    // can know if the user manually exits the Writer window. 

    document.addEventListener(new XEventListener() { 
        public void disposing(EventObject e) { 
            System.out.println( 
                    "WriterDoc (Event Listener): The document window is closing."); 
        } 
    }); 

    // Example of adding a window listener so the application can know 
    // when the document becomes initially visible (in the case of this 
    // implementation, we will manually set it visible below after we 
    // finish building it). 

    window.addWindowListener(new XWindowListener() { 
        public void windowShown(com.sun.star.lang.EventObject e) { 
            System.out.println( 
                    "WriterDoc (Window listener): The document window has become visible."); 
        } 
        public void windowHidden(com.sun.star.lang.EventObject e) { } 
        public void disposing(com.sun.star.lang.EventObject e) { } 
        public void windowResized(com.sun.star.awt.WindowEvent e) { } 
        public void windowMoved(com.sun.star.awt.WindowEvent e) { } 
    }); 

Also, the service SheetCellRange supports the interface XModifyBroadcaster. Maybe you could get the desired behavior if you registered a XModifyListener object to it. The object would implement the ‘modified’ method, which receives an EventObject when called. I believe you can get who the caller is from the source property of the EventObject. If the source turns out to be the whole SheetCellRange, you could try to loop through all the cells you wish that be monitored, and add a XModifyListener to each. The SheetCell service also supports the XModifyBroadcaster interface .

Example of use of the XModifyBroadcaster from a CellRange:
http://openoffice.2283327.n4.nabble.com/Re-How-to-get-the-XModifyBroadcaster-from-Cell-CellRange-Table-td2771959.html

Cheers!

Leave a Comment