Sort buttons on spreadsheet

Shared Libraries
Forum rules
For sharing working examples of macros / scripts. These can be in any script language supported by OpenOffice.org [Basic, Python, Netbean] or as source code files in Java or C# even - but requires the actual source code listing. This section is not for asking questions about writing your own macros.
Post Reply
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Sort buttons on spreadsheet

Post by Villeroy »

Changes the actual sort field of a list by clicking on a hyperlink on the sheet. The attached spreadsheet contains an example and Basic code in library "Calc", module "SheetSort".

The code is most simply designed to work with any named database range without adjusting the code. The calling hyperlink passes the name of the database range, an optional field-ID (default=0 for first field) and an optional switch for descending order (default 0 for ascending).
  1. Create a named database range.
  2. Sort it once, including all required extra options (contains header, include formats).
  3. Add hyperlinks as suggested in the example document.
Attachments
SheetSort.ods
(19.5 KiB) Downloaded 2786 times
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
User avatar
cheros
Posts: 15
Joined: Sun Jun 01, 2008 5:12 pm

Re: Sort buttons on spreadsheet

Post by cheros »

Could I use this to pass arguments to a sort function (and if so, how)?

I'm trying to get the simple sort in the OO Calc manual tied to a button, but part of me objects against having to write 6 different macros because I can't find a way to pass an argument to the same function when I use a different button. I basically have one sheet which has information in range A2:F10, and I want to add a "sort" button to every column A..F so the whole range sorts, based on the data in that specific column.

I've found how to turn a column header into a button (http://user.services.openoffice.org/en/ ... 45&t=31665), but I can't see how I can pass an argument to a macro. I probably missed something somewhere, sorry :crazy: . It's basically day 2 that I look into macros with any degree of seriousness..

Thanks in advance for the help..
= CH =

OOo 3.4.1 and LOo 3.5.6.2 on OSX 10.8.1
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Sort buttons on spreadsheet

Post by Villeroy »

cheros wrote:Could I use this to pass arguments to a sort function (and if so, how)?
Well, this is exactly what my sort buttons do. Select any of the cells A1:B2 in my example file and hit F2. You see all the referenced cell values that are used to concatenate a valid macro URL. With tool tips enabled you see the full URL when you hover the mouse pointer over these cells. An URL contains pairs of names and values after the "?", spearated by "&".
protocol:location/path?arg1=val1&arg2=val2&arg3=val3
http://user.services.openoffice.org/en/forum/posting.php?mode=edit&f=21&p=189067
vnd.sun.star.script:Calc.SheetSort.sortByURL?location=document&language=Basic&dbRange=List&SortField=0 "location=document" and "language=Basic" are required to call the macro, "dbRange=List" and "SortFields=0" are evaluated by the macro
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
uBoer
Posts: 1
Joined: Sat Mar 03, 2012 2:15 pm

Re: Sort buttons on spreadsheet

Post by uBoer »

@ Villeroy

You wrote "The code is most simply designed to work ..." to which I can conclude, yes it is deigned to work, but I do not think it is simple. :D

I am a rooky as to macro development with calc, but have some experience in object orientated scripting and programming. Most of the information in respect of macro I have is from you. (Like the VVJOIN and CELL_functions)

I was looking at developing a sorting function (when querying the internet on that topic I did not get to this posting), because most objects I work with has some significance in respect of order, however, they are created in a random manner. Therefore it is most useful to have the data sorted when disseminating the information so that an ordered sequence can be followed.

The built in functions works for all situations, however it is manually driven in terms of seting up the models and for refreshing the sorting. (As far as I know).

I have been following the macro examples of chapter 12 of the Calc Guide (expecially the very last one on sorting) and got a more customized version working as a sub-routine macro that is run from the menu or by clicking a custom button assigned with the macro. All variables are hard coded within the sub-routine.

The macro contains uno services, a cell range, and then sorts on one of the uno functions available to a range. The uno Object Model is still foreign to me.

Then I tried to use the same code to develop a function that would execute these actions, to no avail. The function would run completely, but the data on the sheet is not updated. There is nothing wrong with the code itself, because of it being an exact copy of the sub-routine that works.

My next idea was to triger the sub-routine from inside the function, but this still did not work.

I was under the impression that triggering the execution of a sub-routine can be done by various ways and the result would be similar in all cases.

I stumbled upon this posting as a result of a search into "how to simulate a button event" in order to find a way to trigger the sub-routine automatically. It seems both cheros and myself are maybe confused as to the execution requirements for running a sub-routine automatically.

The lists are not static and need constant updating and re-sorting which may result in a missed procedure.

Your solution in respect of the url sorting seems to be the perfect answer. If I understand correctly, the method followed can be dynamically maintained for any of the parameters especially the size of the array.

I only have one problem, how do I fire the soring in a dynamic manner, whithout physically clicking on the hyperlink.

As stated above, I was under the impression that such an event can be triggered within a function, so when the function updates, it triggers the sort sub-routine and voi la the sorting is dynamic.

The other solution that works sort of is the following sequence:
1. List of data is parsed as an array into a function taking a range
2. Assign the array as cellrange, sort it inside the function
3. Produce the result as an array back to the sheet

The problem, as I understand, is that when an array is returned to the sheet, the sheet reserves some sort of are for it in protecting it from being erroneously deleted etc. Then when the list grows double its size, half of the list is not accessible. Therefore, I would like a dynamically managed list without restricting the length beyond what was setup.

Could you perhaps comment on this rather complicated concept to a novice.
LibreOffice 3.4 on uBuntu 10.11 and LibreOffice 3.5 on Windows 7 Professional (32 bit) (2.4.3 until October 2009)
Don't kil time.
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Sort buttons on spreadsheet

Post by Villeroy »

uBoer wrote:Could you perhaps comment on this rather complicated concept to a novice.
No, I can't.
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
Post Reply