Search code examples
google-apps-scriptgoogle-slides

Exception: This presentation is closed, and its contents cannot be updated


I have this code (thanks to Tanaike) and it's working fine until I didn't use it for quite some time.

I tried to use it today and it gives off an error of: Exception: This presentation is closed, and its contents cannot be updated. The presentation is opened I'm the only one who can access it.

function cvqautomation() {
  var presentationId = "1NI05tMvTqOT2jVCbozD_hcH5UoU_hjRHNKRkCSTxwko"; // Please set your presentation ID of Google Slide.
  var sheet = SpreadsheetApp.getActiveSheet(); // In this case, the active sheet is used. If you want to use the specific sheet, please modify this to SpreadsheetApp.getActiveSpreadsheet().getSheetByName("###Sheet name###")
  var range = sheet.getRange("A2:B" + sheet.getLastRow());
  var presentation = SlidesApp.openById(presentationId);
  
  // Get the template slide and static slide by their indices.
  var templateSlide = presentation.getSlides()[0];
  var staticSlide = presentation.getSlides()[1]; // Change the index to match the static slide's position.
  
  range.getDisplayValues().forEach(([name, email]) => {
    if (!name || !email) return;
    
    // Copy the template slide and set the name text.
    var newTemplateSlide = presentation.appendSlide(templateSlide);
    newTemplateSlide.getShapes()[4].getText().setText(name);
    
    // Copy the static slide.
    var newStaticSlide = presentation.appendSlide(staticSlide);
    
    presentation.saveAndClose();
    
    // Send email with thumbnails of both slides.
    var templateThumbnail = Slides.Presentations.Pages.getThumbnail(presentationId, newTemplateSlide.getObjectId(), { "thumbnailProperties.thumbnailSize": "LARGE" }).contentUrl;
    var staticThumbnail = Slides.Presentations.Pages.getThumbnail(presentationId, newStaticSlide.getObjectId(), { "thumbnailProperties.thumbnailSize": "LARGE" }).contentUrl;

    var emailBody = '<div style="text-align: center;">' +
      '<div style="text-align: center;">' +
      '<img src="cid:image1"><br><img src="cid:image2"><br>' +
      '</div>' +
      '<div>' + // No CSS style for font-size, use HTML tags
      '<p><span style="font-size: medium;">' +
      'Please don\'t forget to bring your mobile phones with you and make sure to download the PingID application from the App Store (see the app icon below or click <a href="https://www.example.com">here</a>). This will serve as our verification tool!' +
      '</span></p>' +
      '</div>' +
      '</div>';
    
    MailApp.sendEmail({
      to: email,
      subject: "Welcome to TaskUs!", // Please set your email title.
      htmlBody: emailBody,
      inlineImages: {
        image1: UrlFetchApp.fetch(templateThumbnail).getBlob(),
        image2: UrlFetchApp.fetch(staticThumbnail).getBlob()
      }
    });

    SpreadsheetApp.getUi().alert('Email sent successfully!');
  });
}

Solution

  • Modification points:

    • In your script, at presentation.saveAndClose();, presentation is saved and closed. This is used for creating the thumbnail using Slides API. And, in order to continue the loop, it is required to reopen presentation.

    When this point is reflected in your script, how about the following modification?

    From:

    presentation.saveAndClose();
    

    To:

    presentation.saveAndClose();
    presentation = SlidesApp.openById(presentationId); // Added
    

    Reference: