LibreOffice 5.2.3.3在PyUno中優化公式複製
我試圖將一個Excel VBScript程序移植到PyUno。邏輯起作用,但運行速度比Excel中慢得多。
我做了兩張紙,Sheet1和Sheet2。引用下面的腳本,我給Sheet1添加了一個按鈕,以調用create
,並向Sheet2添加一個以調用copy
。運行create
並等待它完成後,我運行copy
。
有什麼辦法可以進一步優化copy
?當它在一個單獨的線程中運行時,我可以看到每一行都被填滿,而我希望它對人眼是瞬間的。刪除線程只是讓圖形等待更新。我原來的代碼從一個不可見的CSV文件中複製數據,由於某種原因,這個數據需要更長的時間,直到它鎖定了Calc而沒有單獨的線程。我認爲這會出現這個問題,但顯然我需要。另一個測試用例或者,也許它很重要,這些細胞有更多的文字)
編輯1:針對@Jim K公司的評論:「單獨的線程」是指附加功能派生業務邏輯線程,如所以:
import threading
def _create():
# ...
pass
def create(clickEvent):
t = threading.Thread(target=_create)
t.start()
g_exportedScripts = create,
test.py(這是有問題的代碼。)
import msgbox
import os
import uno
def copyFormula(a, b):
formula = a.getFormula()
b.setFormula(formula)
return formula != ''
doc = XSCRIPTCONTEXT.getDocument()
def copy(clickEvent):
sheet1 = doc.Sheets.getByName('Sheet1')
sheet2 = doc.Sheets.getByName('Sheet2')
for y in range(0, 5):
for x in range(0, 150):
source = sheet1.getCellByPosition(x, y)
target = sheet2.getCellByPosition(x, y)
copyFormula(source, target)
def create(clickEvent):
sheet1 = doc.Sheets.getByName('Sheet1')
sheet2 = doc.Sheets.getByName('Sheet2')
for y in range(0, 5):
for x in range(0, 150):
target = sheet1.getCellByPosition(x, y)
target.setFormula('({}, {})'.format(x, y))
g_exportedScripts = create, copy
LibreOffice是否試圖重新繪製GUI?我想知道在wxpython中可能有類似於「凍結」和「解凍」的情況,你告訴GUI「在我告訴你這樣做之前停止繪製」 – alex314159
我在快速機器上運行這個代碼,少於一秒。在慢速機器上(大約11歲),大約需要4秒。這是你找到的,還是它在你的機器上運行速度慢得多?此外,我不明白你的意思是在單獨的線程中運行。這是否意味着與按下每張紙上的按鈕不同?注意:如果LibreOffice在[偵聽模式](https://wiki.openoffice.org/wiki/Documentation/DevGuide/ProUNO/Starting_OpenOffice.org_in_Listening_Mode)中打開,它將運行得慢得多。 –
@ alex314159:是的,有'doc.lockControllers()'和'doc.unlockControllers()'[https://www.openoffice.org/api/docs/common/ref/com/sun/star/frame/ XModel.html] – ngulam