Python LibreOffice Calc 中的脚本不会自动刷新它是变量

Python Script in LibreOffice Calc doesn't refresh automatically it's variables

我需要知道 Table 中最后一行的编号才能处理它。所以,我把这个函数写在 Python:

def find_last_cell(cursor):
    cursor.gotoEnd()
    last_cell = sheetCalc.getCellRangeByName(cursorCalc.AbsoluteName)
    last_column = last_cell.CellAddress.Column
    last_row = last_cell.CellAddress.Row

    return last_column, last_row

我在我的“主要”功能中写了这个:

def calcula(oEvent):

    column_1, row_1 = find_last_cell(cursorCalc)
    write_Value(column_1, sheetCalc, 2, 28) #Just for debug
    write_Value(row_1, sheetCalc, 2, 29) #Just for debug

    #fill_qtd(row_1)
    #fill_tension(row_1)
    #fill_fp(row_1)


    return 0

问题:

第一次使用效果很好:

但是,当我修改 Table 并按下按钮时,它保持相同的结果:

调试:

com.sun.star.uno.RuntimeException: (Error during invoking function calcula in module file:///C:/Users/34471945882/AppData/Roaming/LibreOffice/4/user/Scripts/python/Calc_Inst.py (<class 'uno.com.sun.star.uno.RuntimeException'>: File "C:\Program Files\LibreOffice\program\pythonscript.py", line 915, in invoke ret = self.func( *args ) File "C:\Users471945882\AppData\Roaming\LibreOffice\user\Scripts\python\Calc_Inst.py", line 154, in calcula column_1, row_1 = find_last_cell(cursorCalc) #Verifica a última célula ocupada File "C:\Users471945882\AppData\Roaming\LibreOffice\user\Scripts\python\Calc_Inst.py", line 95, in find_last_cell last_cell = sheetCalc.getCellRangeByName(cursorCalc.AbsoluteName) #Seleciona a última célula ))

问题:我该如何纠正这种行为?我想随时使用该按钮而无需刷新或修改 python 文件?

LibreOffice 版本: 版本:7.2.5.2 (x64) / LibreOffice 社区 构建 ID:499f9727c189e6ef3471021d6132d4c694f357e5 CPU线程:4; OS: Windows 6.3 构建 9600; UI 渲染:Skia/Raster; VCL:赢 语言环境:pt-BR (pt_BR); UI: pt-BR 计算:CL

编程中的全局变量通常是个坏主意,这也不例外。来自评论:

If it's global then it will not get the changed value. Initialize those values at the beginning of calcula() instead... The initial part of the code only gets run once, when the python file gets modified, as you have already discovered. It's a good place to declare constants but not variables.