Martin
Martin

Reputation: 25

Set specific ID to the slide

I have a Google Slides master document from which I copy the specific slides into an another Google Slides document. But when I copy the slide I want to give it a specific ID (e.g. Slide_num-001).

I do this by copying the slide, then I duplicate the copy with the ID I want and then I remove the copy. It works but it is far from ideal solution I think.

Could you please help me if there is a better way to do it? I couldn't find any function for this, like setObjectID() for example.

Upvotes: 1

Views: 1227

Answers (1)

NightEye
NightEye

Reputation: 11214

I'm sorry to say that there isn't a method to specifically set the ID of the slide. Also, it is not recommended to set the slide ID of an object as it is already created automatically in the background, that would be counter intuitive and at the same time, wasting resources. That's why it isn't also provided as a method.

I recommend creating a dictionary instead that holds the pairing of the Slide_num-<number> and the slide's ID.

I created a sample code that uses that idea.

Code:

function setDictionary() {
  // I have 5 slides as sample
  var presentation = SlidesApp.getActivePresentation();
  var slides = presentation.getSlides();
  var dictionary = {};

  // Set Object ID as value to key "Slide_num-<number>"
  slides.forEach(function (slide, index) {
    dictionary["Slide_num-" + padLeadingZeros(index + 1, 3)] = slide.getObjectId();
  });
  // In your case, after copying the slide, assign the value "slide.getObjectId()" to the key "Slide_num-<number>"

  // Sample: 
  // Sets 5th slide background to red 
  var slide = presentation.getSlideById(dictionary["Slide_num-005"]);
  Logger.log(slide.getBackground().setSolidFill(255,0,0));
}

function padLeadingZeros(num, size) {
  // Function that appends 0s to the number based on the size
  // Returns "001" when num is "1" and size is "3"
  var s = num + "";
  while (s.length < size) {
    s = "0" + s;
  } 
  return s;
}

The advantage of this is that it lessens the API calls it does, making it more independent from the API.

See reference of the available list of methods for the class slide.

Reference:

I sincerely apologize if this is not the solution you wanted/needed.

Upvotes: 1

Related Questions