Search code examples
google-apps-scriptgoogle-sheetsgoogle-sheets-apiclasp

Google Sheet API - sheet creation with specific app scripts


I need to be able to create Google sheets on the fly and link them to some app scripts code (some gs and html files). For the sheets creation, using the API is fine but I couldn't figure out how to then attach app scripts code to it.

I have my app script code in a central project that I update through a CI process. I need all the google sheets to be fed by the app script code from this central project so that when I update this code, all the google sheets will get the update.

Thank you.


Solution

  • There are 3 ways to bind an Apps Script project to a Google Sheet.

    • Tools Menu - Script Editor menu item
    • Copy an existing Sheets file with a project bound to it - If the original Sheets file has an Apps Script project bound to it, the the Sheets file copy will also have a project bound to it.
    • Publish an add-on, and have the user install the add-on.

    There are library's, but a library can not be directly bound to the Sheets document. A library is simply one Apps Script project bound to another Apps Script project. So, even with a library you still need a second Apps Script project bound to the document. (The document being the Sheets file in this case)

    There is no way to bind an Apps Script file to a Google Sheets file using code.

    If you want to keep your code from being seen by the users, then your only option is an add-on. Library code can be seen by the public. And add-on can be published as Public, unlisted, or private to a G Suite domain. So, you have two choices for keeping it from being seen by the public.

    If your main goal is to have users always be using the latest version of your code, then your only real option is an Add-on. You could have users add a library that uses the "head" version, but if users were using the "head" version, then you wouldn't want to do any development and testing on the library file itself.

    Copying Google Sheets template files, in order to create a new Sheets file with a project bound to it has a problem, because each bound project would need to be updated individually, unless the bound project had a library installed in it. I'm not sure if a bound script file with a library installed will also have a library installed when a copy of the Sheets file is made.

    The Apps Script API can modify an Apps Script projects manifest file, which can be used to automate the updating of projects to a new library version. Otherwise the user would need to manually change the version number. But you'd need to keep track of every project file ID so that your code could access every user's Apps Script project file. An add-on doesn't have that problem, the user is always using the latest published version of the add-on, and you don't need to know or keep track of who every user is.