Google AppScript: Basic/Beginner Introduction [TUTORIAL]

This tutorial provides a very basic introduction to Google App Script. I will also list out the works that can be done with AppScript. And, I will be showing very basic Javascript code that can be run on AppScript’s console. In this article, we will be dealing only with Javascript arrays and loops.

What is Google AppScript?

It’s a scripting language.
It’s based on Javascript.
It lets you work with Google Apps like Docs, Sheets, Forms, etc.
The AppScript code is written in browser in code editor provided by Google.
Overview: https://developers.google.com/apps-script/
Code Editor: https://script.google.com

What Google AppScript can do?

Add custom menus, dialogs, and sidebars to Google Docs, Sheets, and Forms
Write custom functions for Google Sheets
Publish Web apps (standalone or embedded in Google Sites)
Interact with Google services like Gmail, Google Calendar, Google Maps, Adsense, Analytics, etc.
And much more…
Basically, we should understand that Google AppScript is only intended to interact with Google Apps & Services.

Basic Example

  • Go to https://script.google.com
  • A page with code editor opens. By default, it contains function myFunction()
  • Below is the Javascript code that we can write inside that function. Google AppScript has a “Logger” class that helps to log the output result. Logger class is used to log output result of the javascript code below:

function myFunction() {  
  // printing log
  Logger.log('Hello World!');
  
  // working with variables
  var name = 'Mukesh';
  var country = 'Nepal';
  var age = 99;
  Logger.log('Name: ' + name + ', ' + 'Country: ' + country);
  
  // working with arrays (numeric array)
  var fruits = ['Apple', 'Mango', 'Orange'];  
  Logger.log(fruits[0]);
  Logger.log(fruits[2]);
  
  // working with arrays (associative array)
  var person = [];
  person['name'] = 'John';
  person['age'] = '50';
  person['address'] = 'Kathmandu';
  
  Logger.log(person[0]); // returns undefined
  Logger.log(person['name']);
  
  //Logger.clear(); // cleans log
  
  // working with loops
  var names = ['John', 'Roy', 'Alex', 'Sam'];
  var namesLength = names.length;
  var i;  
  for (i = 0; i < namesLength; i++) {
    Logger.log(names[i]); 
  }
  
}

To run the above code:

– Click the “Run” menu of AppScript editor
– After that, a dropdown will appear with the function names present in the script.
– Now, you have to click the function name that you want to run.

In next articles, we will see about accessing Google Docs, Google Sheets, Google Forms, etc. through Google AppScript.

Hope this helps. Thanks.