3

谷歌幻灯片的 GUI 提供下载 GSlides 演示文稿作为 Powerpoint (myFile.pptx)。我在 Google Apps 脚本文档中找不到等效项 - 任何指针?

编辑

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

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 转换为 application/vnd.openxmlformats-officedocument.presentationml.presentation。(第 7 行,文件“代码”)

第二次编辑

根据评论中的另一个建议,我尝试从 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);
}

笔记:

4

3 回答 3

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

笔记:

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

参考:

于 2018-10-01T08:16:06.123 回答
0

正如 tehhowch 所提到的,您可以从 Drive 中获取 Google Slide 文件并将其作为.pptx. (不确定 MIME 类型。)

File#getAs

于 2018-09-27T13:22:47.473 回答
0

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

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));
}
于 2021-04-27T06:22:25.493 回答