[Solved] Sorting rows in selection Calc

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
syperjenka
Posts: 2
Joined: Sat Mar 11, 2023 6:55 pm

[Solved] Sorting rows in selection Calc

Post by syperjenka »

Hi all,
I'm trying to write a macro that would sort rows in ascending order within a selection. With one line it works, but with several lines it starts to work in a strange way. Tell me please, am I missing something?

Code: Select all

Sub TableSort1

	Dim oSortFields(0) as new com.sun.star.util.SortField
	Dim oSortDesc(1) as new com.sun.star.beans.PropertyValue
	document = ThisComponent
	osheet = ThisComponent.CurrentController.ActiveSheet
	oSelection = ThisComponent.getCurrentSelection()
	Rows = oSelection.getRows().getCount()

	For i = 0 to Rows Step 1
	    	oSortFields(0).Field = i
     		oSortFields(0).SortAscending = TRUE
		oSortDesc(0).Name = "SortFields"
		oSortDesc(0).Value = oSortFields()
		oSortDesc(1).Name = "IsSortColumns"
		oSortDesc(1).Value = FALSE
     		oSelection.Sort(oSortDesc())
  	Next

End Sub
Last edited by robleyd on Tue Mar 14, 2023 12:19 am, edited 1 time in total.
Reason: Tagged [Solved]. Add green tick
OpenOffice 4.1 on Windows 10
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Sorting rows in selection Calc

Post by Lupp »

syperjenka wrote: Sun Mar 12, 2023 9:06 am Hi all,
I'm trying to write a macro that would sort rows in ascending order within a selection. With one line it works, ...
Assuming the "line" is a row it obviously doesn't make sense: Sort one row by rows.
syperjenka wrote: Sun Mar 12, 2023 9:06 am ...but with several lines it starts to work in a strange way.
In what strange way?
Assuming your CurrentSelection is (e.g.) B7:H14. That's 7 columns, 8 rows.
Your variable i will therefore run from 0 through 8, and the relevant code line

Code: Select all

oSelection.Sort(oSortDesc())
should be expected to sort the same range 9 times trying to use first the first and finally the 9th column as the soring criterion. (The field index is 0-based.)
Firstly this can't be expected to make sense, secondly the 9th column will not exist at all.
In addition: If the CurrentSelection not is a single SheetCellRange, the .sort method can't work anyway.

(Trying to program in Basic using the API, you should start with simple things.)
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Sorting rows in selection Calc

Post by Lupp »

Probably this was what you actually had in mind:
justForFun.ods
(12.93 KiB) Downloaded 60 times
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
syperjenka
Posts: 2
Joined: Sat Mar 11, 2023 6:55 pm

Re: Sorting rows in selection Calc

Post by syperjenka »

Lupp wrote: Sun Mar 12, 2023 9:13 pm Probably this was what you actually had in mind:
justForFun.ods
Yes! That's what me need. Thanks a lot. Now it's clear what I missed.
OpenOffice 4.1 on Windows 10
User avatar
Lupp
Volunteer
Posts: 3548
Joined: Sat May 31, 2014 7:05 pm
Location: München, Germany

Re: Sorting rows in selection Calc

Post by Lupp »

You may want to edit your question, and to mark it solved.
On Windows 10: LibreOffice 24.2 (new numbering) and older versions, PortableOpenOffice 4.1.7 and older, StarOffice 5.2
---
Lupp from München
Post Reply