Thursday, June 13, 2013

How to get started with Google App Scripts?

Uses of Macros and Scripting are very common when you deal with Ms-Office products such as Ms-Excel, Ms-Word, Ms-Access etc. With macros you can automate a lot of task that you do repeatedly with excel, word etc. What Google App Scripts provide you is ability to create macros and script in cloud using JavaScript cloud scripting language and leverage the use of Google products and services such as Gmail, Google Drive, Spreadsheet, Google Analytics, Calendar etc.

You can create scripts which can access your gmail, goolge drive, spreadsheet, calendar etc and provide you information and statistics that you can use in your daily life. The Google App Script are developed using Google App Script editor which is a browser based tool. The scripts that you developed are stored in cloud to your Google Drive. The good thing is unlike Google Data API, you do not need to download and install libraries to your desktop.

The pre-requisite to create Google App Script are following:

1. You need to have a Google Account. If you do not have one you can create a new Google account.

2. You need a web browser which is compatible to develop Google App Script. For a list of supported browser you can visit this link.

3. You need to enable JavaScript and Cookies in your web browser.

4. A basic understanding of JavaScript scripting language.

5. Internet connectivity on your desktop.

To develop your Google App Script you need to go to http://script.google.com, This is the place where you will develop and test your Google App Script.

You will get the following screen and you can select what kind of script you want to create. You can choose a Blank Project if you want or select Gmail, Drive, Calendar, Spreadsheet project. It also let you open an existing Google App Script project.

Let us select Gmail to create a simple script which will tell us how many new messages and spam messages we have in our Gmail account.

The default script page that you will get is following:

Let us delete the entire code and add following scripting code in the script editor.

/**
* Author  : Vikash Kumar Singh
* Purpose : This script send new and spam message count to any email address
*
*/

function processInbox() {  
  var NewMessageCount = GmailApp.getInboxUnreadCount();
  var SpamMessageCount=GmailApp.getSpamUnreadCount()
  var sentEmailTo="singhvikash.blogspot.com@gmail.com"
  var myMessage="Total New Message in your inbox is:" + NewMessageCount  + " || "
  myMessage  = myMessage + "Total Spam Message in your inbox is:" + SpamMessageCount
  GmailApp.sendEmail(sentEmailTo, "Today Gmail Status" , myMessage) 
 
};

Click on Save button and it will ask to provide name to your project.

Click on OK. The script file will be saved.

Next, click on the Run button to run the project.

This will ask for Authorization for the first time.

Click on Authorize. It will open another window to ask you to Grant access. Click on Grant Access.

Authorization and Grant Access is one time activity for the Script project to run. Next you will get to see the Authorization Status. You can close the window.

Next click on Run and if you do not get any error message, you can check you inbox and you will receive an email with your new and spam message count.

Popular Posts

Real Time Web Analytics