Google Apps Script How to access script from spreadsheet

313 Views Asked by At

I have written a Google Apps Script script using the Apps Script App in Google Suite. I also created a spreadsheet using the same account. I want to add a button to my spreadsheet that runs the script when I click it. I added an image, right clicked, hit three circles and selected the "Assign script" option. Problem: I can only assign scripts that appear under tools>script editor. I cannot assign the script that I previously wrote by opening the Apps Script app directly (in the same account). I can copy paste the whole thing into the spreadsheet's scripts, but then I have to maintain two versions. What I want I want to directly assign the script that I wrote earlier to the button.

Is this possible? Thanks.

1

There are 1 best solutions below

0
On BEST ANSWER

Issue:

Only functions in the script bound to your spreadsheet can be assigned to a clickable image/drawing:

You can also assign an Apps Script function to an image or drawing in Google Sheets, so long as the script is bound to the spreadsheet.

Use libraries:

As a workaround, I'd suggest creating a library for your standalone script (so that the script's functions can be reused in other scripts) and call it on the script bound to your spreadsheet.

You can do it the following way:

  • Visit the standalone script (project called STANDALONE in this example) where you have your desired function:
function standaloneFunction() {
  // Do some stuff
}

enter image description here

  • You can now call the standalone function in your bound script, using the Identifier:
function boundFunction() {
  STANDALONE.standaloneFunction();
}
  • And finally you can assign boundFunction to your clickable image.

Reference: