首页 文章

关于LibreOffice Calc的Python UNO,重新定位游标

提问于
浏览
0

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 回答

  • 1

    在以下代码中,在更改值之前取消选择单元格,然后再次选择 . 这样,即使用户处于编辑模式,也可以修改单元格 .

    关于Python类方法和变量似乎也有一些混淆,所以我也改变了这些部分 .

    import uno
    import unohelper
    
    SCOOBY = "Scooby"
    
    class ODSCursor(unohelper.Base):
        def __init__(self):
            self.ctx = None
            self.desktop = None
            self.document = None
            self.controller = None
            self.sheet = None
            self.counter = 0
    
        def sheet1(self):
            """Initialize uno handle only once and get the first sheet."""
            if self.sheet is not None:
                return self.sheet
            self.ctx = uno.getComponentContext()
            self.desktop = self.ctx.ServiceManager.createInstanceWithContext(
                "com.sun.star.frame.Desktop", self.ctx)
            self.document = self.desktop.getCurrentComponent()
            self.controller = self.document.getCurrentController()
            self.sheet = self.controller.getActiveSheet()
            return self.sheet
    
        def writeonce(self):
            self.writeval("B1", self.inc())
    
        def writetwice(self):
            self.writeval("B2", self.inc())
    
        def writescooby(self):
            self.writeval("C4", SCOOBY)
    
        def writeval(self, address, value):
            self.deselect()
            cell = self.sheet.getCellRangeByName(address)
            cell.String = value
            self.controller.select(cell)
    
        def deselect(self):
            """Select cell A1, then select nothing."""
            cell_a1 = self.sheet.getCellByPosition(0, 0)
            self.controller.select(cell_a1)
            emptyRanges = self.document.createInstance(
                "com.sun.star.sheet.SheetCellRanges")
            self.controller.select(emptyRanges)
    
        def inc(self):
            """Increment the counter and return the value."""
            self.counter += 1
            return self.counter
    
    odsc = ODSCursor()
    
    
    ### BUTTON BOUND FUNCTIONS ###
    
    def dowriteonce(dummy_oEvent):
        odsc.sheet1()
        odsc.writeonce()
    
    def dowritetwice(dummy_oEvent):
        odsc.sheet1()
        odsc.writetwice()
    
    def dowritethrice(dummy_oEvent):
        odsc.sheet1()
        odsc.writescooby()
    

相关问题