Calc macro - data sort

I am trying to write a macro to sort a spreadsheet from (eventually) a button.

I have been unable to get the macro recorder to generate code to be linked to by my new button. It generates a commented out dispatch command but does not set any sort parameters. When done manually the sort works as I want it to do.

I have search for, and failed to find, any documentation of the parameters I need to set to repeat this search using a macro.

The data I want to sort is on a dedicated Tab (no other data). It has a header row and I simply want to sort based on a single specific column (by header name, or could use column number) in ascending order.

If someone could point me to the documentation for the parameters I need to set to perform the sort I would be very grateful.

I am very experienced with Excel macros (since 1989) and with programming in general (C, C++, Pythion, VBA etc). I am not so familiar with the debugging tools available for LO macros.

I am currently using LO version 7.2.2.2 on W10.

Click any cell in the column you want to sort by and click the [A-Z] or [Z-A] button on the standard toolbar.
This sorts the current region around the active cell by the acive cell’s column excluding any detected header row. A first row consisting of text only (no blanks, no numbers) is treated as a header row.
You may also attach push buttons to the sheet and assign them to the corresponding dispatches without writing any macro code.

1 Like

One more thing: Calc supports stable sorting. If you sort by column A and then by B having duplicates, the previous sort order A is preserved within the duplicates in B. If you want to sort by A, then B, then C and D you click a cell in D, sort button, then C, sort button, B, sort button, A, sort button.

Thant does NOT answer my question. I already know how to do if manually but I want to do it with a macro initiated via a button.

The target user for the spreadsheet is for a totally non technical person to be able to do the sort. The sort menu is too complicated for them to remember how to do it, even with step by step instructions…

What do you think who we are? Human macro recorders?

What a pretender!

1 Like

thats fine but its Python not Pythion,

def quick_test_sort(*_):
    doc = XSCRIPTCONTEXT.getDocument()
    data = doc.CurrentSelection.DataArray
    header, *data = data
    out = [header]
    out.extend((sorted(data,key=lambda line: line[4])))
    doc.CurrentSelection.DataArray = out

I had not envisaged using Python (sorry about my spelling!). Is Python installed with LibreOffice or would it be an additional install. I am developing the macro for another very non technical person and I must keep the number if additional software installs to a minimum.

I would prefer to use Basic as the language if possible. It looks as if it should be simple enough to do if I could only find a document that lists the parameters I must set before calling the dispatch function.

Please see the chapter 6.13. Sort range and listings there. The rest of the chapters will help you complete other frequently repetitive tasks.

1 Like

Your link gave me enough information to solve my problem. It is now working as I wanted. Thank you so much.

The other responders did not bother to take the trouble to understand my problem i.e. where can I find the documentation, so I could identify and set the correct parameters. I have had long experience as a programmer (since 1963) however I don’t have a need to program regularly (now recently retired as a network architect) and am not familiar with the current LO object models. I had tried the macro recorder and it did not create any helpful output.