A question in the Apps Script community prompted me to write this post about file conversion and the nature of files on Drive. If you use the Google Drive client on Windows or Mac, and take a look at what gets synced – it’s not the data in your document as you might expect, but rather its a collection of text links, saying where the data is.

That means that the concept of converting a file from one format to another is a bit different than it would be if you were dealing with a file that was full of data – as opposed to links to data.

This file meta data can be retrieved using the Advanced Drive service in the form of a File Resource Representation.

There is an exportLinks property, which is an object with properties keyed by MimeTypes, and with values of URLs which lead to a queries that can be used to retrieve the file contents in various formats.

exportLinks object Links for exporting Google Docs to specific formats.

For a spreadsheet, these exportLinks look like this.

So this tells me that, simply by retrieving the blob at each of those Urls, I can convert a sheet to ods, pdf, xlsx, csv, and zip. You can get more information on each of these mimetypes here. No coding or conversion needed – just fetch the blob at the Url.

The solution

So to get back to the community question, how to schedule something that converts a sheet to an excel file every now and again, the solution is simple. It needs a little sugar, since the op wants to use folder and file names rather than file IDS, but that’s simple enough.

It needs to use Advanced Drive to be able to get access to these Urls – which means you’ll need to authorize the Advanced Drive service in Apps Script and in the developer console.

I’ll just use the regular built in DriveApp to play around with folder and file names as it’s a little easier that way.

The code

Here’s the complete solution.

Note…..

If you get an error about Drive being undefined, it means you still haven’t done this: ‘you’ll need to authorize the Advanced Drive service in Apps Script and in the developer console’, so go and do that now. It’s in the resource menu.

For more like this see Google Apps Scripts Snippets
Why not join our forum, follow the blog or follow me on Twitter to ensure you get updates when they are available.