0

I'm working on an automated slide setup and depending on some opt-out variables I need to remove some of the slides if they are not desired in the final output. To solve this I have created a script that adds a simple text string {{remove-this-slide}} to the slides that need to be deleted.

However, when trying to get a script to delete the slides containing that string it keeps deleting my entire presentation...

This is what I have:

function deleteFunction() {
var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides();
  for (i = 0; i < currentPresentationSlide.length; i++) {
    if (currentPresentationSlide[i].getPageElements().indexOf('{{remove-this-slide}}') > -1); {
    currentPresentationSlide[i].remove();
  }
 }
}

Can anyone figure out what's going wrong here?

Kos
  • 4,890
  • 9
  • 38
  • 42
Jonas Bæk
  • 100
  • 9
  • You have an empty loop body due to the semicolon between your `if` condition and the opening brace. – tehhowch Apr 17 '18 at 01:00
  • Did my answer work? Would you please tell me about it? That is also useful for me to study. If this works, other people who have the same issue with you can also base your question as a question which can be solved. – Tanaike Apr 19 '18 at 23:15
  • Hi Tanaike, Sorry - yes it did indeed fix it! Thank you very much! – Jonas Bæk Apr 20 '18 at 08:30
  • Welcome. I'm glad your issue was solved. Thank you, too! – Tanaike Apr 20 '18 at 08:31

2 Answers2

1

How about this modification?

Modification points :

  • The reason that the entire slides are deleted is ; after if (currentPresentationSlide[i].getPageElements().indexOf('{{remove-this-slide}}') > -1);. By this ;, if doesn't work and currentPresentationSlide[i].remove(); is always run.
  • The text data cannot be retrieved from currentPresentationSlide[i].getPageElements(). When you want to search the text from the text box, please use currentPresentationSlide[i].getShapes().
    • From your question, I was not sure where you want to search the text from. So I supposed that you want to search the text from shapes. The shape includes the text box.

Modified script :

function deleteFunction() {
  var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides();
  for (i = 0; i < currentPresentationSlide.length; i++) {
    var shapes = currentPresentationSlide[i].getShapes();
    for (j = 0; j < shapes.length; j++) {
      if (shapes[j].getText().asString().indexOf('{{remove-this-slide}}') > -1) {
        currentPresentationSlide[i].remove();
      }
    }
  }
}

Reference :

If I misunderstand your question, I'm sorry.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • For a simple typographical error, comment the solution and flag for closure. – tehhowch Apr 17 '18 at 00:59
  • @tehhowch Thank you for your comment. I apologize I didn't know about the rule. Should I delete my answer? – Tanaike Apr 17 '18 at 01:38
  • Your answer adds information in excess of the typo, which may resolve a future issue (which is arguably more related to the OP's eventual question, once the typo is resolved). I think it is OK to keep. – tehhowch Apr 17 '18 at 01:53
  • @tehhowch Thank you for the polite reply. I would like to leave my this answer in the hope of solving OP's issue. When I found the answers which have only typographical error after this, I will comment it and flag for closure. – Tanaike Apr 17 '18 at 02:03
0

There is a small bug in the code from @Tanaike. Because there might be more shapes in the same slide, you have to break the loop after deleting the slide.

Otherwise the code tries to traverse the shapes of a deleted slide, producing an error.

So the correct snippet looks like:

function deleteFunction() {
  var currentPresentationSlide = SlidesApp.getActivePresentation().getSlides();
  for (i = 0; i < currentPresentationSlide.length; i++) {
    var shapes = currentPresentationSlide[i].getShapes();
    for (j = 0; j < shapes.length; j++) {
      if (shapes[j].getText().asString().indexOf('{{remove-this-slide}}') > -1) {
        currentPresentationSlide[i].remove();
        break;
      }
    }
  }
}
brasofilo
  • 25,496
  • 15
  • 91
  • 179