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.
rg of a Calc spreadsheet can sort itself using a
sd (which is an array of
com.sun.star.beans.PropertyValue) 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.