Getting Started with Google Apps Script

Google Apps Script developed by ​Romain Vialard which appeared in 2009.Google Apps Script is a JavaScript-based language which can be used for automatically formatted customized e-mail messages ,Google document file for automatic operation.Google Apps Script is a language for the task automation with the online application using the classic JavaScript.

This article explains you about what a Google Apps Script and how you can use it. After reading this article, you will get a glimpse of the boundless possibilities of Google Apps Script. I must say Google Apps Script is convenient and very easy to understand.

What is GAS – Google Apps Script?

The Google Apps Script is written in JavaScript language and hosted on Google’s servers. Google Apps Script is a kind of Google project which provides a development environment that is integrated into Google Drive with the ability to run and debug.

The purpose of Google Apps Script is to provide an automated system that will benefit both Google applications and other third-party programs. Google Apps Script is the script which can be used to create custom forms, automatic repetitive tasks and linked to a variety of Google products, such as to select one or more newsletter to send an email or a calendar event. You can easily customize an existing Google products including Gmail, Google Docs, Google Drive, Google Contacts, Calendar, Maps and Google Analytics using Google Apps Script.

You can use OAuth implement standard security measures in Google Apps Script.

Standalone Script Example function

Let’s understand the Example of Google Apps Script. After Log into Google Drive and create a new spreadsheet when Go to Script Editor, You will get the new page opens with a Google script IDE and this is where the fun begins. Here is the screenshot of the screen.

You have to copy paste following script in the “script editor”, which you will find at the menu bar of the spreadsheet.

1

2

3

4

5

6

7

8

9

10

11

functioncreateAndSendDocument(){

// Create a new Google Doc named 'CreativeDev GAS Demo'

vardoc=DocumentApp.create('CreativeDev GAS Demo1');

varurl=doc.getUrl();// Get the email address of the active user - that's you.

varemail=Session.getActiveUser().getEmail();// Get the name of the document to use as an email subject line.

varsubject=doc.getName();// Append a new string to the "url" variable to use as an email body.

varbody='Link your document created at CreativeDev : '+url;// Send yourself an email with a link to the document.

GmailApp.sendEmail(email,subject,body);

}

When this function will run, send the email to your id with attached spreadsheet link. You can automate your task with Google Apps Script.

Triggers in Google Apps Script

A trigger is a type of script resource that listens for a particular event and executes a function when that event fires.

Simple triggers – Simple triggers are a set of reserved functions built into Google Apps Script.Some Specially named built-in functions that are specific to spreadsheets.

onEdit – Changing the document (which can be made using IFTTT)

onOpen – The opening of the document

onInstall – Installable triggers

After you write the above function in the Google Apps Script,you need to create a trigger which will run the script. To create a trigger, Go to the top menu > “Resources”. In that click on “Current Project’s Triggers.You will get the popup to create the new trigger.

Here you can set the execution time of project:

The specific day and hour

Every week / day / month

Every n minutes / hours / days

After all the setup in the trigger section,It’s time to test the script so try to run the script.

Spreadsheet / App Bound Script Example

The bound​ script is a kind of script bound to a Google Sheets, Docs, or Forms file if it was created from that document rather than as a standalone script.

Bound scripts can call a few methods getActiveSpreadsheet(), getActiveDocument(), and getActiveForm() allow bound scripts to refer to their parent file without referring to the file’s ID.

getUi() function allows the user interface for their parent file to add custom menus, dialogs, and sidebars for bound scripts.

In Google Sheets, You can use the methods like getActiveSheet(), getActiveRange(), and getActiveCell() which determine the user’s current sheet, selected range of cells, or selected individual cell respectively .setActiveSheet(sheet) and setActiveRange(range) is the script used to change those selections.

In Google Docs, Methods getCursor() and getSelection() used to determine the position of the user’s cursor or selected text and setCursor(position) and setSelection(range) l functions are used to set the script change those locations.

Thank you for reading to the end and Special thanks to all the people who made and released these awesome resources for free.All you need to start experiments using the official documentation and reference.

Post navigation

About Author

Bhumi

Bhumi shah is currently working with leading web development company as a Software Analyst and also the founder of the Creativedev. She has immense interest in programming and web designing.She is passionate about technical blogging and almost versatile in terms of programming across various languages & frameworks such as PHP,MySQL WordPress, Twitter Bootstrap, AngularJS, HTML5, CSS3, jQuery and more.