banner
Light

Light Log

做充满希望的动物
x
github
bilibili
steam

Google Apps Script Learning Record

gas-eyecatch-960x504

I first heard about GAS (Google Apps Script) when I joined my current company as an intern. A senior colleague asked me to take over the development of a small internal tool used by the company. That tool was written in GAS. Based on Google's own technology, it seamlessly integrates with various Google tools, and its open API is comprehensive and easy to use (of course, you can also call Google's API in other languages, but GAS provides the best experience because of its seamless integration). The most impressive feature is its support for Google Spreadsheet and Google Calendar. In my spare time, I used GAS to write a function that automatically imports the schedule on the Spreadsheet to Calendar. It was basically just using the official API, and coding was smooth and satisfying. If you are often tortured by various complicated documents, you must try GAS. As a scripting language (or platform), it can be complex in some cases, but when you just want to automate and collaborate on a simple Google service, it provides a great experience from the very beginning.

Localization and Version Control#

GAS provides a cloud-based editor and also supports simple version control. However, for those who are used to Git and Github, localization may be a better choice. Google's developers have provided a great open-source tool for this: Clasp.

Installing Clasp#

Clasp is packaged as a package, so if you have npm installed locally, you just need to run:

npm install -g @google/clasp

If you don't have npm installed locally, I recommend installing it. You can go to the Node.js download page here. They provide support for multiple platforms. Also, a friendly reminder, don't blindly update to the latest version. The LTS version is a more reliable choice (no one wants to see a few lines of red warning popping up every time they run a program).

Before we start using clasp to build our scripts and applications, there is one important thing we need to do. That is to log in to our Google account. Otherwise, Google will reject our interaction with the cloud without connection permissions. We can run the following command:

clasp login

This will open the default browser on our local machine and ask us to log in to our Google account. We need to log in with the account we want to deploy the application or script on.

Of course, if you don't trust the environment your computer is in, you can choose to log out every time you leave. This can be done with a simple command:

clasp logout

Okay, now we have completed the preparation and can start deploying our project locally using clasp.

Deploying a Project Locally#

With Clasp, we can easily create a new project locally:

clasp create

This will create a default new project in the current working directory. Of course, it also provides some richer options. We can use --title "My Script" to specify the name of the project, and --type to specify the type of the project. By using the --rootDir option, we can also specify the location where the new project is stored. There is also an option closely related to Google services, --parentId "1D_Gxyv*****************************NXO7o", which allows us to bind the project to a specific service, such as a Google Spreadsheet. By the way, we can find the ID on the corresponding service's website: docs.google.com/presentation/d/{id}/edit

In most cases, we often write projects for existing services. In this case, the clasp clone command is very useful. We can directly copy the corresponding script project to our local machine using the URL of the corresponding service:

clasp clone "https://script.google.com/d/15ImUCpyi1Jsd8yF8Z6wey_7cw793CymWTLxOqwMka3P1CzE5hQun6qiC/edit"

Now, we have successfully deployed the project locally. When we want to publish to the cloud or pull from the cloud, we can use the clasp pull and clasp push commands. Please note that these two commands are fundamentally different from Git. They will simply overwrite the original content. A friendly suggestion is to avoid using clasp pull and only use clasp push when necessary. Of course, the clasp development team is very considerate. When using clasp pull, they usually won't directly overwrite our local files. When clasp detects that there is a file with the same name locally, it will terminate the process and remind us of this.

Using Git to Manage Local GAS Projects#

We cannot rely on the built-in version control system in GAS, as it only provides a limited number of versions and its operations are not as complete as Git. So our next goal is to use Git and Github to manage our local projects. It's actually just initializing a new Git repository locally.

We navigate to the corresponding project in the working directory. Then we initialize a Git repository:

git init

If you just want to manage versions locally, you can use Git as usual at this point. But if you want to upload to a cloud repository like Github, we need a few more steps.

First, we need to write a .gitignore file to let Git ignore some files that only need to be kept locally and don't want Git to manage. If we look at the project created by clasp locally, we will find a hidden .clasp.json file, which records our script id and parent id for clasp to locate the relevant files in the cloud. Obviously, this is not something Git needs to manage. So we can ignore it in .gitignore.

touch .gitignore
echo ".clasp.json" > .gitignore

Of course, you can also open the document with an editor (such as vscode), edit it, and save it.

Now we can execute git add and other commands normally.

git add --all
git commit  -m "init"

Finally, you can create a new repository on Github and push the local repository to it.

To be continued...

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.