'How to convert .docx files to Google Docs with Apps Script?

Does anyone has experience in convert a batch of .docx file, stored in Google Drive, to Google Docs format?

I read the solution posted 3 years ago by Youssef, but it is not working anymore.

When I try to replicate the solution, I get this error message:

We're sorry, a server error occurred. Please wait a bit and try again.

var docx = DriveApp.getFileById("###");

var newDoc = Drive.newFile();
var blob =docx.getBlob();
var file=Drive.Files.insert(newDoc,blob,{convert:true});

Execution stops at docx.getBlob().



Solution 1:[1]

This function uses some of the advanced Drive APIs and as such, require you to enable them from the script editor first, before running the script. To achieve this, go to:

Resources > Advanced Google Services... > Scroll all the way down to Drive API > toggle the off button to on

Advanced resources

Here's the final script -

function myFunction() {
  var docx = DriveApp.getFilesByName('Dummy.docx').next();
  var newDoc = Drive.newFile();
  var blob =docx.getBlob();
  var file=Drive.Files.insert(newDoc,blob,{convert:true});
  DocumentApp.openById(file.id).setName(docx.getName());  
}

Hope this helps!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sourabh Choraria