【问题标题】:OpenOffice pyuno "select all"OpenOffice pyuno“全选”
【发布时间】:2011-06-25 21:01:39
【问题描述】:

有人知道如何使用 OO uno bridge api 在 Calc 表中“全选”吗?

或者,找到最大使用的行号和列号也可以。

我想要做的是将格式应用于电子表格中的所有单元格。

(原因是我将工作表保存为 csv,因此除非格式提供足够的小数位,否则无法准确保存数字。)

【问题讨论】:

    标签: python openoffice.org openoffice-calc uno pyuno


    【解决方案1】:

    假设您已经连接到 OpenOffice,并且document 是一个已打开或创建的电子表格。

    #get the sheet you want to work with, I'm just going to grab the first sheet
    sheets = document.getSheets().createEnumeration()
    sheet = sheets.nextElement()
    
    #start with a range on the first cell
    range = sheet.getCellRangeByPosition( 0, 0, 0, 0 )
    
    #expand to full extend of the used area
    range.gotoEndOfUsedArea( True ) #true for expand selection
    
    #no do whatever formatting things you want to do
    

    【讨论】:

    • 两件事:首先,我认为在python中使用'range'作为变量不是一个好主意,因为它与Python范围函数冲突。其次,截至 2018 年 7 月,我没有使用范围对象获得方法“gotoEndOfUsedArea”。 @Daniel 的解决方案虽然有效。
    • 这在 7 年前确实有效。 ;) 很高兴您得到了所需的解决方案。
    【解决方案2】:

    我确实收到了以下行的错误(属性错误):

    range.gotoEndOfUsedArea(True)

    通过结合这两个信息 1:http://nab.pcug.org.au/transferdemo_oocalc.py 和 2:https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Cells_and_Ranges

    我想出了以下解决方案:

    def getLastActiveCell(sheet):
        """returns the last used column and row of the provided sheet 
        (ie the last cell in the range containing something other than '')"""
        #create a cursor for the whole sheet using OO interface XSheetCellRange 
        cursor = sheet.createCursor()
        #goto the last used cell
        cursor.gotoEndOfUsedArea(True)
        #grab that positions "coordinates"
        address = cursor.RangeAddress
        endcol = address.EndColumn
        endrow = address.EndRow
        #and pass them back
        return endcol,endrow
    

    然后您可以像这样在代码中访问这些值:

    lastCell = getLastActiveCell(sheetObject)
    print lastCell[0] #Column
    print lastCell[1] #Row
    

    并创建一个范围

     range = sheetObject.getCellRangeByPosition( 0, 0, lastCell[0], lastCell[1] )
    

    或任何进一步的工作。

    【讨论】:

    • 感谢您的帮助 - 因为文档不容易找到,所以帮助了我。但是,在上面的代码中需要注意两点:(1) cursor = tabelle.createCursor()' 应该是 'cursor = sheet.createCursor()',(2) 自从编写 sn-p 以来,事情似乎发生了变化。我必须使用 'address.value.EndRow' 和 'address.value.EndColumn' 分别访问结束行和结束列。
    • 因为我是德国人 tabelle=sheet 误入了。我确实编辑了我的帖子。我将不得不再次检查(2)。但是很可能是因为一段时间过去了。 :-)
    猜你喜欢
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多