0
votes

I'm building a Google Spreadsheet-based app in Google Script. One of the things it does is output a selection of items attached to a parent item in rows. I'd like for the user to be able to open an editing interface for these items by clicking an image that is also generated for each row.

In Google Sheets, you can right-click on an inserted image to open a contextual menu and attach a script to it. Google Script lets you use the assignScript method to assign a script to an image in the sheet, but for some reason when you call that function it pops this error: "Script function function editContract(){ Logger.log("contract edit") } could not be found"

Here's the code. Nothing complicated!

var fileId = '17-6V_HBlSGwHW-_NeLzUo5kizxxrYOjG';
var img = DriveApp.getFileById(fileId).getBlob();
// Insert the image in cell.
searchSheet.insertImage(img, 10, writeRow);    
// now we need to attach a script to that image  
images = searchSheet.getImages(); 
images[images.length - 1].assignScript(editContract);
2

2 Answers

1
votes

The argument of assignScript(functionName) is the string value. I think that this is the reason of your issue. So in your script, please modify as follows.

From:

images[images.length - 1].assignScript(editContract);
  • In this case, the script of function is directly put. By this, when the created button is clicked, I think that when the button is clicked, such error message is shown.

To:

images[images.length - 1].assignScript("editContract");
  • Please set the function name as the string value like above.

Reference:

1
votes

Responding for posterity: function names in .assignScript need to be encapsulated in quotes. Would have been cool for that to be documented somewhere.