OpenOffice pyno“全选”

发布于 2024-10-16 19:28:19 字数 163 浏览 4 评论 0原文

有谁知道如何使用 OO uno 桥接 api 在 Calc 工作表中“选择全部”?

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

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

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

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

Alternatively, finding the maximum used row and column number would work.

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

(The reason being that I'm saving the sheet as csv, so numbers are not accurately saved unless the format provides enough decimal places.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

执手闯天涯 2024-10-23 19:28:19

假设您已连接到 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

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
如若梦似彩虹 2024-10-23 19:28:19

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

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] )

或其他内容以进行进一步的工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文