首先在单元格中输入=NOW(),然后转到格式 -> 单元格来格式化数字。
接下来,这个基本宏(来自here)每分钟重新计算一次。转到工具 -> 自定义并将其分配给 Open Document 事件。
Sub RecalculatePeriodically
Const secs = 60
On Error Goto ErrorHandler
Do While True
Wait(1000 * secs)
ThisComponent.calculateAll()
Loop
ErrorHandler:
'Document was probably closed
End Sub
但是,退出 LibreOffice 时会崩溃。因此,请改用以下线程化 Python 宏(如 here)。将keep_recalculating_thread 分配给Open Document 事件。
import time
from threading import Thread
import uno
def keep_recalculating_thread(action_event=None):
t = Thread(target = keep_recalculating)
t.start()
def keep_recalculating():
oDoc = XSCRIPTCONTEXT.getDocument()
while hasattr(oDoc, 'calculateAll'):
oDoc.calculateAll()
time.sleep(60)
g_exportedScripts = keep_recalculating_thread,
另一个想法是https://ask.libreoffice.org/en/question/5327/how-can-i-run-a-macro-at-regular-time-interval/,虽然它需要链接到另一个看起来很麻烦的文件。
编辑:
也许您正在寻找这样的东西?从一个空白电子表格开始测试它,然后在单元格 A1 中输入 1。
def keep_changing_cell(action_event=None):
t = Thread(target = keep_changing_thread)
t.start()
def keep_changing_thread():
oDoc = XSCRIPTCONTEXT.getDocument()
oSheet = oDoc.CurrentController.ActiveSheet
COLUMN_A = 0
FIRST_ROW = 0
oCell = oSheet.getCellByPosition(COLUMN_A, FIRST_ROW)
while hasattr(oDoc, 'calculateAll'):
oCell.setValue(oCell.getValue() + 1)
time.sleep(2)