Gmail and Document Services now available in Apps Script

Wednesday, June 15, 2011 | 2:13 PM

Labels: , ,

If you’ve used Google Apps Script before, you’ll know that we have deep integration with many Google Apps & APIs. You can access your Calendar and Contacts, create Google Sites, send email, and lots more. But two integrations were noticeably missing: the Google Docs word processor, and Gmail. A few weeks ago, at Google IO, we announced new Apps Script APIs for these two great products.

GmailApp - read, label and send emails

The new GmailApp should be instantly familiar if you’re used to using Gmail. You have access to labels, threads, and messages, and you can do all the things you expect: change labels, add and remove stars, mark things as important or trash or spam. You can also use the method to do any search you can do in Gmail itself. This is the first full fidelity API to Gmail, and we’re excited to see what you all will do with it.

To make sure your Gmail account remains private and secure, we are extra cautious about any script that uses GmailApp. Any time you change the code for a script that accesses Gmail, we will require you to reauthorize the script to run. Keep that in mind when editing a script that runs on a trigger - if you don’t reauthorize it, it will fail the next time the trigger tries to run it.

DocumentApp - create and edit Docs

The new DocumentApp lets you create new documents or open existing ones by id. The id of a document is always in the URL you see when visiting it, and document ids are consistent between DocumentApp and the existing DocsList service, so you can use DocsList.find() to search for a document, and then get its id from File.getId().

Once you have a document, you can access all of its individual elements. You can do search and replace (great for mail merge!), change styles or text, add or remove tables and lists, and much more. And when you are done, you can call Document.saveAndClose(), which makes sure all of your changes to the document get saved. If you don’t call it, we’ll do it for you at the end of the script, but it can be useful to call it yourself if you’d like to do something with the document after making your edits.

GmailApp and DocumentApp are available to all Apps Script users right now. Now the only question is, what are you going to build with them?

Want to weigh in on this topic? Discuss on Buzz


Alberto Bartoli said...

That's great.

One tiny question: it seems there is no way to access a document description (

Am I wrong ? Thanks !

mhawksey said...

It's worth noting want you can't currently do, that is publish as a service to access the logged in users Gmail or DocList.

[As I discovered and documented here]

Is a fix for this in the pipeline?

Many thanks,

Saurabh Gupta said...

You currently cannot get description of a document but please do open a feature request here.

If you have any questions then please post on our forum here:

Saurabh Gupta said...

Scripts that are published run under the identity of the owner. This is for security and privacy reasons. Please see our documentation for more on security in Apps Script.

Anonymous said...

Could someone point to some documentation detailing how to add custom menu items / actions to a document?

I want to add an action to the File menu in an open document. Also, I want to add an action to the Actions dropdown menu in the Document List.

Schultzter said...

I'm looking through the API reference and I haven't found a way to add an image to document. I see can get the images; but how can I add one!?

Marc Sabatella said...

Sorry to be such a noob, but I cannot figure out how to get started with this interface. I don't see any way to run a script from within a text document. Do I have to create a spreadsheet to attach my script to, and then load and process a document from there? No way to simply run a script on the active document from within the text document itself?

Saurabh Gupta said...


You can post your questions at our forum here

Currently, the script editor can be accessed from a Spreadsheet or a Sites page.


Marc Sabatella said...

Got it, thanks! I've started participating in the forum and issue tracker. Definitely hoping to someday see access to scripts from documents (in particular, to be able to get at the current selection!) and the ability to insert images. And yes, I've starred the relevant issues :-)

Unknown said...

We store PNG image files in a Google folder and would like to display them in a Google Spreadsheet using "=image()" formula. The image function take the image http/s URL. How do you get this type of "download" URL from inside an Apps Script? DocList? What method?

The following guide talks about without providing the request format details:

The following Java guide uses getContent() that seems to be missing in the Script API
String exportUrl = ((MediaContent) entry.getContent()).getUri() + "&exportFormat=" + fileExtension;