将Open / Libre Office按钮链接到宏中的单元格和引用单元格

时间:2014-12-30 09:43:43

标签: macros libreoffice-calc libreoffice-basic

我想在自定义spredsheet(Open / Libre / Star Office)的某些单元格中添加一组标准宏[s]。

应使用放入相关单元格中的Form PushButton激活所述宏。

相对于"相关单元格的访问,我遇到了几个问题":

  1. 如果我尝试将一个PushButton锚定到Cell,它将转到A1而不是当前选定的单元格。
  2. 我可以将Basic片段连接到按钮,但我发现无法检索"相关的单元格" (即:包含按钮的单元格)。
  3. 我想要做的(作为第一个工作示例)是添加一个按钮来增加单元格的数值(可能禁用直接编辑;我希望每个按钮按下该值增加一个而不是否则改变细胞的方式。)

    这样的事情是否可能?

    任何示例(或指向文档的指针)非常欢迎。

    注意This question提供了一些有关如何解决VBA(Excel)问题的提示,但我没有找到[L | O | S] Office

1 个答案:

答案 0 :(得分:0)

您可以从处理程序中找到包含按钮的单元格,如下所示:

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