0

I have been using a script so far to make png thumbnails of my Google Slides Presentations with Google Apps Script. However, I wonder how I can limit it to only be the first slide and not for each slide? I tried various ways but always failed

 function generateScreenshots() {
  
  var presentationId = "***ADD YOUR Google Slide ID Only***";
  var presentation = SlidesApp.openById(presentationId);
  var baseUrl =
    "https://slides.googleapis.com/v1/presentations/{presentationId}/pages/{pageObjectId}/thumbnail";
  var parameters = {
    method: "GET",
    headers: { Authorization: "Bearer " + ScriptApp.getOAuthToken() },
    contentType: "application/json",
    muteHttpExceptions: true
  };

  // Log URL of the main thumbnail of the deck
  Logger.log(Drive.Files.get(presentationId).thumbnailLink);

  // For storing the screenshot image URLs
  var screenshots = [];

  var slides = presentation.getSlides().forEach(function(slide, index) {
    var url = baseUrl
      .replace("{presentationId}", presentationId)
      .replace("{pageObjectId}", slide.getObjectId());
    var response = JSON.parse(UrlFetchApp.fetch(url, parameters));

    // Upload Google Slide image to Google Drive
    var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();
    DriveApp.createFile(blob).setName("Image " + (index + 1) + ".png");

    screenshots.push(response.contentUrl);
  });

  return screenshots;
}

How can I be more specific in this line indicating that I only want to have Slide 1?

presentation.getSlides().forEach(function(slide, index) {

I tried my way with a specific selection type as documented here https://developers.google.com/apps-script/guides/slides/selecting but failed with that method.

Henry2004
  • 33
  • 4

1 Answers1

1

Modification points:

  • forEach doesn't return values.

  • When you want to retrieve 1st slide by modifying presentation.getSlides().forEach(function(slide, index) {, you can modify as follows.

    • From

        presentation.getSlides().forEach(function(slide, index) {
      
    • To

        [presentation.getSlides()[0]].forEach(function(slide, index) {
      

I thought that your script might become a bit simple to retrieve the 1st slide. The sample modified script is as follows.

Modified script:

From:

var screenshots = [];

var slides = presentation.getSlides().forEach(function(slide, index) {
  var url = baseUrl
    .replace("{presentationId}", presentationId)
    .replace("{pageObjectId}", slide.getObjectId());
  var response = JSON.parse(UrlFetchApp.fetch(url, parameters));

  // Upload Google Slide image to Google Drive
  var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();
  DriveApp.createFile(blob).setName("Image " + (index + 1) + ".png");

  screenshots.push(response.contentUrl);
});

return screenshots;

To:

var slide = presentation.getSlides()[0];
var url = baseUrl
  .replace("{presentationId}", presentationId)
  .replace("{pageObjectId}", slide.getObjectId());
var response = JSON.parse(UrlFetchApp.fetch(url, parameters));
var blob = UrlFetchApp.fetch(response.contentUrl).getBlob();
DriveApp.createFile(blob).setName("Image 1.png");
return [response.contentUrl];
  • In this modification, for your current script, the returned value is an array. When you can directly return the URL, please modify the above script.

Reference:

Tanaike
  • 181,128
  • 11
  • 97
  • 165