在LibreOffice Calc的宏中,如何获取鼠标单击所指向的单元格地址?

人工智能 2026-07-11
Global oMouseHandler As Object

' --- Register the listener (run this to start) ---
Sub RegisterMouseHandler
    Dim oController As Object
    oController = ThisComponent.CurrentController

    ' Create the listener object
    oMouseHandler = CreateUnoListener("ML_", "com.sun.star.awt.XEnhancedMouseClickHandler")

    ' Add it to the controller
    oController.addEnhancedMouseClickHandler(oMouseHandler)
    MsgBox "Mouse Listener Registered"
End Sub

' --- Unregister the listener (run this to stop) ---
Sub UnregisterMouseHandler
'    On Error Resume Next
    ThisComponent.CurrentController.removeEnhancedMouseClickHandler(oMouseHandler)
    MsgBox "Mouse Listener Removed"
End Sub

' --- Listener Methods ---
Function ML_mousePressed(oEvent)
    ML_mousePressed = False
End Function

Function ML_mouseReleased(oEvent)
    Dim sTarget As String
    ' Access the target cell
    If HasUnoInterfaces(oEvent.Target, "com.sun.star.sheet.XCell") Then
        sTarget = oEvent.Target.AbsoluteName
        MsgBox "Cell clicked: " & sTarget & " at X: " & oEvent.X & " Y: " & oEvent.Y
    End If
    ML_mouseReleased = False
End Function

Sub ML_disposing(oEvent)
    ' Required, but can be left empty
End Sub

这个AI生成的宏应该把目标单元格对象和单元格地址作为其中一个属性返回给我,但它没有做到!

在Macro Basic IDE调试函数 ML_mouseReleased 时,我可以看到对象 oEvent 及其 Target 属性,但宏无法访问它。oEvent.Target 似乎被隐藏,或者缺少某个UNO接口。这里到底出了什么问题?

我在Windows 11上使用LibreOffice版本25.8.3.2 (X86_64)。

解决方案

解决方法是把函数 'ML_mouseReleased' 中的if语句注释掉。条件

HasUnoInterfaces(oEvent.Target, "com.sun.star.sheet.XCell")

似乎不合适或构造有误……

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章