Google Apps Script file converting

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

Converting file in Google App Script into blob

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

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]

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.

Now we put down the whole code

 


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

 [2] Acknowledgment: StackOverflow Q&A