[Solved]Delete multiple sheets (Alternative for removebyname

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

[Solved]Delete multiple sheets (Alternative for removebyname

Post by sokolowitzky »

Hello,

I've been looking around for this for a while, but I could not find any answer.
How can I delete two sheets without writing their names on macro?

The only way to delete a sheet that I could find is;

Dim Doc As Object
Doc = ThisComponent
Doc.Sheets.removeByName("Sheet2")

but what I need is something like this; (I know there is no such a code, I just made it up to show what I'm looking for)
Dim Doc As Object
Doc = ThisComponent
Doc.getSheets.getByIndex(1,2).remove
Last edited by sokolowitzky on Tue Jul 31, 2018 3:30 am, edited 1 time in total.
Win10-OpenOffice 4.1/LibreOffice 7.4
FJCC
Moderator
Posts: 9274
Joined: Sat Nov 08, 2008 8:08 pm
Location: Colorado, USA

Re: Delete multiple sheets (Alternative for removebyname)

Post by FJCC »

Here is a list of all of the methods for the Sheets container and I don't see any way to remove one except removeByName(). You can use getElementNames to get an array of sheet names and iterate over that. Why can't you use removeByName?

Code: Select all

(Name)
acquire                  
copyByName             
createEnumeration  
getByIndex  
getByName  
getCellByPosition  
getCellRangeByPosition
getCellRangesByName  
getCount  
getElementNames
getElementType   
getImplementationId 
getImplementationName 
getSupportedServiceNames
getTypes   
hasByName
hasElements
insertByName
insertNewByName
moveByName   
queryAdapter   
queryInterface 
release            
removeByName
replaceByName
supportsService
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: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Delete multiple sheets (Alternative for removebyname)

Post by Villeroy »

WRONG:

Code: Select all

shx = ThisComponent.getSheets()
for i = 1 to 3
  sh = shx.getByIndex(i)
  s = sh.getName()
  shx.removeByName(s)
next
After you removed i=1, the next one will be #2 which used to be #3 before the deletion.
After you removed i=2, the next one will be #3 which used to be #5 before the deletion.

BETTER:

Code: Select all

shx = ThisComponent.getSheets()
for i = 3 to 1 step -1
  sh = shx.getByIndex(i)
  s = sh.getName()
  shx.removeByName(s)
next
ALTERNATIVE:

Code: Select all

shx = ThisComponent.getSheets()
for each i in Array(9,7,5,3,1)
  sh = shx.getByIndex(i)
  s = sh.getName()
  shx.removeByName(s)
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
mcmurchy1917
Posts: 23
Joined: Fri Feb 22, 2013 2:15 pm

Re: Delete multiple sheets (Alternative for removebyname)

Post by mcmurchy1917 »

This is how I delete all sheets except the first -

Code: Select all

		oSheets = oDoc.Sheets
		aSheetNames() = oSheets.getElementNames()
		for iSheet = 1 to ubound(aSheetNames)
		        oSheets.removeByName( aSheetNames(iSheet))
		next iSheet
Slackware user
sokolowitzky
Posts: 103
Joined: Mon Sep 15, 2014 7:34 pm

Re: Delete multiple sheets (Alternative for removebyname)

Post by sokolowitzky »

Thanks to all. Each post has thought me a different thing.
Win10-OpenOffice 4.1/LibreOffice 7.4
Post Reply