Google Suite-Apps脚本-通过API将幻灯片下载为PNG文件 [英] Google Suite - Apps Script - Download Slides as PNG files via API

查看:109
本文介绍了Google Suite-Apps脚本-通过API将幻灯片下载为PNG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家早上好.我写了一个简短的脚本,该脚本基于电子表格中的行批量创建[单页] Google幻灯片.在每次创建循环中,我想在Google云端硬盘中创建幻灯片的PNG(或在用户桌面上下载).这些图片应与用户单击文件>下载> PNG时的规格相同-粗细的小文本需要完整的投影机高清-因此,我不相信我可以使用缩略图"功能,该功能看起来仅限于1600像素.

我的下面的代码生成错误不支持从text/html转换为image/png"-因此,我不确定这是API的限制还是编码问题.预先谢谢你.

  var options =
     {
       "contentType" : "image/PNG"
     };
  var url = 'https://docs.google.com/presentation/d/' + presentationCopyId + '/export/PNG';
  var response = UrlFetchApp.fetch(url, options);
  var image = response.getAs(MimeType.PNG);
  image.setName(SlideName);
  DriveApp.createFile(image);  

解决方案

是的,有可能.

您可以使用Google幻灯片API并为Google幻灯片的每个页面制作一个PNG文件.

这是代码, 但是首先您必须启用以下API

  1. 打开脚本编辑器
  2. 点击资源->进阶Google服务
  3. 启用Google幻灯片API和Drive API.
  4. 单击确定

现在复制并粘贴此代码, 并在presentationID中写入您的幻灯片ID.

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

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 like to create a PNG of the Slide in Google Drive (or download on the user's desktop). These pictures should be the same specs as if a user clicked File>Download>PNG - the heavy small text requires full projector HD - so I don't believe I can use the "Thumbnail" function which appears limited to 1600 pixels.

My code below generates the error "Converting from text/html to image/png is not supported" - so I'm not sure if this is a limitation of the API or a problem with my coding. Thank you in advance.

  var options =
     {
       "contentType" : "image/PNG"
     };
  var url = 'https://docs.google.com/presentation/d/' + presentationCopyId + '/export/PNG';
  var response = UrlFetchApp.fetch(url, options);
  var image = response.getAs(MimeType.PNG);
  image.setName(SlideName);
  DriveApp.createFile(image);  

解决方案

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

这篇关于Google Suite-Apps脚本-通过API将幻灯片下载为PNG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