如何在libreoffice calc中定期更改单元格值? [英] How to change a cell value periodically in libreoffice calc?
问题描述
标题说明了一切...
The title says it all...
例如,如果我想要一个显示当前时间并每分钟自动更新的单元格(嗯,我想我们称它为时钟),该怎么做?
For example, if I want to have a cell which displays the current time and auto updates minute by minute (well, I think we call that a clock), how do I do it?
是否已经实现了一个简单的功能,还是应该创建一个宏并将其分配给特定事件?
Is there a simple function implemented already or should I create a macro and assign it to a specific event?
编辑:按照@Jim K提供的答案,我想更加清楚自己想要什么.上面的时钟"示例在这里是为了使其易于理解,但我真正想要的是标题:一个定期更改的单元格值,它可以是字符串,数字,日期...
Following the provided answer by @Jim K, I want to be more clear about what I want. The "clock" example above was here to make it simple to understand, but what I really want is in the title: a cell value which changes periodically, be it a string, a number, a date...
推荐答案
首先在单元格中输入=NOW()
,然后通过转到格式->单元格来格式化数字.
First enter =NOW()
in a cell, and format the number by going to Format -> Cells.
接下来,此基本宏(来自此处)每分钟重新计算一次.转到工具->自定义,然后将其分配给Open Document
事件.
Next, this Basic macro (from here) recalculates every minute. Go to Tools -> Customize and assign it to the Open Document
event.
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宏(例如此处).将keep_recalculating_thread
分配给Open Document
事件.
However, this crashes when exiting LibreOffice. So instead, use the following threaded Python macro (like here). Assign keep_recalculating_thread
to the Open Document
event.
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/,尽管它需要链接到另一个看起来很麻烦的文件.
Another idea is at https://ask.libreoffice.org/en/question/5327/how-can-i-run-a-macro-at-regular-time-interval/, although it requires linking to another file which seems cumbersome.
编辑:
也许您正在寻找类似的东西?通过从空白电子表格开始并在单元格A1中输入1
进行测试.
Maybe you are looking for something like this? Test it by starting with a blank spreadsheet and entering 1
in cell A1.
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)
这篇关于如何在libreoffice calc中定期更改单元格值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!