[Solved] Rename worksheets from sheet cells

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Fitch
Posts: 115
Joined: Mon Feb 22, 2010 6:01 pm

[Solved] Rename worksheets from sheet cells

Post by Fitch »

 Edit: Split from [Solved] Rename worksheet from the cell since this is a different question in a solved topic from a new person 
Even newer than "a70Coupe" as far as macros are concerned.
It does work very well.
Thanks for that.
I'm amazed I managed to "make" a macro..
Can I put a spanner in the works and ask if it could name all the sheets at once, or is that a bit too much?
Thanks.
Last edited by Hagar Delest on Fri Oct 01, 2021 11:39 pm, edited 1 time in total.
Reason: Tagged [Solved].
LibreOffice 5.1.4.2
Xenial Xerus
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Rename worksheet from the cell

Post by FJCC »

All of the sheets can be renamed. From where do you want to get the new names?
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.
Fitch
Posts: 115
Joined: Mon Feb 22, 2010 6:01 pm

Re: [Solved] Rename worksheet from the cell

Post by Fitch »

From C1 in each sheet.
I have 5 sheets at the moment, but more could follow, so it would have to count the number of sheets, an then rename each sheet to C1 of that sheet.
A bit of a tall order, but it would be handy.
Fitch.
LibreOffice 5.1.4.2
Xenial Xerus
FJCC
Moderator
Posts: 9280
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: [Solved] Rename worksheet from the cell

Post by FJCC »

Here is one solution.

Code: Select all

Sub Rename
oSheets = ThisComponent.Sheets
Cnt = oSheets.Count
For i = 0 to Cnt - 1
  oSheet = oSheets.getByIndex(i)
  oCell = oSheet.getCellrangeByName("C1")
  oSheet.Name = oCell.String
 Next i
End Sub
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.
Fitch
Posts: 115
Joined: Mon Feb 22, 2010 6:01 pm

Re: [Solved] Rename worksheet from the cell

Post by Fitch »

Wow!
That worked a treat.
Thank
LibreOffice 5.1.4.2
Xenial Xerus
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] Rename worksheet from the cell

Post by karolus »

Hallo

for each exists!

Code: Select all

Sub rename
sheets = thisComponent.Sheets
for each sheet in sheets  
  cell = sheet.getCellrangeByName("C1")
  sheet.Name = cell.String
next 
End Sub
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: [Solved] Rename worksheet from the cell

Post by Villeroy »

You may also select any cell with a sheet name, then all the sheets with sheet names in the same cell and run this:

Code: Select all

addr = ThisComponent.CurrentSelection.getRangeAddresses()
sheets = ThisComponent.getSheets()
for each a in addr
  sh = sheets.getByIndex(a.Sheet)
  c = sh.getCellByPosition(a.StartColumn, a.StartRow)
  sh.setName(c.getString())
next
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
Fitch
Posts: 115
Joined: Mon Feb 22, 2010 6:01 pm

Re:[Solved] Rename worksheets from sheet cells

Post by Fitch »

I see there's more than 1 way to skin a cat!

Thanks all.
LibreOffice 5.1.4.2
Xenial Xerus
Post Reply