arrow_back

The Apps Script CLI - clasp

Join Sign in

The Apps Script CLI - clasp

30 minutes Free

GSP239

Google Cloud self-paced labs logo

Introduction

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. In this lab you will learn how to use clasp, the command line tool for Apps Script, to update script.google.com.

Features

  • Develop Locally. clasp lets you write code on your own computer and upload it to Apps Script when you're done. You can also download existing Apps Script projects and then edit them locally. Once the code is local, you can use your favorite development tools like git to work on Apps Script projects.
  • Manage Deployment Versions. Create, update, and view multiple deployments of your project.
  • Structure Code. clasp automatically converts your flat project on script.google.com into folders.

Example:

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

What you'll learn

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

  • How to create new Apps Script projects

  • How to clone, pull, and push existing projects

  • How to manage deployments of your scripts

Setup and requirements

Before you click the Start Lab button

Read these instructions. Labs are timed and you cannot pause them. The timer, which starts when you click Start Lab, shows how long Google Cloud resources will be made available to you.

This hands-on lab lets you do the lab activities yourself in a real cloud environment, not in a simulation or demo environment. It does so by giving you new, temporary credentials that you use to sign in and access Google Cloud for the duration of the lab.

To complete this lab, you need:

  • Access to a standard internet browser (Chrome browser recommended).
Note: Use an Incognito or private browser window to run this lab. This prevents any conflicts between your personal account and the Student account, which may cause extra charges incurred to your personal account.
  • Time to complete the lab---remember, once you start, you cannot pause a lab.
Note: If you already have your own personal Google Cloud account or project, do not use it for this lab to avoid extra charges to your account.

How to start your lab and sign in to the Google Cloud Console

  1. Click the Start Lab button. If you need to pay for the lab, a pop-up opens for you to select your payment method. On the left is the Lab Details panel with the following:

    • The Open Google Console button
    • Time remaining
    • The temporary credentials that you must use for this lab
    • Other information, if needed, to step through this lab
  2. Click Open Google Console. The lab spins up resources, and then opens another tab that shows the Sign in page.

    Tip: Arrange the tabs in separate windows, side-by-side.

    Note: If you see the Choose an account dialog, click Use Another Account.
  3. If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.

  4. Copy the Password from the Lab Details panel and paste it into the Welcome dialog. Click Next.

    Important: You must use the credentials from the left panel. Do not use your Google Cloud Skills Boost credentials. Note: Using your own Google Cloud account for this lab may incur extra charges.
  5. Click through the subsequent pages:

    • Accept the terms and conditions.
    • Do not add recovery options or two-factor authentication (because this is a temporary account).
    • Do not sign up for free trials.

After a few moments, the Cloud Console opens in this tab.

Note: You can view the menu with a list of Google Cloud Products and Services by clicking the Navigation menu at the top-left. Navigation menu icon

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

  2. Click Continue.

It takes a few moments to provision and connect to the environment. When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:

gcloud auth list

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:

gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Task 1. Download the CLI

Google Cloud comes with Node.js. The Apps Script CLI (clasp) requires Node.js >= v4.7.4.

  1. Verify your Node.js version:

node --version

You should see a v8.9.4 or more.

  1. Install the CLI globally (alias clasp):

npm i @google/clasp@2.2.0 -g

Task 2. Login

  1. Try out clasp! The only command you need to remember is clasp:

clasp

You should see detailed information about the clasp command.

  1. Before you start using the command line tool, run this command to log in:

clasp login --no-localhost

The output directs you to authorize clasp by visiting a url.

  1. Copy the URL and paste it into a new browser window.

  2. Click on your user name for this lab.

  3. In the Sign in with Google page click Allow.

  4. Copy the code provided in the Sign in page.

  5. Return to Cloud Shell and paste it in the command line at the prompt.

Task 3. Create a new standalone project

Start out by creating a stand alone Google Apps Script project.

  1. Run the following to make and then navigate to the clasp_lab directory:

mkdir clasp_lab && cd clasp_lab
  1. Create the project:

clasp create "clasp Lab"
  1. Since you're creating a stand alone project, use the arrow keys to select standalone then press Enter.

  2. You'll be notified that the Apps Script API has not been enabled. Click on the URL provided to enable it.

  3. Click VIEW DASHBOARD, then click Google Apps Script API. Move the button right to enable Apps Script API.

The Google Apps Script page

  1. Back in Cloud Shell, re-run the command to create the project:

clasp create "clasp Lab"

Example output:

