google-apps-scriptgoogle-sheetsgoogle-slides

Saving google slides as images and then inserting the image ID/URL into google sheets


I am trying to create customized invitations. I have a list of names and email addresses in a google sheets file. The name of the guest is inserted into a google slides file and then saved as a thumbnail which can then be emailed. However, I want the image to also be saved in a specified location on my drive, and I want the image ID/URL for each guest to be inserted in Row C for the corresponding person.

My code before modification.

function changeSlideTextAndDuplicate() {
  var presentationId = "11i9F3qH-xX1c4FGO6cSSAaTGNwTRowk-6W183lntLRA"; // 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);
  var templateSlide = presentation.getSlides()[0];
  range.getDisplayValues().forEach(([name, email]) => {
    if (!name || !email) return;
    var newSlide = presentation.appendSlide(templateSlide);
    newSlide.getShapes()[0].getText().setText(name);
    presentation.saveAndClose();
    var { contentUrl } = Slides.Presentations.Pages.getThumbnail(presentationId, newSlide.getObjectId(), { "thumbnailProperties.thumbnailSize": "LARGE" });
    
    MailApp.sendEmail({
      to: email,
      subject: "Invitation to the walima of Ameen to Aqeela", // Please set your email title.
      htmlBody: "Assalamu Alaikum Dear" +" " + name + "." + "\n" + "\n" + "We would love for you to attend the walima of Ameen to Aqeela. Please see the invitation attached",
      inlineImages: { image1: UrlFetchApp.fetch(contentUrl).getBlob() }
    });
    presentation = SlidesApp.openById(presentationId);
  });
}

I modified the code as below but it is not able to execute the code citing that there is an error in getting the thumbnail:

function changeSlideTextAndDuplicate2() {
  var presentationId = "11i9F3qH-xX1c4FGO6cSSAaTGNwTRowk-6W183lntLRA";
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange("A2:B" + sheet.getLastRow());
  var presentation = SlidesApp.openById(presentationId);
  var templateSlide = presentation.getSlides()[0];

  range.getDisplayValues().forEach(([name, email]) => {
    if (!name || !email) return;
    var newSlide = presentation.appendSlide(templateSlide);
    newSlide.getShapes()[0].getText().setText(name);

    // Generate a unique ID for the image (you can use Utilities.getUuid() or any other method)
    var imageId = Utilities.getUuid();

    // Save the image ID in Google Sheets
    var imageIdColumn = sheet.getRange(sheet.getLastRow() + 1, 3); // Assuming the image IDs will be stored in column C
    imageIdColumn.setValue(imageId);

   // Get the objectId of the new slide
var newSlideObjectId = newSlide.getObjectId().replace("SLIDES_API", "");

try {
  // Make the API call with the correct objectId
  var { contentUrl } = Slides.Presentations.Pages.getThumbnail(presentationId, newSlideObjectId, { "thumbnailProperties.thumbnailSize": "LARGE" });

  MailApp.sendEmail({
    to: email,
    subject: "Invitation to the walima of Ameen to Aqeela",
    htmlBody: "Assalamu Alaikum Dear" ...",
    inlineImages: { image1: UrlFetchApp.fetch(contentUrl).getBlob() }
  });
} catch (error) {
  Logger.log("Error getting thumbnail. Slide ID:", newSlideObjectId);
  Logger.log("Error details:", error);
}


  });

  // Close the presentation after all slides are processed
  presentation.saveAndClose();
}

Solution

  • I believe your goal is as follows.

    Modification points:

    Modified script:

    Please set your presentationId and folderId.

    function changeSlideTextAndDuplicate() {
      var presentationId = "11i9F3qH-xX1c4FGO6cSSAaTGNwTRowk-6W183lntLRA"; // Please set your presentation ID of Google Slide.
      var folderId = "###"; // Please set your folder ID. The images are saved into this folder.
    
      var folder = DriveApp.getFolderById(folderId);
      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:C" + sheet.getLastRow());
      var presentation = SlidesApp.openById(presentationId);
      var templateSlide = presentation.getSlides()[0];
      var values = range.getDisplayValues().map(([name, email, url]) => {
        if (!name || !email) return [""];
        if (name && email && url) return [url];
        var newSlide = presentation.appendSlide(templateSlide);
        newSlide.getShapes()[0].getText().setText(name);
        presentation.saveAndClose();
        var { contentUrl } = Slides.Presentations.Pages.getThumbnail(presentationId, newSlide.getObjectId(), { "thumbnailProperties.thumbnailSize": "LARGE" });
        var blob = UrlFetchApp.fetch(contentUrl).getBlob().setName(name);
        MailApp.sendEmail({
          to: email,
          subject: "Invitation to the walima of Ameen to Aqeela", // Please set your email title.
          htmlBody: "Assalamu Alaikum Dear" + " " + name + "." + "\n" + "\n" + "We would love for you to attend the walima of Ameen to Aqeela. Please see the invitation attached",
          attachments: [blob]
        });
        presentation = SlidesApp.openById(presentationId);
        return [folder.createFile(blob).getUrl()];
      });
      range.offset(0, 2, values.length, 1).setValues(values);
    }