macro for auto sort cells and save [calc]

Hi people,

In according to my earlier sorting question, which is solved with great help, I wonder if there is any possibility to make a simple script/macro that sort alphabetically Column A from A to Z at the moment when closing the program. In fact when I press the X-button [close] or File/Close, the macro must sort in Column A the cells A to Z and then Save and Close the program.

Only problem is that Column B and Column C for now is automatically linked to A while I am sorting. So, that function must be in the macro/script too.

I have much computer experience but not in programming and scripting. :wink:

Can someone help me with this?

Cheers, Geeviz.

Columns A through C is too much (3145728 cells). You need to reduce the size of the range (as the interactive sorting does automatically). You can get the “blank bottom tail” of the range using the queryEmptyCells method of the range.

The current way of sorting now is: I sort only Column A A-Z but I tick the option to have Column B and C to sort with Column A. Only thing is I need a trick to auto sort at saving my document.

Why do you want to combine the sorting and the closing of the document?
Why do you think you need a “macro”?

What seems “simple” concerning macros depends on the experience of the person judging.
Any range rg of a Calc spreadsheet can sort itself using a SheetSortDescriptor sd (which is an array of as the controlling structure: rg.sort(sd). That’s simple enough.

However, you need to know how to get the appropriate range and what properties (by name) are supported by the sort method, what values of the properties are accepted, and how they work.
See LibreOffice: SheetSortDescriptor Service Reference e.g.
You may start with studying the famous texts by Andrew Pitonyak (“Useful Macro Information” and “OpenOffice Macros Explained”).

The “extend” option is a gimmick of the UI. Writing a macro you need to explicitly choose the range to sort. Assuming coulmns A through C haven’t any content below the last entry in column A you can get that using a range query.
Then there is to create a SortDescriptor and a SortFields structure. Both need to get set their properties. The sorting itself ist then ordered with a few letters.
Finally the "macro must be assigned to the proper document event.
There may occur side effects one day. No guarantee of any kind!
That’s a game for me. I never considered to do such things with documents I actually used “in production”.
You may try this attached example.

I don’t like to sort each time manually so I want to sort automatically at document’s saving. With the option [extend] so that columns B and C sort automatic with A. And Column A is the head-column to sort. And I thought a Macro is the most simple way…