LibreOffice 5.3,python 3.53,VOID Linux
这更像是一个uno问题,而不是一个python问题 . 下面的代码对3个单元格进行了简单的更新 . 在工作表上配置的3个按钮调用dowriteonce()dowritetwice()和dowritethrice(),它们都会更新并且工作就像您可能希望将数字和文本写入选定的单元格一样 .
问题出现的地方是,当用户在UI中编辑单元格时,阻止通过执行该功能对该单元格的任何后续更新 . 因此,只需在calc UI中单击单元格C4,就可以防止writethrice()函数更新单元格C4 . 如果我删除了内容并单击UI中的另一个单元格,例如C5,那么一切都会正常工作,单击按钮时C4会更新 .
我想要做的是在执行之前将UI编辑光标重定位到未使用的单元格,以防止这种情况发生 . 用户复制粘贴会将活动光标留在不可预测的位置,如果我无法隔离光标,则会进行计算 .
所以问题是,如何通过UNO API将UI编辑光标移动到命名单元格?或者如果它更容易,只需暂时停用它 .
蟒蛇:
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()
1 回答
在以下代码中,在更改值之前取消选择单元格,然后再次选择 . 这样,即使用户处于编辑模式,也可以修改单元格 .
关于Python类方法和变量似乎也有一些混淆,所以我也改变了这些部分 .