简体   繁体   English

使用pyuno对calc文档中的单元格范围进行排序

[英]Sorting cell range in a calc document with pyuno

With pyuno I open a LibreOffice calc document, I define a range of cells and I want to sort it. 使用pyuno,我打开了一个LibreOffice calc文档,定义了一个单元格范围,并希望对其进行排序。 Here is the code: 这是代码:

import os
import uno

# open a calc document
# (it is assumed that LibreOffice is launched with the command line:
# soffice -accept="socket,host=localhost,port=2002;urp")
local = uno.getComponentContext()
resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)
context = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
url = uno.systemPathToFileUrl(os.path.abspath("c:/aaa/essai.xls"))
doc = desktop.loadComponentFromURL(url,"_blank", 0, ())

# range to be sorted
range = doc.Sheets.getByIndex(0).getCellRangeByPosition(0,0,4,4)
# use the first column for the sort key 
colDescr = uno.createUnoStruct(
  'com.sun.star.table.TableSortField')
colDescr.Field = 0 
# sort descriptor
sortDescr = range.createSortDescriptor()
for x in sortDescr: 
  if x.Name == 'SortFields':
    x.Value = (colDescr,)
    break
else:
  raise KeyError('SortFields')
# sort ...
range.sort(sortDescr)

This code is correctly interpreted but does nothing (the rows are not sorted in the calc document) Am'I wrong? 这段代码可以正确解释,但是什么也不做(行未在calc文档中排序)我错了吗? I think that sortDescr has the right type (PropertyValue tuple) but I'm not sure. 我认为sortDescr具有正确的类型(PropertyValue元组),但我不确定。 I use Windows 7, LibreOffice 4.3.4.1, Python 3.3 Thank you for your answers. 我使用Windows 7,LibreOffice 4.3.4.1,Python 3.3谢谢您的回答。

Change the row: x.Value = (colDescr,) 更改行:x.Value =(colDescr,)

By: x.Value = uno.Any('[]com.sun.star.table.TableSortField',(aCriterios,)) 创建者:x.Value = uno.Any('[[]。com.sun.star.table.TableSortField',(aCriterios,))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM