Search code examples
google-apps-scriptgoogle-drive-apigoogle-slides-api

When trying to convert ppt to Google Slide receive converting error


In my google script program, I am trying to iterate over a folder and make all of the ppt files into google slide files.

function makeSlides(url) {
  slideUrls = []; 
  var id = getId(url);
  var powerPoints = DriveApp.getFolderById(id).getFilesByType(MimeType.MICROSOFT_POWERPOINT);

  // turn ppt into slides
  while(powerPoints.hasNext()) {
    var powerPoint = powerPoints.next()
    try{
      var sheet = powerPoint.getBlob().getAs(MimeType.GOOGLE_SLIDES);
      DriveApp.getFolderById(url).createFile(sheet)
      Logger.log("OK " + powerPoint.getName());
    }catch(e) {
      Logger.log("ERROR: " + e) 
    }
 }

After checking the logs I get an error

Exception: Converting from application/vnd.openxmlformats-officedocument.presentationml.presentation to application/vnd.google-apps.presentation is not supported.

I know within the UI of Google Drive, you can open a ppt as a Google Slide. Is there any work around to this? Or am I doing it wrong?

I did find this but this is the opposite of what I am trying to accomplish.


Solution

  • It cannot convert from Powerpoint format to Google Slides using getAs(). You can achieve this using Drive API. In this modification, I used Drive API using Advanced Google Services.

    When you use this script, please enable Drive API at Advanced Google Services and API console. You can see about this at here.

    Modified script:

    Please modify as follows.

    From:
    var sheet = powerPoint.getBlob().getAs(MimeType.GOOGLE_SLIDES);
    DriveApp.getFolderById(url).createFile(sheet)
    
    To:
    Drive.Files.insert({title: powerPoint.getName(), mimeType: MimeType.GOOGLE_SLIDES}, powerPoint.getBlob());
    

    Note:

    • In this modified script, the converted file is created to the root folder. If you want to create in the specific folder, please modify from {title: powerPoint.getName(), mimeType: MimeType.GOOGLE_SLIDES} to {title: powerPoint.getName(), mimeType: MimeType.GOOGLE_SLIDES, parents: [{id: folderId}]}.
    • If you want to retrieve the file ID from the converted file, please use var id = Drive.Files.insert({title: powerPoint.getName(), mimeType: MimeType.GOOGLE_SLIDES}, powerPoint.getBlob()).id.

    References:

    If I misunderstand your question, please tell me. I would like to modify it.