4

I know how to convert a Microsoft Word document to Google Documents format manually (right click, open with Google Drive). But is there any way to do this using Google Apps Script? I have looked through the help and can't find any methods for files that lets you change the file type, or save in a different format. This seems such an obvious service that I suspect I am missing something.

Does anyone know a way to do this?

0

Currently, that's not possible, even though the request is pending for some time now. If you follow the below mentioned issue (star it; upper left corner), then you will be notified about any change.

Issue Tracker

Note

The higher the number of votes (star), the higher the chance of it being implemented by the Google Apps Developers team.

2

I had a similar issues and found a work around. I can't remember where I got it so apologies for not crediting the source. With this method I created a separate drive folder where the Word document is uploaded to and then a new Google Doc is created alongside it and the text pasted into that. I also had to create a new project in the Google Developers Console in order to get a developer key.

The below code is for a .docx file. For older .doc files change the content type to "application/msword"

//Function to convert a Microsoft Word file to a Google Document.
function ConvertWordDoc(file) {
  authorize();
  var key = "YOUR_DEVELOPER_KEY"; // <-- developer key

  var metadata = { title: file.getName(),
                   parents: [{
                   "kind": "drive#fileLink",
                   "id": DocsList.getFolder("Folder Name").getId()
                   }]
                 }
  var params = {method:"post",
                oAuthServiceName: "drive",
                oAuthUseToken: "always",
                contentType: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                contentLength: file.getAs("application/vnd.openxmlformats-officedocument.wordprocessingml.document").getBytes().length,
                payload: file.getAs("application/vnd.openxmlformats-officedocument.wordprocessingml.document").getBytes()
  };
  var folderName = DocsList.getFolder("Folder Name");
  var uploadRequest = UrlFetchApp.fetch("https://www.googleapis.com/upload/drive/v2/files/?uploadType=media&convert=true&key="+key, params);
  var uploadResponse = Utilities.jsonParse(uploadRequest.getContentText());
  var params = {method:"put",
                oAuthServiceName: "drive",
                oAuthUseToken: "always",
                contentType: "application/json",
                payload: Utilities.jsonStringify(metadata)
               };
  var metaRequest = UrlFetchApp.fetch("https://www.googleapis.com/drive/v2/files/"+uploadResponse.id+"?key="+key, params);
  return DocsList.getFileById(uploadResponse.id);
}

function authorize() {
  var oauthConfig = UrlFetchApp.addOAuthService("drive");
  var scope = "https://www.googleapis.com/auth/drive";
  oauthConfig.setConsumerKey("anonymous");
  oauthConfig.setConsumerSecret("anonymous");
  oauthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope="+scope);
  oauthConfig.setAuthorizationUrl("https://accounts.google.com/OAuthAuthorizeToken");    
  oauthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");  
}
  • Thank you very much for this, which I have only just noticed, since gmail concealed the notification in a heap of worthless promotional material. – Andrew Brown Mar 31 '14 at 14:23
  • A deleted non-answer says: "docslist is now deprecated, could you update this script to one using driveapp?" – ale Jan 17 '16 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.