首页 文章

OOo / LibreOffice UNO / Java:如何调用calc函数的电子表格单元格?

提问于
浏览
34

在用Java编写的OpenOffice / LibreOffice Calc(Spreadsheet)的UNO扩展中,如何确定UDF(电子表格函数)实现中的调用单元?

Remarks

  • 在Excel / VBA中,这可以通过 Application.Caller 实现

  • 获取调用者的主要动机是记录/跟踪/调试,即将调用单元视为堆栈跟踪的一部分 .

  • 应该可以获得此信息,因为像"ROW()"和"COLUMN()"这样的内置函数确实具有调用单元的一些知识 .

  • 使用此可能性的应用程序(对于Excel)是Obba,电子表格的对象处理程序 . 这里"control panel"提供(Java)异常列表,包括调用单元,即,单元是堆栈跟踪的一部分 . 请参阅以下屏幕截图:

Obba Control Panel showing exceptions by spreadsheet cell of calling function

这也是Apache OpenOffice Bugzilla上的功能请求

1 回答

  • 1

    看起来您想要将监听器注册到电子表格组件 . 为了满足您的目标,您可以将侦听器添加到它自己的电子表格对象中,或者添加到另一个实现支持添加的接口的嵌套对象 . 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对象,也许可以获得所需的行为 . 该对象将实现'modified'方法,该方法在调用时接收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

    干杯!

相关问题