Sorting the Sheet tabs in Spreadsheet in Numerical Order

How can I sort sheets (tabs) in numerical order.

Hello @Siskar63,

The Sheets are already sorted in numerical order, ranging from index=0 to the total amount of Sheets minus 1.

Do you want to sort the Sheets based on a number which occurs in their names ?

To sort sheets by name, run this macro from Ordering sheets (View topic) • Apache OpenOffice Community Forum.

If (Not GlobalScope.BasicLibraries.isLibraryLoaded("Tools")) Then
   GlobalScope.BasicLibraries.LoadLibrary("Tools")
End If
Sheets = ThisComponent.Sheets
Names = Sheets.ElementNames
SortedList = BubbleSortList(Names) 'BubbleSortList is a macro in the Tools Library'
for i = 0 to UBound(SortedList)
   Sheets.MoveByName(SortedList(i), i)
next i

Hi @jimk my edit is only to add ' (end of comment : no need in basic of course, only here…)

Regards

@PYS: Thank you; I couldn’t figure out how to make it look right.

@jimk :why not quote the python-solutions from the same Thread

@karolus: Because it’s quicker for a typical user to copy and paste a Basic macro into the IDE than to set up a Python script. If the question was about macro programming, then I may have suggested Python, which is what I use most.

Worked for me… thanks!

Hallo

In Addition to the solutions in the link a solution to sort Sheet-tabs in Natural_sort_order

import re
rex = re.compile(r'(\d+|\D+)')

def natsort(stext):
    return [int(token) if token.isdigit()
            else token
            for token in rex.findall(stext)]

def sort_sheets_by_Name():
    doc = XSCRIPTCONTEXT.getDocument()
    sheets = doc.Sheets
    sheetnames = sheets.ElementNames
    for i, sheetname in enumerate(sorted(sheetnames, key=natsort)):
        sheets.moveByName(sheetname, i)

This looks even better than the solutions in the link. Can’t upvote though – not enough rep.