【问题标题】:Downloading a Google Slides presentation as PowerPoint doc using Google Apps Script?使用 Google Apps 脚本将 Google 幻灯片演示文稿下载为 PowerPoint 文档?
【发布时间】:2018-09-27 11:59:07
【问题描述】:

Google 幻灯片的 GUI 提供以 Powerpoint (myFile.pptx) 形式下载 GSlides 演示文稿。我在 Google Apps 脚本文档中找不到对应的 - 任何指针?

编辑

感谢cmets和答案,我尝试了这个sn-p:

function testFileOps() {
  // Converts the file named 'Synthese' (which happens to be a Google Slide doc) into a pptx
  var files = DriveApp.getFilesByName('Synthese');
  var rootFolder = DriveApp.getRootFolder();
  while (files.hasNext()) {
    var file = files.next();
    var blobPptx = file.getBlob().getAs('application/vnd.openxmlformats-officedocument.presentationml.presentation');
    var result = rootFolder.createFile(blobPptx);
  }
}

它返回一个错误:

从 application/vnd.google-apps.presentation 转换为 应用程序/vnd.openxmlformats-officedocument.presentationml.presentation 不支持。 (第 7 行,文件“代码”)

第二次编辑

根据 cmets 中的另一个建议,我尝试从 Google App Script 进行 http 调用,直接将 gslides 转换为 pptx,没有大小限制。它会在 G Drive 上生成一个文件,但该文件已损坏/无法读取。 GAS 脚本:

function convertFileToPptx() {
  // Converts a public Google Slide file into a pptx
 var rootFolder = DriveApp.getRootFolder();
 var response = UrlFetchApp.fetch('https://docs.google.com/presentation/d/1Zc4-yFoUYONXSLleV_IaFRlNk6flRKUuAw8M36VZe-4/export/pptx');
 var blobPptx = response.getContent();
 var result = rootFolder.createFile('test2.pptx',blobPptx,MimeType.MICROSOFT_POWERPOINT);
}

注意事项:

  • 我得到了 pptx here 的 mime 类型
  • 使用 mime 类型 'pptx' 返回相同的错误消息

【问题讨论】:

  • 将文件导出为给定的 mimetype。请注意,由于 Apps 脚本不会在您的浏览器中运行,因此您需要将其作为 web 应用程序的一部分(返回 downloadAsFile)或在 Google 云端硬盘中创建工作文件。
  • 非常感谢!关于哪一行 GAS 将进行导出的任何指针?我在在线文档中找不到?
  • 您需要 Drive Service 或 Drive API(高级服务)。
  • 用代码 sn-p 编辑了问题。是否符合您推荐的方法?
  • 没有大小限制(目前为 10 Mb)将 Google 幻灯片导出到 pptx 的答案:stackoverflow.com/a/50680468/555121

标签: google-apps-script google-slides-api google-slides


【解决方案1】:

这个修改怎么样?

修改点:

  • response.getContent() 返回字节数组。所以请使用response.getBlob()。

修改脚本:

function convertFileToPptx() {
  var fileId = "1Zc4-yFoUYONXSLleV_IaFRlNk6flRKUuAw8M36VZe-4";
  var outputFileName = "test2.pptx";

  var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx';
  var rootFolder = DriveApp.getRootFolder();
  var response = UrlFetchApp.fetch(url);
  var blobPptx = response.getBlob();
  var result = rootFolder.createFile(blobPptx.setName(outputFileName));
}

注意:

  • 如果您想在您的 Google Drive 中转换未发布的 Google 幻灯片,请使用访问令牌。届时请将url修改如下。
    • var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx?access_token=' + ScriptApp.getOAuthToken();
  • DriveApp.createFile() 默认在根文件夹中创建一个文件。

参考资料:

【讨论】:

  • 效果很好。我以为我在响应中尝试过 getBlob() ,但显然没有……而且您对非公开文件的附加评论非常有用,谢谢!!
  • @seinecle 感谢您的快速回复。很高兴您的问题得到了解决。
【解决方案2】:

正如 tehhowch 所说,您可以从您的云端硬盘中获取 Google 幻灯片文件并将其作为 .pptx 获取。 (不确定 MIME 类型。)

File#getAs:

【讨论】:

  • 好的,我会在 G Slides => pptx 上专门试一试,然后再接受答案
  • 不急,如果有效请告诉我。我将来很可能需要使用它。
  • 我在问题中添加为 sn-p。
  • 如果设置 contentType = 'pplication/vnd.openxmlformats-officedocument.presentationml.presentation' - File#getAs 将导出到 pptx
【解决方案3】:

我使用令牌部分和特定文件夹添加所有修改

function convertFileToPptx() {
  var fileId = "Your File ID";
  var outputFileName = "name.pptx";

  var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx';
  //var rootFolder = DriveApp.getRootFolder();
  var rootFolder = DriveApp.getFolderById("Your Folder ID")
  var params = {method:"GET", headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};
  var response = UrlFetchApp.fetch(url,params);
  var blobPptx = response.getBlob();
  var result = rootFolder.createFile(blobPptx.setName(outputFileName));
}

【讨论】:

  • 你能解释一下你的答案吗?
猜你喜欢
  • 1970-01-01
  • 2020-03-19
  • 1970-01-01
  • 2020-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多