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

App Script is a language and framework that deserves to be better known. Using it you can write programs that work with almost any Google service from Gmail, through Calendar and on to Spreadsheeting. You can even use it to work with non-Google APIs. It is the glue that binds all of these facilities together and the good news is that it is very easy to use. So let's get started...

First you need to have a Google account. As App Script is mostly about working with services that you get via a Google account this shouldn't a shock or a hardship. So from here on in it is assumed that you have say a Gmail account.

Getting started is a matter of visiting a few websites - development is, of course, all done online.

The main site you will need to use and the one that has the IDE that you can use to work with your projects is:

https://script.google.com

You can find the current status of all of the services that Google offers you need to visit

https://script.google.com/dashboard

and finally for documentation you also need to visit:

https://developers.google.com/apps-script/

It is also worth knowing where your app scripts are stored. If you visit your Google Drive page you will see any apps you have created. It is important that you have enough storage available in Drive for your app file - but this isn't a huge amount, If you click on a project file listed in Drive it will open in the script editor.

 

What Is App Script?

The first and most important thing to realize about App Script is that it is just JavaScript. So if you know JavaScript you know App Script. For the remainder of this article it will be assumed that you know the basics of JavaScript.

So if App Script is just JavaScript what is that new to learn?

Perhaps the biggest difference is that this is a server side JavaScript. It runs on Google's servers not in the client. Mostly this doesn't make a great deal of difference and it is an advantage from the point of view of communicating with Google's services which are on servers which are locally connected to the one running the script. The biggest problem is causes is when you want to add a user interface. As this is on the client side you can see that you need something new to allow the server side script to create a UI.

Two main things - the object library and the idea of a container.

App Script is JavaScript augmented by a very lager collection of objects that have methods and properties. This is the area that you have to explore and which parts you decide to explore depends on what you are trying to do. One big problem is that it is very difficult to discover if a particular feature is available in a form that you need to complete a project. There is no other way than to first read the documentation and then put together a prototype a quickly as possible. Alternatively you can look for an app that does the same sort of task - but if there is one why are you writing your own?

The second important idea is that of a container. App Scripts run in a range of possible environments each one is a container. You can think of the container as being the web page it is designed to run in. For example, if you start a new project you are offered a range of different containers:

 

 

containers

 

The two most general are the Blank Project which can be run from the IDE with essentially the IDE web page as its container. The Script as Web App option provides the script with its own web page to run in. You can use this to create a user interface for the script. The other options host the script in the web pages that you use to access the listed Google service. For example, if you select GMail as the container then the generated app contains code that makes working with GMail easy.

Let's see how it all works.

Hello World

Open https://script.google.com, sign in to Google if you have to and select a blank project. You will be transferred to the code editor with a few lines to get you started:

function myFunction() { 
}

You can change the name of the function, erase it and start with a really bank project. 

In this case change the function to Hello wand type in the following:

function Hello1() {
  Logger.log("Hello App Script World");
}

Notice that as you type Logger. the IDE prompts you with possible completions by listing all of the methods and properties of the object.

helloworld1

 

If you now click the Run menu option then you will discover that you have to save the file first. Save the project under the title Hello1.

If you now select the Run menu option and the Hello1 function as the entry point for the program you will see - nothing. The reason is that IDE apps don't have a user interface and this is the reason we are writing to the Logger object. To see the log window you have to use the View, Logs menu command which displays the desired output message.

logger



Last Updated ( Saturday, 26 January 2013 )
 
 

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