The Apps Script CLI, or clasp, is a tool that lets you to create, edit, and deploy Apps Script projects locally. It allows you to create and publish web apps and add-ons for products like Sheets, Docs, Forms, and Slides from the command line. There are two ways you can develop Apps Script, using script.google.com or locally on your computer. We'll be learning the latter, how to use clasp, the command line tool for Apps Script.

Features

# On script.google.com:
├── tests/slides.gs
└── tests/sheets.gs

# Locally:
├── tests/
│   ├─ slides.gs
│   └─ sheets.gs

What you'll learn

This codelab will show you how to do 3 key activities with clasp:

Downloading the CLI

The Apps Script CLI (clasp) requires Node.js >= v4.7.4 to be installed. Install Node.js here.

Once you have Node, install the CLI globally (alias clasp):

npm i @google/clasp -g

Login

Let's try out clasp! The only command you should remember is clasp.

clasp

Before we start using the command line tool, we must log in. Run this command:

clasp login

At this point, you are prompted to log in to Google. Any projects you create with the CLI are associated with this Google account.

Create a standalone project

Start out by creating a standalone Google Apps Script project with the following command:

mkdir clasp_codelab;
cd clasp_codelab;
clasp create "Clasp Codelab";

You just created an Apps Script Project in the folder "clasp_codelab"!

(Optional alternative) Clone an existing project

Let's try creating a container-bound script for a Google Slides Add-on.

To do this, go to slides.google.com and create a new presentation. Change the presentation name to "clasp Codelab Test". In the header, under Tools, press Script Editor....

This will open your Apps Script project at script.google.com. To clone a project, we need to find the Script ID. You can find this value by going to File > Project properties > Info > Script ID (this value is also found in the URL). Copy the value and paste it in the following command:

clasp clone <scriptID>

The output should look like this...

Now you have downloaded the project in your current directory. Use your favorite editor to view the contents of Code.gs (an empty function).

Edit code on script.google.com

Now that you're able to clone a project, let's learn how to pull and push files. We'll walk you through the steps to edit on the cloud via script.google.com and pull to locally to your computer. Let's open the script in the cloud:

clasp open

Now that we're on the online editor, we'll edit some code online that we'll later fetch using clasp.

In the editor, Create a new file by choosing File > New > Script file. Enter the name utils/strings. In the newly created file, utils/strings.gs, replace the code with the following:

var world = "世界";

In Code.gs, replace the existing code with the following:

function hello() {
  Logger.log("Hello, " + world);
}

Be sure to save all files. Try running the function by pressing Run > Run function > hello.

Go to View > Logs to see the greeting.

Edit code locally

Let's go back to the command line where we last cloned the project. You may notice that our code is now out of sync with the online editor. To fix that, let's pull the code from our online project.

clasp pull

Now go back to the code. You should notice there's a folder for our utils. The clasp CLI automatically converts the slash character / to folders on the local filesystem.

In your favorite text editor, navigate to util/strings.gs and replace the variable name world to mondo. Also, update the Code.gs by replacing world to mondo. To update the updated code on script.google.com, push your edited code.

clasp push

And that's it! Your code is now updated on script.google.com.

clasp allows you to manage versions and deployments. First, some vocabulary:

Let's create a version of our script:

clasp version "First version"

Using the logged version string we created in place of [version], we can deploy the script:

clasp deploy 1 "First deployment"

The clasp deploy command looks at your manifest and creates a new versioned deployment. Your code is now deployed as an executable. Learn more about this in the deployments guide.

We hope the Apps Script CLI is a simple tool to help you manage Apps Script projects.

What we've covered

Clasp is available on GitHub and is welcome to new features and patches.

We can't wait to see what you build!