【问题标题】:link Open/Libre Office button to cell and reference cell in macro将 Open/Libre Office 按钮链接到宏中的单元格和引用单元格
【发布时间】:2014-12-30 09:43:43
【问题描述】:
我想向自定义电子表格(Open/Libre/Star Office)的某些单元格添加 [一组] 标准化宏。
应该使用拖放到相关单元格中的表单按钮来激活所述宏。
我遇到了几个与“相关单元格”的访问有关的问题:
- 如果我尝试将按钮锚定到单元格,它将转到 A1 而不是当前选定的单元格。
- 我可以将 Basic 片段连接到按钮,但我发现无法检索“相关单元格”(即:包含按钮的单元格)。
我想要做的(作为第一个工作示例)是添加一个按钮来增加单元格的数值(可能禁用直接编辑;我希望该值在每次按下按钮时增加一个并且不以其他方式更改单元格)。
这样的事情有可能吗?
任何示例(或指向文档的指针)非常欢迎。
注意:This question 给出了一些关于如何在 VBA (Excel) 中解决问题的提示,但我没有找到 [L|O|S]Office
【问题讨论】:
标签:
macros
libreoffice-calc
uno
libreoffice-basic
【解决方案1】:
您可以从处理程序中找到包含按钮的单元格,如下所示:
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