Ordering sheets

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Letto
Posts: 1
Joined: Mon Apr 02, 2012 11:29 pm

Ordering sheets

Post by Letto »

I'd like to know how to order may sheets in an alphabetical order. For example I have many patients and each one has its own sheet. I need to order them to find a patient easily.
Thanks
BrOffice.org 3.0.0 OOO300m9
OS Windows Xp
FJCC
Moderator
Posts: 9574
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Ordering sheets

Post by FJCC »

I don't know of a built in way to do that. Here is a simple macro that should work

Code: Select all

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
OpenOffice 4.1 on Windows 10 and Linux Mint
If your question is answered, please go to your first post, select the Edit button, and add [Solved] to the beginning of the title.
User avatar
Villeroy
Volunteer
Posts: 31349
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Ordering sheets

Post by Villeroy »

Python can sort lists:

Code: Select all

import uno
def sortSheets():
    doc = XSCRIPTCONTEXT.getDocument()
    sheets = doc.getSheets()
    c = sheets.getCount()
    t = sheets.getElementNames()
    l = list(t)
    l.sort()
    for n in range(c):
        sheets.moveByName(l[n],n)
g_exportedScripts = sortSheets,
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
karolus
Volunteer
Posts: 1234
Joined: Sat Jul 02, 2011 9:47 am

Re: Ordering sheets

Post by karolus »

Hallo
@Villeroy:

slightly more readable:

Code: Select all

def sheets_sort():
    
    doc = XSCRIPTCONTEXT.getDocument()
    sheets = doc.getSheets()
    sheetnames = sorted( sheets.getElementNames() )
    for i, sheet in enumerate( sheetnames ):
        sheets.moveByName( sheet, i)
Karo
Libreoffice 25.2… on Debian 13 (trixie) (on RaspberryPI5)
Libreoffice 25.8… flatpak on Debian 13 (Bookworm) (on RaspberryPI5)
User avatar
Villeroy
Volunteer
Posts: 31349
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Ordering sheets

Post by Villeroy »

karolus wrote:Hallo
@Villeroy:

slightly more readable:

Karo
Thank you. That's a lot more readable and shorter. I'm not very profiecient in Python programming and did not even search for a built-in function. Instead I accessed the list object I knew to be sortable.
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
hanya
Volunteer
Posts: 885
Joined: Fri Nov 23, 2007 9:27 am
Location: Japan

Re: Ordering sheets

Post by hanya »

It seems Letto uses BrOffice.org 3.0.0 OOO300m9 which uses Python 2.3.X. sorted function is new feature of Python 2.4, so importing the function from __feature__ module is required, I suppose.
Please, edit this thread's initial post and add "[Solved]" to the subject line if your problem has been solved.
Apache OpenOffice 4-dev on Xubuntu 14.04
kaybee1
Posts: 1
Joined: Fri Nov 21, 2014 11:02 am

Re: Ordering sheets

Post by kaybee1 »

Hi,
how to change that basic macro to sort all the sheets except the first and second, and a sheet named 'NoSortSheet'? I can not deal with it :( , please help.
OpenOffice 3.4.1 on WinXP/Win7/Win8
Post Reply