34

In an UNO extension for OpenOffice/LibreOffice Calc (Spreadsheet), written in Java, how can you determine the calling cell inside the implementation of a UDF (spreadsheet function)?

Remarks

  • In Excel/VBA this is possible via Application.Caller
  • The main motivation to get the caller is logging/tracing/debugging, i.e., view the calling cell as part of a stack trace.
  • It should be possible to obtain this information, since built-in functions like "ROW()" and "COLUMN()" do have some knowledge of the calling cell.
  • An application where this possibility is used (for Excel) is Obba, an object handler for spreadsheets. Here the "control panel" provides a list of (Java) exceptions including the calling cell, i.e., the cell is part of the stack trace. See the following screenshot:

Obba Control Panel showing exceptions by spreadsheet cell of calling function

This is also a feature request on the Apache OpenOffice Bugzilla

4

1 回答 1

1

看起来您想向电子表格组件注册一个侦听器。为了满足您的目标,您可以将侦听器添加到它自己的电子表格对象,或者添加到另一个实现支持 add.+EventListener() 方法的接口的嵌套对象。

下面是我认为您可以在项目中使用的一对(广播/监听器): XDocumentEventBroadcaster / XDocumentEventListener

UNO 事件模型在这里解释:https ://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Event_Model

以下是如何使用这些侦听器的示例。

    //////////////////////////////////////////////////////////////////// 
    // 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) { } 
    }); 

此外,服务 SheetCellRange 支持接口 XModifyBroadcaster。如果您向其注册了 XModifyListener 对象,也许您可​​以获得所需的行为。该对象将实现“修改”方法,该方法在调用时接收一个 EventObject。我相信你可以从 EventObject 的 source 属性中知道调用者是谁。如果源结果是整个 SheetCellRange,您可以尝试遍历您希望被监视的所有单元格,并为每个单元格添加一个 XModifyListener。SheetCell 服务还支持 XModifyBroadcaster 接口。

从 CellRange 使用 XModifyBroadcaster 的示例:http: //openoffice.2283327.n4.nabble.com/Re-How-to-get-the-XModifyBroadcaster-from-Cell-CellRange-Table-td2771959.html

干杯!

于 2014-04-09T22:53:18.813 回答