简体   繁体   English

在PyUno中优化公式复制

[英]Optimizing formula copying in PyUno

LibreOffice 5.2.3.3 LibreOffice 5.2.3.3

I'm trying to port an Excel VBScript program to PyUno. 我正在尝试将Excel VBScript程序移植到PyUno。 The logic works, but it runs much more slowly than it did in Excel. 该逻辑有效,但运行速度比Excel中慢得多。

I made two sheets, Sheet1 and Sheet2. 我制作了两张纸Sheet1和Sheet2。 Referencing the below script, I added a button to Sheet1 to call create and one to Sheet2 to call copy . 参考以下脚本,我在Sheet1中添加了一个按钮来调用create ,在Sheet2中添加了一个按钮来调用copy After running create and waiting for it to complete, I run copy . 运行create并等待其完成后,我运行copy

Is there any way to further optimize copy ? 有什么办法可以进一步优化copy When it runs in a separate thread, I can see each row get filled, while I hoped it would be instantaneous to the human eye. 当它在单独的线程中运行时,我可以看到每一行都被填满了,而我希望它对人眼是瞬间的。 Removing the thread just makes the graphics wait to update. 删除线程只会使图形等待更新。

(My original code copies data from an invisible CSV file, which takes even longer for some reason, to the point that it locks up Calc without separate threads. I thought this was going to manifest that problem, but apparently I need another test case. Or maybe it matters that those cells have more text.) (我的原始代码从一个不可见的CSV文件复制数据,由于某种原因,该文件甚至需要更长的时间,以至于它锁定了Calc而没有单独的线程。我以为这将证明这个问题,但显然我需要另一个测试用例。也许重要的是这些单元格有更多文本。)

Edit 1: In response to @Jim K's comment: "Separate thread" means an additional function spawns a thread for the business logic, like so: 编辑1:响应@Jim K的评论:“单独的线程”表示一个附加功能为业务逻辑生成了一个线程,如下所示:

import threading


def _create():
    # ...
    pass


def create(clickEvent):
    t = threading.Thread(target=_create)
    t.start()


g_exportedScripts = create,

test.py (This is the code in question.) 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

Either of these functions should be much faster: 这些功能中的任何一个都应该更快:

def copy2(clickEvent=None):
    sheet1 = doc.Sheets.getByName('Sheet1')
    sheet2 = doc.Sheets.getByName('Sheet2')
    range1 = sheet1.getCellRangeByPosition(0,0,150,5)
    range2 = sheet2.getCellRangeByPosition(0,0,150,5)
    range2.setDataArray(range1.getDataArray())

def copy3(clickEvent=None):
    sheet1 = doc.Sheets.getByName('Sheet1')
    sheet2 = doc.Sheets.getByName('Sheet2')
    range1 = sheet1.getCellRangeByPosition(0,0,150,5).RangeAddress
    range2 = sheet2.getCellRangeByPosition(0,0,150,5).RangeAddress
    cell2 = sheet2.getCellByPosition(
        range2.StartColumn, range2.StartRow).CellAddress
    sheet1.copyRange(cell2, range1)

Alternatively, use the dispatcher to copy and paste with the clipboard. 或者,使用调度程序复制并粘贴到剪贴板。

See section 5.23 in Andrew Pitonyak's macro document for more information about copying and pasting cells. 有关复制和粘贴单元格的更多信息,请参见Andrew Pitonyak的宏文档中的 5.23节。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM