OpenOffice pyuno "select all"

Does anyone know how to use the OO uno bridge api to "select all" in a Calc sheet?

As an alternative, finding the maximum number of rows and columns will work.

What I want to do is apply the format to all cells in the spreadsheet.

(The reason is that I save the sheet as csv, so the numbers are not saved exactly if the format does not contain enough decimal places.)

+5
source share
2 answers

Assuming you are 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 
+2
source

I get an Attribute Error with the line:

range.gotoEndOfUsedArea (True)

Combining the two information in 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 can then access these values ​​in your code as follows:

 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.

+2
source

Source: https://habr.com/ru/post/1339422/


All Articles