Google Apps Script (GAS) and Clasp Cheatsheet


What & Link Type
Apps Script Homepage Homepage
Gettings Started with CLASP (Command Line Apps Script Platform) Guide
My post - Using TypeScript with Google Apps Script Guide

Using clasp

Reference material:

Quick reference:

  • One time setup

    • Install

      • yarn global add @google/clasp
      • npm install @google/clasp
    • Grant credentials

      • clasp login
  • Project setup

    • cd {projectDir}
    • If...

      • ... starting from scratch

        • clasp create {scriptTitle}
      • ... project started already in web UI

        • clasp clone {scriptId}
  • While working

    • Download: clasp pull
    • Upload: clasp push
    • Open in browser: clasp open

Ignoring files

You can create and use a .claspignore file to ignore files from being uploaded, just like how .gitignore works.

By default, if no file is provided, it will only look for matching source code files (.js, .ts, .gs, .html), and blocks anything in .git or node_modules.

Using type definitions

You can find the types in @types/google-apps-script and @types/google-adwords-scripts.

If you are using straight TS, you can just install and then use as normal.

If you are using JS and want typed intellisense, you can install and then use the triple slash directive trick, or setup a tsconfig file.

If you don't want to have a package.json file, you could globally install types, and then pull in via symlink with npm link @types/google-apps-script. Just make sure you don't have clasp auto-upload!

Common Coding Challenges

How to respond to GET / POST

In order to respond to incoming requests, you simply declare a top level function that matches the magic function name the docs indicate:

HTTP Method Matching function
GET doGet(e)
POST doPost(e)

For those looking for what e, or event looks like and its properties, this table in the docs has you covered. Or, if you are using types, the explicit type of e is GoogleAppsScript.Events.DoGet or GoogleAppsScript.Events.DoPost, depending on the matching function.

How to make web requests (GET, POST, etc.)

Because GAS does not run in the browser, you don't have access to window.fetch() or even XMLHttpRequest; both of those are web APIs. You do however have a GAS provided service and set of methods - "Url Fetch".

NOTE: GAS's method fetch() is not the same as the web API fetch() that most people are used to. To start with, GAS fetch(), like pretty much everything in GAS, is synchronous, not async!

Markdown Source Last Updated:
Sun Dec 29 2019 18:03:20 GMT+0000 (Coordinated Universal Time)
Markdown Source Created:
Sat Nov 23 2019 05:26:46 GMT+0000 (Coordinated Universal Time)