Search code examples
javascriptgoogle-apps-scriptenumsgoogle-slides

How to access ENUMs in Google Apps Script?


I can't find any documentation that explains how ENUMs can be utilized in Google Apps Script. For example, I am trying to duplicate the last slide in a presentation. The documentation says:

To call an enum, you call its parent class, name, and property. For example, SlidesApp.SlidePosition.LAST_SLIDE.

However, it doesn't give any syntax for how to actually invoke this.

Assuming that "pres" is the presentation itself, let pres = SlidesApp.getActivePresentation();, I have tried:

pres.getSlides()[SlidesApp.SlidePosition.LAST_SLIDE].duplicate();
pres.getSlides(SlidesApp.SlidePosition.LAST_SLIDE).duplicate();
SlidesApp.SlidePosition.LAST_SLIDE.duplicate();
duplicate(SlidesApp.SlidePosition.LAST_SLIDE);
pres.SlidesApp.SlidePosition.LAST_SLIDE.duplicate();
pres.getSlidesById(SlidesApp.SlidePosition.LAST_SLIDE).duplicate();

I just get a bunch of errors. I know this is a simple syntax question, but I can't find any information on Google at all. How do I actually access the last slide using this ENUM?


Solution

  • The Slides Service of Google Apps Script has six classes having a method that use the SlidePosition Enum. The method is setLinkSlide(slidePosition)

    The brief description of this method is:

    Sets a Link to the given Slide using the relative position of the slide.

    The classes are

    1. Class Image
    2. Class Line
    3. Class Shape
    4. Class SheetChart
    5. Class TextStyle
    6. Class WordArt

    Reference

    To get the last slide you might use

    function getLastSlide(){
      return SlidesApp.getActivePresentation().getSlides().slice(-1)[0];
    
    }
    

    Example of use of the above function.

    This function sets the slide title assuming that the slide layout is one of those having a title shape as the first shape, like the layout "Title and body",

    function setTitle(){
        getLastSlide().getShapes()[0].getText().setText('This is the last slide')
    }