2

According to Google Apps Script, container-bound scripts https://developers.google.com/apps-script/guides/bound is limited to that file.

I wrote a script that reads some values of GSheet and submits it to the server.

But I also want this spreadsheet to be cloned for different usages. But each time I clone this spreadsheet, it creates a new container-bound script and it asks for permissions again.

Is there a way to clone the spreadsheet and have it run the script without asking for permissions again?

erotsppa
  • 14,248
  • 33
  • 123
  • 181
  • I don't think you can do that - each new instance is a separate script after all with its own binding, so it makes sense that it requires you to reauthorize itself. In case your workflow includes user interaction, you could go the Sheets Add-on route - this way you will have to authorize only once. Otherwise, especially if you expect the script to run in background, it is not possible (at least as far as I am aware) – Oleg Valter is with Ukraine Jun 02 '20 at 19:41
  • Following your advise, I just moved the code to an private Addon. But I'm unable to call the code from the addon from a button? How do I do that? – erotsppa Jun 02 '20 at 20:08
  • hm, not sure what you mean by "unable" - is there an error or something stopped working? If so, could you please clarify how - there is a lot that might go wrong, unfortunately – Oleg Valter is with Ukraine Jun 02 '20 at 20:11
  • There is no ability to refer to a function inside an Add-on. Do you have documentation for this? Everything I've read so far points to that it is not possible – erotsppa Jun 02 '20 at 20:12
  • 2
    You can add an image to a Google Sheet that works as a button. An image can have a function name assigned to it. And it *can* run a function in an add-on. Of course, you can have a button in a sidebar or dialog box that runs your add-on code also, but if you want a button directly in the Sheet, then an image is the only option. You can draw the image to look like a button. – Alan Wells Jun 02 '20 at 20:16
  • How do you call a function inside an add-on, according to google you cannot. And when I put in the function name myFunction. It just says Script function myFunction could not be found – erotsppa Jun 02 '20 at 20:18
  • could you clarify what exactly you mean by "call a function inside an add-on"? In addition to what @AlanWells said, you can also make it create a menu that will call the functions inside the Add-on – Oleg Valter is with Ukraine Jun 02 '20 at 20:32
  • I dont understand what is there to clarify? I have a function inside my addon that runs some code, myFunction. I want to call that function from a button on my spreadsheet. How? The whole point of moving myFunction to the addon was so that everytime I copy the spreadsheet I dont have to reauthorize the permission – erotsppa Jun 02 '20 at 20:37
  • 3
    When a user installs an "editor" add-on, the code runs under the authority of their account, just the same as if they had bound the project to their document themselves. Of course, they can't open the add-on code. The word "function" is often use interchangeably between a cell formula, and a function in a "gs" script file. Are you using a [custom function](https://developers.google.com/gsuite/add-ons/editors/sheets#custom_functions)? – Alan Wells Jun 03 '20 at 00:01
  • 1
    Well, that's why I asked for clarification - there are, at least to my knowledge, no proper "buttons" to speak of. You can add one as image or drawing and assign a function name to run on click to it or create a menu with items and assign a function to them. Both can call the functions in the Add-on (as it is the same script project, only deployed as an Add-on which should allow you to authorize it per user to work in different spreadsheets). Also, please, see Alan's comment – Oleg Valter is with Ukraine Jun 03 '20 at 00:42

0 Answers0