[Solved] Sort cells of a sheet with python

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Timitos
Posts: 5
Joined: Tue Jun 17, 2008 9:36 am

[Solved] Sort cells of a sheet with python

Post by Timitos »

Hi,

I try to sort a cellrange with python.

This is my code:

Code: Select all

def sortSheet(self,sortArea):
        from com.sun.star.table import TableSortField
        
        rSortArea = self._oSheet.getCellRangeByName("A1:C10")
        sortFld = TableSortField
        sortFld.Field = 0
        sortFld.IsAscending = True
        sortDesc = (PropertyValue("SortFields",0,sortFld,0),
                    PropertyValue("ContainsHeader",0,False,0))
        rSortArea.sort(sortDesc)
With this code I get an "segmentation fault (core dumped)" error.

I tried already many things but I can´t find a solution.

I am working with OpenOffice 2.3 on Ubuntu Gutsy 7.10.

Does anybody have a hint for me?
Last edited by Timitos on Wed Jun 18, 2008 5:08 pm, edited 1 time in total.
OOo 2.3.X on Ubuntu 7.x + Ubuntu 8.x
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: sort cells of a sheet with python

Post by Villeroy »

What is function call "PropertyValue"? Sort fields are a maximum of 3 TableSortFields in a tuple.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Timitos
Posts: 5
Joined: Tue Jun 17, 2008 9:36 am

Re: sort cells of a sheet with python

Post by Timitos »

These are the sources I used to create my code.
http://www.oooforum.org/forum/viewtopic.phtml?t=40114
http://api.openoffice.org/servlets/Read ... sgNo=16619
http://wiki.services.openoffice.org/wik ... ts/Sorting

In all those examples my variable aSortDesc is of type PropertyValue.

I also tried this:

Code: Select all

def sortSheet(self,sortArea):

        from com.sun.star.beans import PropertyValue
        from com.sun.star.table import TableSortField
        
        rSortArea = self._oSheet.getCellRangeByName("A1:C10")

        sortFld = TableSortField
        sortFld.Field = 0
        sortFld.IsAscending = True

        sortDesc = [PropertyValue, PropertyValue]

        sortDesc[0].Name = "SortFields"
        sortDesc[0].Value = sortFld
        sortDesc[1].Name = "ContainsHeader"
        sortDesc[1].Value = False
        
        rSortArea.sort(sortDesc)
Then I got this error:
uno.RuntimeException: <type 'exceptions.AttributeError'>: 'list' object has no attribute 'getTypes'

I think that I cannot choose a set in python to do this.
OOo 2.3.X on Ubuntu 7.x + Ubuntu 8.x
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: sort cells of a sheet with python

Post by Villeroy »

Python is just as good as any other scripting language. As far as ease of use is *not* concerned, it is certainly much better than Basic. However, it is not that easy to debug Python code running in OOo's component context. pythonscript.py in OOo's program folder has a utitility to print debug strings or variable contents into a log file (it's the same script from where we inherit XSCRIPTCONTEXT). Then there is the xray tool, written in Basic, but pretty much usable in Python: http://www.oooforum.org/forum/viewtopic.phtml?t=69350

There are some problems with non-trivial types, such as sequences, within structures:
http://www.oooforum.org/forum/viewtopic ... ython+sort
B. Marcelly wrote:This problem is common to all languages accessing OpenOffice through COM Automation.
At the bottom of that thread you find a piece of code that should help. I did not remember me writing that piece one year ago.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Timitos
Posts: 5
Joined: Tue Jun 17, 2008 9:36 am

Re: sort cells of a sheet with python

Post by Timitos »

Thank you very much Villeroy.
It´s working now.

This is my code for everybody who interested in:

Code: Select all

def sortSheet(self):
       
        rSortArea = self._oSheet.getCellRangeByName("A1:C200")
        sortField = uno.createUnoStruct('com.sun.star.table.TableSortField')
        sortField.IsAscending = True
        sortField.Field = 0
        sortDesc = rSortArea.createSortDescriptor()

        for PropVal in sortDesc:
            if PropVal.Name == 'SortFields':
                PropVal.Value = uno.Any('[]com.sun.star.table.TableSortField',(sortField,))
            elif PropVal.Name == 'ContainsHeader':
                PropVal.Value = uno.Any('boolean', False)
        rSortArea.sort(sortDesc)
OOo 2.3.X on Ubuntu 7.x + Ubuntu 8.x
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: sort cells of a sheet with python

Post by Villeroy »

Timitos wrote:Thank you very much Villeroy.
It´s working now.

This is my code for everybody who interested in:

Code: Select all

                PropVal.Value = uno.Any('[]com.sun.star.table.TableSortField',(sortField,))
which assignes a tuple of one element to the value of a property struct. The value is explicitly typed as a sequence of sort fields.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Timitos
Posts: 5
Joined: Tue Jun 17, 2008 9:36 am

Re: sort cells of a sheet with python

Post by Timitos »

Hi Villeroy.

I used your own solution from this thread:
http://www.oooforum.org/forum/viewtopic ... ython+sort

Do you have perhaps a hint for me how to use this code correctly?

Regards,
Timitos
OOo 2.3.X on Ubuntu 7.x + Ubuntu 8.x
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: sort cells of a sheet with python

Post by Villeroy »

Do you have a hint how you run this incorrectly? My last answer was just to explain other readers which particular line solved your problem, how to use sequences in structs.
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Timitos
Posts: 5
Joined: Tue Jun 17, 2008 9:36 am

Re: sort cells of a sheet with python

Post by Timitos »

Ah ok. Sorry. I thought that there is still something wrong with my code.

I tag this thread as solved.

Thanks again, Villeroy.
OOo 2.3.X on Ubuntu 7.x + Ubuntu 8.x
Post Reply