Quick Setup For Google Clasp

Google Clasp is a Node.js library that you can use to edit Google Apps Script (GAS) in a local manner, using your own IDE. I like using it more than trying to play around with the GAS interface (although there are advantages to using it, like the debugger).

I use Neovim now, so I use COC for intelisense and auto-complete. It works pretty well and gives you most of what you need for an IDE. In this case, as long as you add the right extensions, it will autocomplete methods just like in a normal GAS document. Most of this I have already setup here in this Github repository, if you want to check it out. It covers my Neovim config, and what yo need to install for Node.

Essentially, you need to have the following up and running (presupposing you have Node 12.* already installed):

Then, if you have COC installed, you need to add the following extensions (just take a look at my Neovim config file):

With that, you’ll have what you need for intelisense right in the terminal. Just keep in mind that you need to type at least three or four letters and then tab for the results to come out.

So, with that done, we can start using Clasp. We’ll start by creating an empty folder (whatever you want), and then a subfolder. For each project you have for Clasp, you need to create a subfolder.

In that subfolder, type clasp login --no-localhost if you are working from a server, like Google Cloud Platfrom as I am now. (if you are working locally, you can just type clasp login ).

This will shoot out an http link which you’ll put in your browser to get a line code that you’ll then paste back into the terminal after you gave permissions.

After putting in the code, this will save a clasprc.json file in the root folder:

From here we have two options: clasp create or clasp clone . If you use clasp clone you add a script id right after it like this: `clasp clone "15ImUCpyi1Jsd8yF8Z6wey_7cw793CymWTLxOqwMka3P1CzE5hQun6qiC"

If we use clasp create we have a few options as well, for example, these:

  • clasp create --type standalone (default)
  • clasp create --type docs
  • clasp create --type sheets

We’ll just start with the first option. If we do this, it will grab the title of the subfolder we created as the title of the GAS document. This will automatically add two files as well in this subfolder: .clasp.json and appsscript.json

We are still missing the most important file, the Code.js file. We get that by typing clasp pull :

Now, for the most part, you could just go into Code.js edit something and then exit, and then type clasp push and with that, you have the changes in the GAS document in Google Drive. But, we want to do a bit more…

First, we don’t want to manually do clasp push every time we want to update the GAS document, so we will type clasp push -wso that changes are pushed every few seconds to Drive automatically.

Now comes the hard part. We also want to run functions from the terminal, instead of opening the GAS file to run them there. This take a bit more configuration…

First, we need to add the following to the appsscript.json file and get rid of what was previously there:

After playing around with this, even though the documentation says you don’t have to add the Scopes, I couldn’t get it to work without them, so just add the scopes you need there. As many as you want (I think the limit is 50, but I have not tested that many yet). Be careful of the commas, or COC will start complaining (which is a good thing, letting you know something is wrong).

After updating this file, make sure you type clasp push so it’s updated in the GAS file in Drive.

Next, you will need a project in Google Cloud Platform. If you don’t want to go there, then you can just use clasp push and just run everything from the GAS file in Drive, otherwise, you will need a project in GCP.

I will presuppose then you have one up and running, so you can grab info from that. If you go to your project and click on the three horizontal bars, and then on home, you will see in the upper left the info for that project (project name, ID, and number).

You’ll first need to get the project ID and then go back to your terminal and type clasp setting projectId ID (changing where it says ID with your project ID).

After that, you’ll have to go in Google Drive and open the GAS file you are using in Clasp. Then, go to Resources, then Cloud Platform Project, and there you have to add the Project number.

We still have to do two more things. First we have to go to the Google Cloud Platform project we are using, and then go to APIs & Services, then OAuth consent screen:

After that, just select Internal to make sure only you have access, and then put anything as an Application name, then save it (the button save is blue at the very bottom). You don’t have to do anything else here.

After that, we’ll go back to the menu and select Credentials. Then in the upper center, we’ll click on + CREATE CREDENTIALS

After that, we’ll click on OAuth client ID.

Then, we’ll select Desktop app. It will then ask you to name it. Put what you want there, and then click on the blue button CREATE.

Finally, when you go back out, you need to download the credentials so you can open them up, and copy them so as to paste them in a file you’ll create in the terminal.

In the terminal, in the same folder where your Clasp project is, create a file that is called creds.json and paste the contents of the file you downloaded there, and then save the file. If you are working locally and you can just move the file to that folder and rename it, that’s fine as well.

When that is finished, we can finally update our login info so we can run functions.

We will now type clasp login --no-localhost --creds creds.json and it will ask us to again copy a link, open it in a browser, give permissions, and grab a line of code to paste back into the terminal to finish the process.

You should now have 5 files in the folder you are setting up this Clasp project with:

Now, we can finally write a function and run it! You can now go into the Code.js file and start editing with Neovim. As you can see below, there is autocomplete…

For now, let’s just create a new Google Sheet.

With that, we can save and close. Then, we need to again type clasp push just to make sure that everything is up to date.

Then, finally, we can run our function by typing clasp run testmyclasp

With luck, you should have a new Google Sheet in your Drive with the name you gave it.

If you want to see logs, just type clasp logs and it will show you any errors that came up, and give you any text you put inside Logger.log().

I still have yet to see how to get rid of the “No response” error, but, indeed, our new Google Sheet has been created, so all is well.

UPDATE: Turns out that the “No response.” can be gotten rid of if you return something from the function. So, for the last line of the function, if you put return "done"; it will output “done” after “Running in dev mode.

Now you can use your IDE to edit GAS. Have fun!

Masters in Philosophy, Author, Professor, Consultant, Tech Promoter and Programming Novice. www.jjir.org

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store