google-apps-script Getting started with google-apps-script

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Remarks

The official overview for Google Apps Script is published at http://www.google.com/script/start, from there

Google Apps Script is a JavaScript cloud scripting language that provides easy ways to automate tasks across Google products and third party services and build web applications.

From https://developers.google.com/apps-script/guides/services/#basic_javascript_features

Apps Script is based on JavaScript 1.6, plus a few features from 1.7 and 1.8. Many basic JavaScript features are thus available in addition to the built-in and advanced Google services: you can use common objects like Array, Date, RegExp, and so forth, as well as the Math and Object global objects. However, because Apps Script code runs on Google's servers (not client-side, except for HTML-service pages), browser-based features like DOM manipulation or the Window API are not available.

A deeper look at Google Apps Script

Google Apps Script is a JavaScript based platform-as-a-service primarily used to automate and extend Google Apps. Apps Script runs exclusively on Google's infrastructure requiring no server provisioning or configuration. An online IDE serves as the interface to the entire platform connecting all the services that are available to Apps Script. User authentication is baked into the platform via OAuth2 and requires no code or setup by the script author.

Apps Script runs server-side, but can have user interfaces built with Html, CSS, JavaScript, or any other browser supported technologies. Unlike Nodejs, which is event driven, App Scripts runs in a threaded model. All calls to a script generate a unique instance of that script which runs in isolation of all other instances. When an instance of a script finishes execution it is destroyed.

Functions in Apps Script are blocking so callback and async programming patterns are not needed. Locking is used to prevent critical sections of code, such as file IO, from being executed simultaneously by different instances.

In practice writing Apps Scripts are simple. Below is a simple script that creates a new spreadsheet from a template spreadsheet.

// Create a new spreadsheet from a template
function createSpreadsheet(){
   var templateFileId = '1Azcz9GwCeHjGl9TXf4aUh6g20Eqmgd1UMSdNVjzIZPk';
   var sheetName = 'Account Log for:' + new Date();
   SpreadsheetApp.openById(templateFileId).copy(sheetName);   
} 
 

Hello World

We are going to say Hello as a message box.

function helloWorld() 
{
  Browser.msgBox("Hello World");
}
 

To execute the script, either click ▶ or select the menu item Run -> helloWorld

Installation or Setup

Google Apps Script does not require setup or installation. The only requirement is a Google Account. A Gmail account works as well as a Google Apps for Work/Education/Government account. You can create a new Google account by going to accounts.google.com

Start your first script by going to script.google.com. You can also access Google Apps Script under the tools -> Script editor... of many Google Apps i.e Docs, Sheets, Forms etc. Google Apps Script can also be added directly to your Google Drive with the Connect more apps.. feature.

Official documentation can be found at developers.google.com/apps-script/.

For app-scripts to run they must contain a code.gs file. The code.gs file must contain a function named doGet (standalone scripts) or an onOpen function (addon scripts). The quick starts in the documentation contain examples.

If an api is turned on in the app-script it must also be turned on in the developers-console. However, the developers console contains api's that can be turned on but do not appear in the app-script interface. For example, Marketplace SDK needs to be turned on in the developers console before the app can be published to the Google play store or to a G suite domain wide deployment.

For Google apps for education/work/government there are settings in the domain admin console that can be adjusted to allow or disallow app-scripts to run.

Running/Debugging your script

Try to run your code from the tool bar as shown below :

enter image description here

In your code, if you have more than one function then, before running it you should mention the function you want to run with. For example :

enter image description here

Alternatively, you can press ctrl + r from your keyboard to run the code. It will save the code first, if not saved, and then run it. But, for that to work, you must have selected the function, as seen in the image above.

Also, if your script is called by some external activities, still you will be able to see logs by clicking view->logs if you are logging anything after the code is executed.

Types of Scripts

Google App scripts are of three types.

  • Standalone
  • Bound to Google Apps
  • Web Apps

Standalone script

Standalone scripts are not bound to any Google apps i.e Docs, Sheets or Forms etc. Standalone script can either be created by visiting script.google.com or by connecting Google app script with Google drive. Standalone script can be used to program Google apps independently, can be used as a Web App or can be set up to run automatically from an installable trigger. See the documentation for standalone script.

Bound to Google Apps

Script bound to Google Apps also known as container-bound script; unlike standalone scripts, are bound to Google apps i.e Google Docs or Google Sheets etc. Container bound script can be created by selecting tools> Script editor from Google App. Some features like dialogs, prompts, menus and sidebar are only provided by container-bound scripts. Furthermore, container-bound script is used to create Google Add-ons. See the documentation for container-bound scripts.

Web Apps

Google App Script can be used as web app as they can be accessed by browser. Web App can provide user interface on the browser and can make use of google apps i.e docs, sheets etc. Both standalone scripts and scripts bound to Google Apps can be turned into web apps. For any script to work as a web app, script has to meet two requirements:

  • include a doGet() or doPost() function.
  • The function returns an HTML service HtmlOutput object or a Content service TextOutput object.

Inshort, doGet() and doPost() functions works like http get and post request handlers respectively.

For more details on Web Apps, see the official documentation.



Got any google-apps-script Question?