Getting Started With Google App Script
Getting Started With Google App Script
Written by Mike James   
Monday, 06 August 2012
Article Index
Getting Started With Google App Script
Web Apps
A Gmail Web App

Client Side Events

There are client side events but they are, at the moment, fairly limited. You don't create the event handler by writing code but by calling a set of methods to build up an event handler object.

For example, to set a client handler on the button's click event you can write:

var handler =app.createClientHandler()
        .setText("Hello Client World");

The forEventSource returns the object that the event happened on i.e. the button and from this point we can use the methods that it supports to implement the event handler. You can see that this is more limited but it is faster and easier.

To get to grips with the UI Service you need to look at the range of objects it can create for you and some of the other ideas such as validators.

From here you can explore more about building a UI and there is also a GUI designer that will create a UI specification as a file for you to load into your app. It makes things very easy.

A GMail Web App

As a final example of how versatile App Script is let's create a simple Gmail web app. All this is going to do is display the number of threads in your inbox - but it is easy to see how to do more.

First we need to define the doGet function with a button and a textbox. The textbox is going to be used to display the number of threads so it needs an id:

function doGet() {
  var app = UiApp.createApplication();
  var mybutton=app.createButton(
             'Number of Gmail Threads');
  var handler = app.createServerHandler(
  var textbox=app.createTextBox();
  return app;


There isn't anything new in this part of the example. The event handler is a server side routine that will run on Google's server and retrieve the threads in your inbox:

function onClick()
 var app = UiApp.getActiveApplication();
 var threads = GmailApp.getInboxThreads();
 var text=app.getElementById('textbox1');
 return app;

This is also simple but the new element is the use of the GmailApp object and the getInboxThreads method to retrieve all of the threads in your inbox.

If you deploy this web app you may well find that even though it should have access to your Gmail account it simply complains that you are not authorized to run this script. The solution is to select doGet and try running the app as if it was an IDE script i.e. run it directly. Then you will see a set of requests to authorize access to your Gmail account. After this the web app will be authorized and you should be able to run it as many times as you like while you debug it.

Where Next?

There is a lot more to find out about App Script. You need to discover "triggers", which let you run apps at predefined times or in response to events that occur in containers. You can use it to build add-ins to existing containers such as the Spreadsheet, you can use it to access other Google services, you can even use it to build web apps that use other services that have nothing to do with Google. In fact it provides a fairly general set of facilities to build web apps in general. 


Related Articles

Gmail, Spreadsheets and Google App Script




blog comments powered by Disqus


To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin,  or sign up for our weekly newsletter.




Last Updated ( Saturday, 26 January 2013 )

RSS feed of all content
I Programmer - full contents
Copyright © 2017 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.