Google Suite - Apps Script - Download Slides as PNG files via API

前端 未结 2 1818
情歌与酒
情歌与酒 2021-01-25 22:56

Good Morning All. I have written a short script which batch-creates [single page] Google Slides based on rows from a spreadsheet. While in the loop for each creation, I would

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-25 23:36

    Yes, It is possible.

    You can use Google slide API and make a PNG file of every page of Google slide.

    Here is the code, BUT first you have to enable API as following

    1. open script editor
    2. click on resources-> Advanced Google Services
    3. Enable Google slide API and Drive API .
    4. click on ok

    now copy and paste this code, and write your slide ID in presentationID.

    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 Googel 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;
    }
    

提交回复
热议问题