4

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
seinecle
  • 10,118
  • 14
  • 61
  • 120
  • 1
    Export the file as a given mimetype. Note that since Apps Script doesn't run in your browser, you'll need to either do this as part of a webapp (return `downloadAsFile`) or create the effort file in Google Drive. – tehhowch Sep 27 '18 at 13:05
  • thx a lot! Any pointer as to which line of GAS would do the export? I couldn't find it in the online doc? – seinecle Sep 27 '18 at 13:10
  • 1
    You need Drive Service or the Drive API (advanced service). – tehhowch Sep 27 '18 at 13:15
  • Edited the question with a code snippet. Does it correspond to the approach you recommend? – seinecle Sep 27 '18 at 14:30
  • Thx. Changed the code accordingly in the OP. The error message shows it recognizes the Mime type of the gslides doc. But the error still occurs. – seinecle Sep 27 '18 at 15:10
  • Try file.getAs() instead of file.getBlob.getAs() - Going to test this quickly. Getting the same error using this method: Converting from application/vnd.google-apps.presentation to application/vnd.openxmlformats-officedocument.presentationml.presentation is not supported. (line 6, file "Code") - My code is slightly modified as I am getting the file by ID instead of name. – New_2_Code Sep 27 '18 at 15:11
  • 2
    There is answer for exporting Google Slides to pptx without size limit (which currently is 10 Mb): https://stackoverflow.com/a/50680468/555121 – Kos Sep 27 '18 at 15:18
  • great! That http call must be done from outside of G Apps Script however... so you need a server somewhere. I'll read further, maybe GAS can make http calls, read the result (the pptx file) and save it to the drive. What a pain. – seinecle Sep 27 '18 at 15:35
  • @seinecle If your issue was solved, can you post it as an answer? By this, it will be also useful for other users. – Tanaike Sep 30 '18 at 22:54
  • It is not solved, the trick to do it via an http call works only from outside Google Apps Script, which is not what I asked about. So far I'd say conversion to pptx from Google Apps Script is impossible – seinecle Oct 01 '18 at 05:16
  • @seinecle Thank you for replying. I misunderstood about your situation. I'm really sorry. Can you provide your latest script? I would like to confirm it. If you can do, please add it to your question. By the way, about what you want, you want to convert from Google Slides to Microsoft powerpoint using Google Apps Script, and create it as a file in your Google Drive. Is my understanding correct? – Tanaike Oct 01 '18 at 07:16
  • Yes that's exactly this. – seinecle Oct 01 '18 at 07:24
  • @seinecle Thank you for quick reply . Can you provide your latest script? I would like to confirm it. If you can do, please add it to your question. – Tanaike Oct 01 '18 at 07:29
  • edited the question with the script I used. – seinecle Oct 01 '18 at 07:42
  • @seinecle Thank you for updating. I posted an answer. Could you please confirm it? If this didn't work, please tell me. I would like to modify it. – Tanaike Oct 01 '18 at 08:16
  • Did anything work? I am getting corrupt file or the similar error as well with my script too. Any solutions? https://stackoverflow.com/questions/52998990/how-to-attach-google-slides-with-gmailapp-sendemail – WiData Oct 26 '18 at 00:43
  • Yes the accepted solution worked. – seinecle Oct 26 '18 at 06:38

4 Answers4

6

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:

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 1
    works great. I thought I had tried getBlob() on the response but apparently not... And your additional comment on non public files is very useful, thx!! – seinecle Oct 01 '18 at 08:41
  • @seinecle Thank you for quick reply. I'm glad your issue was solved. – Tanaike Oct 01 '18 at 08:43
0

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:

tehhowch
  • 9,645
  • 4
  • 24
  • 42
New_2_Code
  • 330
  • 2
  • 18
0

I add all modifications with token part and specific folder

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

To get the byte[] do:

function downloadAsPPTX(){

  var presentation = SlidesApp.getActivePresentation();
     
  var fileId = presentation.getId();               
  var url = 'https://docs.google.com/presentation/d/' + fileId + '/export/pptx';
  
  var response = UrlFetchApp.fetch(url);      
  var blobPptx = response.getBlob();
   Logger.log("size: "+blobPptx.getBytes().length);

}
Nathan B
  • 1,625
  • 1
  • 17
  • 15