Categories
Development

How To Automaticlly Convert files in Google App Script


The other day I was challenged to do some cloud converting following the web scraping project with Google App Script(GAS)[1]. Namely to get a google doc file and to convert it into MS Word format. 

I had a few struggles in accomplishing it – and behold this post has appeared.

Advanced service handle

Ok, after my GAS has harvested data into a Google docs file, I needed to convert it into MS Word format. After some good-ol-fashioned googling I found that Google App Script does not allow this functionality. Seems a bit odd, but OK. Yet more Googling showed me that there is an workaround using the Advanced Drive Service. This very advanced Google service. 

Before using that service follow these instructions on how to enable this advanced Google service in Google App Script.

converting file in Google App Script
Converting file in Google App Script into blob

OK, so we’ve got our needed info into a current active file with documentId

var documentId = DocumentApp.getActiveDocument().getId();        

function getBlob(documentId) {
 var file = Drive.Files.get(documentId);
 var url = file.exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
 var oauthToken = ScriptApp.getOAuthToken();
 var response = UrlFetchApp.fetch(url, {
   headers: {
     'Authorization': 'Bearer ' + oauthToken
   }
 });
 return response.getBlob();
}

Notice, for such a converting file in Google App Scrip we set the mime-type for MS Word docx format. The MS Office mime types I got from here.

Saving file as docx in Drive[2]
function saveFile(blob) {
   var file = {
    title: 'Converted_into_MS_Word.docx',
    mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  };
  file = Drive.Files.insert(file, blob);
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
  return file;
}
Sending file as an attachment (optional)

The good thing about gmail is that if you send the link to your cloud document, Gmail can handle it as an attachment, so we only have to add download and viewing links into the mail body.

function sendMailwithFile(file)
{       
  var email   =  Session.getActiveUser().getEmail(); // active user email
  var subject = 'Created file: ' + file.title; 
  var body = ' - Link to docx file for download: ' + file.webContentLink +  '\n' + ' - Link to view file online: ' + file.alternateLink;
  GmailApp.sendEmail(email, subject, body);
}
Now we put down the whole code
var documentId = DocumentApp.getActiveDocument().getId();     
var blob = getBlob(documentId);
//var blob = UrlFetchApp.fetch().getBlob();
var file = saveFile(blob);
sendMailwithFile(file);

function getBlob(documentId) {
 var file = Drive.Files.get(documentId);
 var url = file.exportLinks['application/vnd.openxmlformats-officedocument.wordprocessingml.document'];
 var oauthToken = ScriptApp.getOAuthToken();
 var response = UrlFetchApp.fetch(url, {
   headers: {
     'Authorization': 'Bearer ' + oauthToken
   }
 });
 return response.getBlob();
}

function saveFile(blob) {
  var file = {
    title: 'Converted_into_MS_Word.docx',
    mimeType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
  };
  file = Drive.Files.insert(file, blob);
  Logger.log('ID: %s, File size (bytes): %s', file.id, file.fileSize);
  return file;
}

function sendMailwithFile(file)
{         
  var email   =  Session.getActiveUser().getEmail(); // active user email
  var subject = 'Created file: ' + file.title; 
  var body = ' - Link to docx file for download: ' + file.webContentLink +  '\n' + ' - Link to view file online: ' + file.alternateLink;
  GmailApp.sendEmail(email, subject, body);
}

[1] To get the basics of Google App Script, just go here.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.