代码之家  ›  专栏  ›  技术社区  ›  seinecle

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

  •  2
  • seinecle  · 技术社区  · 6 年前

    googleslides的GUI提供下载GSlides演示文稿作为Powerpoint(myFile.pptx)。我在googleapps脚本文档中找不到等价的东西-任何指针?

    感谢评论和回答,我尝试了以下片段:

    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

    第二次编辑

    根据评论中的另一个建议,我尝试从googleappscript发出一个http调用,直接将gslides转换为pptx,而不受大小限制。它在G驱动器上生成一个文件,但该文件已损坏/无法读取。气体脚本:

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

    笔记:

    • here
    • 使用mime类型 'pptx' 返回相同的错误消息
    2 回复  |  直到 6 年前
        1
  •  5
  •   Tanaike    6 年前

    这次修改怎么样?

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

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

        2
  •  0
  •   tehhowch    6 年前

    正如tehhowch所提到的,您可以从驱动器中获取Google幻灯片文件并将其作为 .pptx . (不确定mime类型。)

    File#getAs :

        3
  •  0
  •   Jonny Alexander Aristizabal    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));
    }