使用 Google Apps 脚本将 Google 幻灯片演示文稿下载为 PowerPoint 文档?

sei*_*cle 3 google-apps-script google-slides google-slides-api

Google Slides 的 GUI 提供将 GSlides 演示文稿下载为 Powerpoint (myFile.pptx)。我在 Google Apps Script 文档中找不到等效的东西 - 任何指针?

编辑

感谢评论和答案,我尝试了这个片段:

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

它返回一个错误:

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

第二次编辑

根据评论中的另一个建议,我尝试从 Google App Script 进行 http 调用,这会将幻灯片直接转换为 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);
}
Run Code Online (Sandbox Code Playgroud)

笔记:

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

Tan*_*ike 5

这个改装怎么样?

修改点:

  • 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));
}
Run Code Online (Sandbox Code Playgroud)

笔记:

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

参考: