Skip to main content

Google Apps Script (GAS)

Google Apps Script, commonly abbreviated GAS, is a JavaScript-based scripting system for Google products such as Gmail, Google Drive, Google Sheets, and more. GAS is the code that powers any addons you might use in any of these apps. It can allow us to make our own addons or simple utility/automation functions that integrate with various Google services. Because GAS is based on Google Cloud, we do not need to host our own backend to run GAS code or host GAS apps - Google handles it for us, all for free.

Some example use cases on our team:

  • Automated data storage
  • Cross-platform webapps integrating with Google services
  • Automatic email template rendering and sending
  • Automatic Google Calendar event creation
  • HTTP APIs based on the contents of a Google Drive file

For more information about the capabilities of GAS, read this page.

A GAS project can either be standalone or container-bound.

  • A standalone project is not linked to any particular Google Drive file and should be created from script.google.com. You would typically use a standalone project if it's designed to be used with multiple files that aren't directly related to each other. For example, an addon might be intended to be used with any Google Sheet rather than one in particular.
  • A container-bound project is permanently attatched to a specific Google Drive file. If you make a copy of that file, you will also make a copy of the GAS project. If you share the file, you will also share the GAS project. Container-bound projects are often used when writing automation functions that are intended for a specific file. For example, you may want to write a function that sends an email to all email addresses within a selected column in a specific Google Sheet.

Google provides an in-browser IDE for GAS that also includes execution and error logs stored on the cloud along with deployment management and various other features.

Getting Started

The homepage for GAS documentation can be found here. The easiest way to get started is with the official guide.

Clasp

Clasp is a CLI that enables us to use any IDE to write GAS code as opposed to being restricted to the GAS in-browser IDE. In some ways, it is conceptually similar to Git; however, Clasp is not a VCS. It allows us to create, pull, push, deploy, and debug GAS projects - but it will not track version history or handle things like branch management. Clasp is best used alongside Git.

Getting Started

The official Clasp guide can be found here. You will need to have Node and a package manager like npm installed on your system to use Clasp. You will then need to install the package @google/clasp globally.

When using Clasp for the first time, you may be prompted to open a link to sign in or toggle a setting to enable access. You will need to run clasp login to authenticate your Google Account (you can sign out with clasp logout).

If you create a Clasp project with clasp create, a GAS project and a corresponding Google Drive file (if the project is container-bound) will be automatically generated by Google. Links will be provided in the terminal that you can open. You can also use clasp open to open the project in your browser at any time.

You can pull updates from Google's end with clasp pull and force push your changes to Google with clasp push. Keep in mind that this will not affect your Git repository. You must still pull and push changes with Git to sync with the Git repo.

Your code in a Clasp project can be written either in JavaScript (.js) or TypeScript (.ts). Clasp automatically compiles TypeScript and pushes your JavaScript code to Google where it is saved with the extension .gs (but it's really still just JavaScript).

Typically, you will also want to create a project in the same directory using your preferred JS package manager (npm, pnpm, Yarn, etc). This will allow you to install dependencies from NPM for you to use in your project.

GAS Type Hinting

You may notice that the convenience of the in-browser IDE type and argument hinting and autocompletion is missing in your own IDE. This is because your IDE does not have information about the classes and functions referenced in GAS. To solve this, run:

npm install --save @types/google-apps-script

Or the equivalent for your package manager. View info about this package here.

For GAS webapps, you may also want to run install @dgcode/html-service (info here). This contains type hinting for the google.script object which is described in further detail in the Webapps section.

Webapps

GAS can be used to host serverless webapps that interact with Google services. For example, you could create a frontend for an app that stores and modifies data in a Google Sheet.

Svelte + Clasp + GAS (Advanced)

WIP