【发布时间】:2018-05-27 01:39:38
【问题描述】:
LibreOffice 5.3、python 3.53、VOID Linux
这更像是一个 uno 问题而不是 python 问题。下面的代码对 3 个单元格进行了简单的更新。工作表上配置了 3 个按钮,调用 dowriteonce()、dowritetwice() 和 dowritethrice(),它们都会更新和工作,就像您可能期望将数字和文本写入选定的单元格一样。
问题出在哪里,当用户在 UI 中编辑单元格时,通过执行该函数对该单元格的任何后续更新都会被阻止。因此,只需在计算 UI 中单击单元格 C4,就可以防止 writethrice() 函数更新单元格 C4。如果我删除内容并单击 UI 中的另一个单元格,比如 C5,那么一切都会再次正常工作,并且在单击按钮时 C4 会更新。
我想做的是在执行之前将 UI 编辑光标重新定位到未使用的单元格,以防止这种情况发生。用户复制粘贴会将活动光标留在不可预测的位置,如果我无法隔离光标,这将导致计算失败。
所以问题是,如何使用 Python 通过 UNO API 将 UI 编辑光标移动到命名单元格?或者,如果它更容易,只需暂时停用它。
Python:
import socket
import sys
import re
import uno
import unohelper
class ODSCursor(unohelper.Base):
# predeclare class properties
ctx=None
desktop=None
model=None
activesheet=None
counter=0
scooby="Scooby"
# import namespaces
def __init__(self):
import socket
import uno
import unohelper
import sys
import re
# initialize uno handle only once and get the first sheet
@classmethod
def sheet1(cls,*args):
if cls.activesheet is not None:
return (cls.activesheet)
cls.ctx = uno.getComponentContext()
cls.desktop = cls.ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", cls.ctx)
cls.model = cls.desktop.getCurrentComponent()
# cls.activesheet = cls.model.Sheets.getByName("Sheet1")
cls.activesheet = cls.model.Sheets.getByIndex(0)
return (cls.activesheet)
@classmethod
def writeonce(self,*args):
self.counter += 1
cell_b1 = self.activesheet.getCellRangeByName("B1")
cell_b1.String = self.counter
@classmethod
def writetwice(self,*args):
self.counter += 1
cell_b2 = self.activesheet.getCellRangeByName("B2")
cell_b2.String = self.counter
@classmethod
def writescooby(self,*args):
cell_c4 = self.activesheet.getCellRangeByName("C4")
cell_c4.String = self.scooby
### BUTTON BOUND FUNCTIONS ###
def dowriteonce(*args):
Odc = ODSCursor() # create the object
Odc.sheet1()
Odc.writeonce()
def dowritetwice(*args):
Odc = ODSCursor() # create the object
Odc.sheet1()
Odc.writetwice()
def dowritethrice(*args):
Odc = ODSCursor() # create the object
Odc.sheet1()
Odc.writescooby()
【问题讨论】:
标签: python libreoffice openoffice.org uno