Google Apps Script – Script management with App Scripts CLI (Clasp)

Google Apps Script is a cloud-based platform for developing add-ons for G Suite applications, automating workflows, integrating external APIs and more. Several tools exist which make it easier to develop with Apps Script.

One of these tools is Clasp, the Apps Script command line interface (CLI). We'll cover what this tool can do, how it is installed, and how it is used below.

Features

  • Develop locally: You can download projects and develop them locally with your favorite development tools (IDEs, Git, Linter).
  • Managing versions and deployments: You can manage the versions of the developed scripts and revert to earlier versions.
  • Structuring code: script.google.com projects are automatically mapped to appropriate project directories on the local system.

To see more details, refer to the documentation or the CodeLabs tutorial.

Installation

Once the Node Package Manager has been installed, you can install Clasp quickly and easily with the following command.

$ npm i @google/clasp -g
 > @google/clasp@1.0.7 postinstall /dir/.nvm/versions/node/v9.2.1/lib/node_modules/@google/clasp
 > echo '-> Thanks for installing clasp!
-> Thanks for installing clasp!
+ @google/clasp@1.0.7
 added 199 packages in 4.708s

Now Clasp should be available in the command line as an executable application.

$ clasp -h
Usage: clasp <command> [options]

clasp - The Apps Script CLI

Options:

-h, --help output usage information

Commands:

login Log in to script.google.com
 logout Log out
 create [scriptTitle] Create a script
 clone <scriptId> Clone a project
 pull Fetch a remote project
 push Update the remote project
 open Open a script
 deployments List deployment ids of a script
 deploy [version] [description] Deploy a project
 undeploy <deploymentId> Undeploy a deployment of a project
 redeploy <deploymentId> <version> <description> Update a deployment
 versions List versions of a script
 version [description] Creates an immutable version of the script

Activate and authenticate the API

First, allow the Google Apps Script API to be used in your Google Account.

Activate Google App Scripts API (German UI)

Activate Google App Scripts API (German UI)

Now you can authenticate yourself in your local development environment with Clasp and OAuth to continue working with Google products.

$ clasp login
 -> Authorize clasp by visiting this url:
 https://accounts.google.com/o/oauth2/auth?......

Finally, confirm that you have the required permissions in the browser window and you will see the following message.

Saved the credentials to ~/.clasprc.json. You may close the page.

With that, you can start using the tool.

Work with projects

In order to make an existing Apps Script project available locally, you can create a clasp clone by specifying your project ID. This project ID is visible in the Apps Script administration in your browser.

Project properties for a Google Apps Script project (German UI)

Project properties for a Google Apps Script project (German UI)

$ clasp clone PROJEKTID
 Cloned 2 files.
- Code.gs
- appsscript.json

You can now work with the files. To synchronize them again with the Google Apps Script project, you can use clasp push.

$ clasp push
- Code.gs
- appsscript.json
 Pushed 2 files.

You can see the changes that you have made to files in the project in the Apps Script interface.

Google Apps Script - change history of project files (German UI)

Google Apps Script - change history of project files (German UI)

Versioning

To keep track of changes to the scripts, you can version project stages. In the following example we will use clasp version to assign an alpha1 named version to a project.

$ clasp version alpha1
 Created version 1.

You can see all of the versions that exist with clasp versions.

$ clasp versions
 ~ 1 Version ~
 1 - alpha1

You can publish a version with clasp deploy.

$ clasp deploy 1 "Erste Alpha Version veroeffentlicht"
 - ABC123ABC123ABC123 @1.

You can see the different deployments that have already been created with clasp deployments.

$ clasp deployments
 2 Deployments.
 - ID @HEAD
 - ID-ABC123ABC123ABC123 @1 - Erste Alpha-Version veroeffentlicht

In the Google Apps Script interface, you can also see and work with the various versions.

Google Apps Scripts - working with versions (German UI)

Google Apps Scripts - working with versions (German UI)

As you can see: Clasp is fast and easy to install and use, and the tool seamlessly integrates with Google Apps Script and fits into your workflow with useful features. It is especially practical because you can work in your preferred development environment instead of the Apps Script Editor.

Lesen Sie diese Seite auf Deutsch

Further information

Apps for Google G Suite and the Google App Maker
Google G Suite: features, use cases and implementation projects