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 GmailApp.search() 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

10 comments:

Alberto Bartoli said...

That's great.

One tiny question: it seems there is no way to access a document description (http://googledocs.blogspot.com/2011/06/tips-tricks-using-description-field-in.html).

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 http://mashe.hawksey.info/2011/06/what-you-cant-do-with-gmail-and-document-services-now-available-in-apps-script/]

Is a fix for this in the pipeline?

Many thanks,
Martin
@mhawksey

Saurabh Gupta said...

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

http://code.google.com/p/google-apps-script-issues/issues/list

If you have any questions then please post on our forum here:
http://www.google.com/support/forum/p/apps-script/

Saurabh Gupta said...

Martin,
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.

http://code.google.com/googleapps/appsscript/guide_security.html

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...

Marc,

You can post your questions at our forum here
http://www.google.com/support/forum/p/apps-script/

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

Saurabh

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:
http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html#DownloadingFiles

The following Java guide uses getContent() that seems to be missing in the Script API
http://code.google.com/apis/documents/docs/3.0/developers_guide_java.html#DownloadingOtherFiles
String exportUrl = ((MediaContent) entry.getContent()).getUri() + "&exportFormat=" + fileExtension;