OpenOffice pyuno “select all”

旧时模样 提交于 2019-12-05 09:13:19

Assuming you have already connected to OpenOffice and document is a spreadsheet that has been opened or created.

#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

I do get an error (Attribute Error) with the line:

range.gotoEndOfUsedArea(True)

By combining the two information at 1: http://nab.pcug.org.au/transferdemo_oocalc.py and 2: https://wiki.openoffice.org/wiki/Documentation/BASIC_Guide/Cells_and_Ranges

I came up with the following solution:

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

you may then access those values in your code like this:

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

and create a range

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

or whatever for further work.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!