In my project there is script that creates sheet and adds some images in it by insertImage function. After that it assigns script to each image. The image script needs to get the row of the image that was pressed. So can i somehow programatically determine which button was pressed by using single script for all of them? images illustration
The only way i found to determine the clicked image is by assigning individual function to each of them, that logically do the same thing, but the problem of this realisation is that there is no information about how much "buttons" will be created and it even sounds awful.
You can use the onSelectionChange simple trigger to detect when an image has been selected.
Here is a demo sheet where you can see it in action.
You may need to refresh the spreadsheet once after opening it in order to activate the trigger.
onSelectionChange is a simple trigger that runs automatically when a user changes the selection in a spreadsheet.
To activate this trigger, you must refresh the spreadsheet once the trigger is added and every time the spreadsheet is opened. I don't exactly understand that second part, since opening a spreadsheet causes it to refresh automatically. But perhaps you have to do it a second time.
The function receives an event object similar to the one received by the onEdit trigger, and just like with the onEdit trigger, if multiple selections are made in a short time, some selection change events might be skipped to reduce latency.
You can see this latency issue in the image below.