Managing code for Google Apps Script can be a hassle, especially if you want version control. Luckily, you can use CLASP (Command Line Apps Script) to develop scripts locally and sync them with your Google Apps Script projects. In this guide, we’ll cover how to:
- Install and configure CLASP
- Set up your project with Git and GitHub
- Automate
clasp push
with Git pre-push hooks
1. Installing Prerequisites
Install Node.js and NPM
- Download and install Node.js.
- Verify the installation:
node -v npm -v
Install CLASP globally using NPM:
npm install -g @google/clasp
Log in to Google Apps Script with CLASP:
clasp login
- This will open a browser window to authenticate your Google account.
2. Setting Up Your Google Apps Script Project Locally
- Get the Script ID:
- Open your Apps Script project in Google Sheets or Google Drive.
- Go to Extensions → Apps Script → Project Settings.
- Copy the Script ID.
- Clone the Script Locally:
- Create a new folder for your project:
mkdir my-apps-script && cd my-apps-script
- Clone the script with CLASP:
clasp clone <SCRIPT_ID>
- Create a new folder for your project:
- Verify Files:
- You should see files like
Code.gs
or other script files from your project. Now you can edit them locally with your favorite editor, such as Visual Studio Code.
- You should see files like
3. Set Up Git for Version Control
- Initialize Git in Your Project:
git init git add . git commit -m "Initial commit"
- Create a GitHub Repository:
- Go to GitHub, create a new repository, and copy the repository URL.
- Add GitHub as a Remote:
git remote add origin <REPO_URL> git push -u origin main
4. Automate clasp push
Using Git Hooks
Since Git hooks are executed locally, we’ll use the pre-push
hook to ensure that every time you push to GitHub, the code also syncs with Google Apps Script.
Create the Pre-Push Hook:
- Navigate to the hooks folder inside your Git project:
cd .git/hooks
- Create a
pre-push
file:nano pre-push
- Add the following code to the
pre-push
file:#!/bin/bash echo "Pushing to Google Apps Script..." /Users/your-username/.nvm/versions/node/v16.20.2/bin/clasp push
- Make the hook executable:
chmod +x pre-push
5. Push Your Code to GitHub and Google Apps Script
Now that everything is set up, you can push your changes to both GitHub and Google Apps Script with a single command:
git add .
git commit -m "Updated script"
git push
This will:
- Push your changes to GitHub.
- Trigger the
pre-push
hook, which runsclasp push
to sync your code with Google Apps Script.
6. Troubleshooting Common Issues
- CLASP Not Found in Pre-Push Hook:
If you get an error likecommand not found: clasp
, ensure you’re using the full path to CLASP.
Find the path with:which clasp
Update thepre-push
hook to use the full path. - Permission Issues:
Make sure thepre-push
hook is executable:chmod +x .git/hooks/pre-push
- Test Your Setup:
Run the following command to ensureclasp push
works independently:/Users/your-username/.nvm/versions/node/v16.20.2/bin/clasp push
By following this tutorial, you’ve learned how to:
- Install and configure CLASP to manage Google Apps Script projects locally.
- Use Git and GitHub to version control your code.
- Automate
clasp push
using Git hooks to sync changes effortlessly.
This setup ensures your code is always backed up in GitHub and Google Apps Script, preventing code loss and making collaboration easier.