How to Set Up Google Apps Script Projects with CLASP and Automate Push with Git

How to Set Up Google Apps Script Projects with CLASP and Automate Push with Git

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:

  1. Install and configure CLASP
  2. Set up your project with Git and GitHub
  3. 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

  1. 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.
  2. 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>
  3. 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.

3. Set Up Git for Version Control

  1. Initialize Git in Your Project:git init git add . git commit -m "Initial commit"
  2. Create a GitHub Repository:
    • Go to GitHub, create a new repository, and copy the repository URL.
  3. 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:

  1. Navigate to the hooks folder inside your Git project:cd .git/hooks
  2. Create a pre-push file:nano pre-push
  3. 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
  4. 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:

  1. Push your changes to GitHub.
  2. Trigger the pre-push hook, which runs clasp 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 like command not found: clasp, ensure you’re using the full path to CLASP.
    Find the path with:which clasp Update the pre-push hook to use the full path.
  • Permission Issues:
    Make sure the pre-push hook is executable:chmod +x .git/hooks/pre-push
  • Test Your Setup:
    Run the following command to ensure clasp 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.