在 LibreOffice Calc 中插入带有基本宏的单元格

Insert a cell with a Basic Macro in LibreOffice Calc

我正在尝试插入一个以范围中的第一个单元格开头的单元格(通过 Document 的活动 Sheet 的 .getCellRangeByName() 方法)。

我发现了如何使用 OpenOffice 库 (.uno:InsertCell) 中的 Dispatcher 执行此操作,但如果可能,我更愿意使用不需要 Dispatcher 的东西。

我计划连接到按钮的示例代码...

Sub AddManualBalance(EntryDate As Date, EntryAmount As Currency)

    Dim Doc As Object
    Dim Sheet As Object
    Doc = ThisComponent
    If Doc Is Nothing Then
        Return
    EndIf
    Sheet = Doc.getCurrentController().getActiveSheet()
    If Sheet Is Nothing Then
        Return
    EndIf

    Dim TargetCells As Object
    TargetCells = Sheet.getCellRangeByName("B9:C9");

    // insert a cell in both the B and C columns at position 9,
    // then move all other cells down

    // add my EntryDate as a value to the new cell in B column
    // add my EntryAmount as a value to the new cell in C column

End Sub

在此先感谢您的帮助!

P.S。我真的不喜欢 Basic,但是对于电子表格和办公应用程序自动化,Basic 似乎是首选语言。有什么方法可以用更像 C 的语言来做 LibreOffice/OpenOffice 宏吗?

以下代码可以满足您的需求:

Dim Doc As Object
Dim Sheet As Object
Dim oDestCell As Object
Dim CellRangeAddress As New com.sun.star.table.CellRangeAddress

Doc = ThisComponent
Sheet = Doc.Sheets(0)

CellRangeAddress.Sheet = 0
CellRangeAddress.StartColumn = 1
CellRangeAddress.StartRow = 8
CellRangeAddress.EndColumn = 2
CellRangeAddress.EndRow = 8

Sheet.insertCells(CellRangeAddress, com.sun.star.sheet.CellInsertMode.DOWN)

oDestCell=Sheet.getCellByPosition(1,8)
oDestCell.setValue(EntryDate)

oDestCell=Sheet.getCellByPosition(2,8)
oDestCell.setValue(EntryAmount)

您可以在 api.libreoffice.org

阅读有关 C++ 和 LibreOffice 的信息

感谢Mac,我的代码现在...

Public Doc As Object
Public Sheet As Object

Sub AddManualBalance()

    GetCurrentSheet()

    REM insert two new cells, move cells down
    Dim TargetCells As New com.sun.star.table.CellRangeAddress
    TargetCells.Sheet = 3
    TargetCells.StartColumn = 1
    TargetCells.StartRow = 8
    TargetCells.EndColumn = 2
    TargetCells.EndRow = 8
    Sheet.insertCells(TargetCells, com.sun.star.sheet.CellInsertMode.DOWN)

    REM get date and balance from text boxes, add value to cells
    Dim BalanceDate As Object
    Dim BalanceAmount As Object
    Dim Forms As Object
    Dim MainForm As Object
    Forms = Doc.getCurrentController().getActiveSheet().getDrawPage().getForms()
    MainForm = Forms("MainForm")
    BalanceDate = MainForm.getByName("BalanceDate")
    BalanceAmount = MainForm.getByName("BalanceAmount")
    Sheet.getCellByPosition(1,8).setValue(BalanceDate.CurrentValue)
    Sheet.getCellByPosition(2,8).setValue(BalanceAmount.CurrentValue)

End Sub

Sub GetCurrentSheet()

    REM get references to document and active sheet, test if exist
    If ThisComponent Is Nothing Then
        Return
    End If
    Doc = ThisComponent
    If Doc Is Nothing Then
        Return
    EndIf
    Sheet = Doc.getCurrentController().getActiveSheet()

End Sub