LibreOffice Macro总是显示#NULL!重新打开文件后

问题描述 投票:0回答:2

我在LibreOffice Calc中编写了一个宏,它能够正确运行。但是,如果我关闭文件并重新打开,它总是显示#NULL!而不是正确的值。我在这里错过了什么?

我的宏代码

Rem Attribute VBA_ModuleType=VBAModule
Option VBASupport 1
Function Calculate(CalType As String) As Double
'
' Calculate Macro
'

Dim i As Integer
Calc = 0

i = 1

Do While Not IsEmpty(Cells(i, 2))
    If (Cells(i, 3).Value = CalType And (Cells(i,2) = "A" Or Cells(i,2) = "B")) Then
        Calculate = Calculate + Cells(i, 4).Value
    ElseIf (Cells(i, 3).Value = CalType And Cells(i,2) = "C") Then
        Calculate = Calculate - Cells(i, 4).Value
    End If
    i = i + 1
Loop

'
End Function

调用函数将类似于=Calculate(J6)

该文件保存为.ods格式。

macros libreoffice
2个回答
0
投票

Cells电话对我来说根本不起作用。它来自VBA,而不是LO Basic。但我认为这不是主要问题。

LibreOffice期望用户定义的函数很简单,只访问包含公式的单元格。由于在调用函数时电子表格尚未完全加载,因此无法读取其他单元格。

解决方法是忽略错误并等待文档完全加载后再运行该函数。以下面的代码为例:

Function ReadOtherCell(row, col)
    On Error GoTo ErrorHandler
    oSheet = ThisComponent.CurrentController.ActiveSheet()
    oCell = oSheet.getCellByPosition(row, col)
    ReadOtherCell = "value is '" & oCell.getString() & "'"
    Exit Function
    ErrorHandler:
        Reset
End Function

Sub RecalculateAll
    ' This is for user-defined functions that need to read the spreadsheet.
    ' Assign it to the "View created" event,
    ' because before that, the spreadsheet is not fully loaded.
    ThisComponent.calculateAll
End Sub

在A1输入foo,在A2输入=ReadOtherCell(0,0)。到目前为止,这有同样的问题 - 首次打开文档时它将失败。

现在,去Tools -> Customize。在“事件”选项卡中,突出显示View created。按Macro...并找到RecalculateAll函数。然后按OK。

现在,当文档关闭并重新打开时,单元格A2应显示结果value is 'foo'

这是源于B. Marcelly在http://ooo-forums.apache.org/en/forum/viewtopic.php?f=20&t=73090&sid=f92a89d676058ab597b4b4494833b2a0的回答。


0
投票

我有同样的问题。我注意到在模块中,我有一个空的Sub Main在我擦除之后,功能再次开始工作

© www.soinside.com 2019 - 2024. All rights reserved.