Created new standalone script: https://script.google.com/d/1LL5L_Lj1FIvIZ5HMK65JE7ATQXJexVzqbYtLaZK0vDiTURemUjfXIO9x/edit Cloned 1 file. └─ appsscript.json

Click Check my progress to verify the objective. Create a standalone script

You just created an Apps Script Project in the folder clasp_lab!

Note: The clasp create command created a standalone script. This means the project is not connected to a Google Sheet, Doc, Form, or Presentation. How to create a bound script is covered in the next section.
  1. Remove the project you just created before moving to the next section:

rm .clasp.json

Task 4. Clone an existing project

Now create a container-bound script for a Google Slides Add-on.

  1. Go to slides.google.com and create a new presentation.

  2. Name the presentation "clasp Code Test".

Click Check my progress to verify the objective. Create a Google Slide

  1. In the header, under Tools, select Script Editor.

This opens your Apps Script project at script.google.com.

  1. Click on Use classic editor in the top right corner.

  2. To clone a project, you need the Script ID. Find this value by going to File > Project properties.

  3. In the window that pops up, in the Info tab, type clasp Code Test in the Name field.

  4. Look for the Script ID (this value is also found in the URL). Copy the value and then click Save.

  5. Back in Cloud Shell, clone the project, replacing with the value you copied:

clasp clone <scriptID>
  1. Open the Code Editor in Cloud Shell by clicking on the Open Editor icon.

You should see the cloned files Code.js and appscript.json.

Code.js viewed in Cloud Shell code editor:

The Cloud Shell code editor displaying the open Code.js file

You have downloaded the project in your current directory.

  1. Use your favorite editor to view the contents of Code.gs (an empty function).

Task 5. Pulling and pushing files - edit code on script.google.com

Next, learn how to pull and push files. You will edit on the cloud via script.google.com and pull locally to your computer using clasp.

  1. In the script editor, create a new file by choosing File > New > Script file.

  2. Enter the name utils/strings and click OK.

  3. In the newly created file, utils/strings.js, replace the code with the following:

var world = "世界";
  1. Click on the Code.gsfile, replace the existing code with the following:

function hello() { Logger.log("Hello, " + world); }
  1. Be sure to save all the files.

  2. Try running the function by clicking Run > Run function > hello.

  3. Go to View > Logs to see the greeting.

Task 6. Edit code locally

  1. Go back to the Cloud Shell command line where you last cloned the project. You may notice that your code is now out of sync with the online editor.

  2. To fix that, pull the code from your online project:

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

Pulled files in Cloud Shell code editor:

The Cloud Shell code editor displaying the first three lines of the open Code.js file

  1. In your favorite text editor:

  • Navigate to util/strings.js and replace the variable name world to mundus.

  • Update the Code.js by replacing "world" with "mundus".

  • Push your edited code to update the code on script.google.com:

clasp push

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

Note: clasp push replaces code that is on script.google.com and clasp pull replaces all files locally. For this reason, follow these guidelines:

  • Do not concurrently edit code locally and on script.google.com.
  • Use a version control system, like git.

Task 7. Versioning and deploying

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

  • Version: A "snapshot" of a script project. A version can be considered a read-only branch used for deployments.

  • Deployment: A published release of a script project (often as an add-on or web app). Requires a version number.

  1. In Cloud Shell, create a version of your script:

clasp version "First version"
  1. Using the logged version string you created in place of [version], 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.

You can now choose what version of your project to deploy.

Congratulations!

The Apps Script CLI is a simple tool to help you manage Apps Script projects. In this lab you performed the following:

  • Created a new Apps Script project
  • Pushed and pulled existing projects
  • Managed deployments of your scripts

Clasp is available on GitHub and welcomes new features and patches.

Finish your quest

This self-paced lab is part of the Workspace Integrations quest. A quest is a series of related labs that form a learning path. Completing this quest earns you a badge to recognize your achievement. You can make your badge or badges public and link to them in your online resume or social media account. Enroll in this quest or any quest that contains this lab and get immediate completion credit. See the Google Cloud Skills Boost catalog to see all available quests.

Take your next lab

Continue your quest with these labs:

Learn more

Google Cloud training and certification

...helps you make the most of Google Cloud technologies. Our classes include technical skills and best practices to help you get up to speed quickly and continue your learning journey. We offer fundamental to advanced level training, with on-demand, live, and virtual options to suit your busy schedule. Certifications help you validate and prove your skill and expertise in Google Cloud technologies.

Manual Last Updated September 23, 2022

Lab Last Tested August 05, 2021

Copyright 2022 Google LLC All rights reserved. Google and the Google logo are trademarks of Google LLC. All other company and product names may be trademarks of the respective companies with which they are associated.