google-apps-scriptclasp

Is there a way to tie code to multiple GAS using clasp for testing?


I work on a lot of google scripts that are tied to different people's google sheets. Recently I've been trying to shift to using Clasp and VS Code to make it easier to keep a history and add to Github. This is great as it helps streamline my process however, most of the GAS I am working are tied to sheets that are used for someone else's business, which means if I push buggy code, I can mess up their business.

I don't want to push directly to the "production script" without testing the code first using the dummy sheet I built along with my dummy calendar and dummy email. This means that I first want to push the code to my "dummy script", test it, then, when ready, push the code to the "production sheet.

Is there a way to do this? I have looked, but no one seems to have a similar need to my own as most people use clasp to push scripts, then deploy them after testing. My need is different since, when I push the code, it will be effective immediately and does not deploy. Any help on this issue would be appreciated.


Solution

  • This requires some manual manipulation before pushing, but one approach that at least allows you to maintain a single copy of the code is as follows:

    Clasp always pushes to the script identified by the script id in the file .clasp.json (how to find a project's script id). This file usually contains exactly one key/value pair:

    {
        "scriptId": "abcdefghijklmnopqrstuvwxyz12345678910",
    }
    

    By experimentation, I've discovered that additional key/value pairs can be included, and clasp will ignore them. It only pushes to the script identified by scriptId. Therefore, I keep a list of all projects I might want to push to, in .clasp.json, something like this:

    {
        "scriptId": "abcdefghijklmnopqrstuvwxyz12345678910",
        "production": "987654321zxywvutrsqpomnlkjifghedcba",
        "development": "abcdefghijklmnopqrstuvwxyz12345678910"
    }
    

    As currently shown, clasp push will push to the development copy. If you want to push to production, copy and paste the production id value into the top scriptId line and then clasp push.

    It's not completely automatic but at least it keeps all the script id's in one place. You could also write a short command line script that performs the copy/paste manipulation automatically and pushes to the desired branch.