Macros 将Open/Libre Office按钮链接到宏中的单元格和参考单元格
我想在自定义存储表(Open/Libre/Star Office)的某些单元格中添加一组标准化宏 应使用放入相关单元格的窗体按钮激活所述宏 我遇到了几个与“相关单元”访问相关的问题:Macros 将Open/Libre Office按钮链接到宏中的单元格和参考单元格,macros,libreoffice-calc,uno,libreoffice-basic,Macros,Libreoffice Calc,Uno,Libreoffice Basic,我想在自定义存储表(Open/Libre/Star Office)的某些单元格中添加一组标准化宏 应使用放入相关单元格的窗体按钮激活所述宏 我遇到了几个与“相关单元”访问相关的问题: 如果我尝试锚定到单元格按钮,它将转到A1,而不是当前选定的单元格 我可以将一个基本片段连接到按钮,但我找不到检索“relevent单元格”(即:包含按钮的单元格)的方法 我试图做的(作为第一个工作示例)是添加一个按钮以增加单元格的数值(可能禁用直接编辑;我希望该值在每次按下按钮时增加一个,并且无法以其他方式更改单元
注意:提供了一些关于如何在VBA(Excel)中解决问题的提示,但我在[L|O|S]Office中找不到任何提示您可以从处理程序中找到包含按钮的单元格,如下所示:
Sub ButtonHandler(oEvent)
Dim sControlName$
Dim oSheet
Dim nCount As Long
Dim i As Long
Dim oPage
Dim oShape
Dim oAnchor
sControlName = oEvent.source.model.Name
oSheet = thiscomponent.currentcontroller.activesheet
nCount = oSheet.drawpage.count
oPage = oSheet.drawpage
For i = 0 To nCount - 1
oShape = oPage.getbyindex(i)
'oControlShape = oPage.getbyindex(i).control
If (oShape.supportsService("com.sun.star.drawing.ControlShape")) Then
If oShape.control.Name = sControlName Then
oAnchor = oShape.anchor
If (oAnchor.supportsService("com.sun.star.sheet.SheetCell")) Then
Print "Button is anchored in cell: " + oAnchor.AbsoluteName
Exit For
End If
End If
End If
Next i
End Sub
我知道,它不漂亮,是吗?我添加了重大错误检查。如果您想知道单击按钮时哪个单元格处于活动状态,可以调用此例程
Sub RetrieveTheActiveCell()
Dim oOldSelection 'The original selection of cell ranges
Dim oRanges 'A blank range created by the document
Dim oActiveCell 'The current active cell
Dim oConv 'The cell address conversion service
Dim oDoc
oDoc = ThisComponent
REM store the current selection
oOldSelection = oDoc.CurrentSelection
REM Create an empty SheetCellRanges service and then select it.
REM This leaves ONLY the active cell selected.
oRanges = oDoc.createInstance("com.sun.star.sheet.SheetCellRanges")
oDoc.CurrentController.Select(oRanges)
REM Get the active cell!
oActiveCell = oDoc.CurrentSelection
oConv = oDoc.createInstance("com.sun.star.table.CellAddressConversion")
oConv.Address = oActiveCell.getCellAddress
Print oConv.UserInterfaceRepresentation
print oConv.PersistentRepresentation
REM Restore the old selection, but lose the previously active cell
oDoc.CurrentController.Select(oOldSelection)
End Sub