Downloading a Google Slides presentation as PowerPoint doc using Google Apps Script?

家住魔仙堡 提交于 2019-12-20 03:22:14

问题


The GUI of Google Slides offers to download a GSlides presentation as a Powerpoint (myFile.pptx). I could not find the equivalent in the Google Apps Script documentation - any pointer?

EDIT

Thanks to comments and answers, I tried this snippet:

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

It returns an error:

Converting from application/vnd.google-apps.presentation to application/vnd.openxmlformats-officedocument.presentationml.presentation is not supported. (line 7, file "Code")

SECOND EDIT

As per another suggestion in comments, I tried to make an http call from Google App Script, that would directly convert the gslides into pptx, without size limit. It produces a file on G Drive, but this file is corrupted / unreadable. The GAS script:

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

Notes:

  • I got the mime type for pptx here
  • using the mime type 'pptx' returns the same error message

回答1:


How about this modification?

Modification point:

  • response.getContent() returns byte array. So please use response.getBlob().

Modified script:

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

Note:

  • If you want to convert Google Slides, which are not published, in your Google Drive, please use access token. At that time please modify url as follows.
    • var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx?access_token=' + ScriptApp.getOAuthToken();
  • DriveApp.createFile() creates a file on root folder as the default.

References:

  • Class HTTPResponse
  • getOAuthToken()



回答2:


As mentioned by tehhowch, you could get the Google Slide file from your Drive and get it as a .pptx. (Not sure of mime type.)

File#getAs:



来源:https://stackoverflow.com/questions/52536507/downloading-a-google-slides-presentation-as-powerpoint-doc-using-google-apps-scr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!