Create a standalone script
Create a Google Slide
The Apps Script CLI - clasp
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.
clasplets 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.
claspautomatically converts your flat project on script.google.com into folders.
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).
- Time to complete the lab---remember, once you start, you cannot pause a lab.
How to start your lab and sign in to the Google Cloud Console
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
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.
If necessary, copy the Username from the Lab Details panel and paste it into the Sign in dialog. Click Next.
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.
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.
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.
Click Activate Cloud Shell at the top of the Google Cloud console.
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:
gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.
(Optional) You can list the active account name with this command:
(Optional) You can list the project ID with this command:
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.
Verify your Node.js version:
You should see a v8.9.4 or more.
Install the CLI globally (alias clasp):
Task 2. Login
Try out clasp! The only command you need to remember is
You should see detailed information about the
Before you start using the command line tool, run this command to log in:
The output directs you to authorize clasp by visiting a url.
Copy the URL and paste it into a new browser window.
Click on your user name for this lab.
In the Sign in with Google page click Allow.
Copy the code provided in the Sign in page.
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.
Run the following to make and then navigate to the
Create the project:
Since you're creating a stand alone project, use the arrow keys to select
standalonethen press Enter.
You'll be notified that the Apps Script API has not been enabled. Click on the URL provided to enable it.
Click VIEW DASHBOARD, then click Google Apps Script API. Move the button right to enable Apps Script API.
Back in Cloud Shell, re-run the command to create the project:
Click Check my progress to verify the objective.
You just created an Apps Script Project in the folder clasp_lab!
clasp createcommand 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.
Remove the project you just created before moving to the next section:
Task 4. Clone an existing project
Now create a container-bound script for a Google Slides Add-on.
Go to slides.google.com and create a new presentation.
Name the presentation "clasp Code Test".
Click Check my progress to verify the objective.
- In the header, under Tools, select Script Editor.
This opens your Apps Script project at script.google.com.
Click on Use classic editor in the top right corner.
To clone a project, you need the Script ID. Find this value by going to File > Project properties.
In the window that pops up, in the Info tab, type clasp Code Test in the Name field.
Look for the Script ID (this value is also found in the URL). Copy the value and then click Save.
Back in Cloud Shell, clone the project, replacing with the value you copied:
- Open the Code Editor in Cloud Shell by clicking on the Open Editor icon.
You should see the cloned files
Code.js viewed in Cloud Shell code editor:
You have downloaded the project in your current directory.
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
In the script editor, create a new file by choosing File > New > Script file.
Enter the name
utils/stringsand click OK.
In the newly created file,
utils/strings.js, replace the code with the following:
Click on the
Code.gsfile, replace the existing code with the following:
Be sure to save all the files.
Try running the function by clicking Run > Run function > hello.
Go to View > Logs to see the greeting.
Task 6. Edit code locally
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.
To fix that, pull the code from your online project:
- Now go back to the code. You should notice there's a
utilsfolder now. The clasp CLI automatically converts the slash character
/to folders on the local filesystem.
Pulled files in Cloud Shell code editor:
In your favorite text editor:
util/strings.jsand replace the variable name
Code.jsby replacing "world" with "mundus".
Push your edited code to update the code on script.google.com:
And that's it! Your code is now updated on script.google.com.
clasp pushreplaces code that is on script.google.com and
clasp pullreplaces all files locally. For this reason, follow these guidelines:
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.
In Cloud Shell, create a version of your script:
Using the logged version string you created in place of [version], deploy the script:
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.
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:
- Read the Google Sheets API developer documentation.
- Post questions and find answers on Stackoverflow under the google-sheets-api tag.
- Google Workspace Learning Center
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.