有谁知道如何使用 OO uno bridge api 在 Calc 表中“全选”?
或者,找到最大使用的行号和列号也可以。
我想要做的是将格式应用于电子表格中的所有单元格。
(原因是我将工作表保存为 csv,因此除非格式提供足够的小数位,否则无法准确保存数字。)
有谁知道如何使用 OO uno bridge api 在 Calc 表中“全选”?
或者,找到最大使用的行号和列号也可以。
我想要做的是将格式应用于电子表格中的所有单元格。
(原因是我将工作表保存为 csv,因此除非格式提供足够的小数位,否则无法准确保存数字。)
假设您已经连接到 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
我确实收到了以下行的错误(属性错误):
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] )
或任何进一步的工作。