最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

google apps script - can i somehow determine which clickable image on my sheet is pressed rn - Stack Overflow

programmeradmin12浏览0评论

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 Programmatically determine which button was pressed by using a 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 realization is that there is no information about how much buttons will be created and it even sounds awful.

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 Programmatically determine which button was pressed by using a 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 realization is that there is no information about how much buttons will be created and it even sounds awful.

Share Improve this question edited Feb 7 at 9:00 Patsytalk 9331 gold badge1 silver badge15 bronze badges asked Feb 5 at 2:38 pledxpledx 133 bronze badges 5
  • 1 the problem of this realisation is that there is no information about how much "buttons" will be created and it even sounds awful. Could you rephrase that or explain it a bit more? – TheMaster Commented Feb 5 at 3:53
  • Related questions: Get the row/col of a clicked drawing?, How Can I Find a Button's Location Within a Spreadsheet With Google Apps Script? – Tedinoz Commented Feb 5 at 5:11
  • +1 for the comment by @TheMaster. But be careful, pledx, that you are not describing an XY problem. The focus of the question is on a specific solution not on the underlying scenario and why you need "to get the row of the image that was pressed." – Tedinoz Commented Feb 5 at 5:16
  • @TheMaster so the task was to add clickable OverGridImage, and the number of images is unknown. With realization that i tried you need to write a function to each image: function showData0() { showData(0)} function hideData0() { hideData(0)} function showData1() { showData(1)} function hideData1() { hideData(1)}... It works but it takes a lot of memory and slows down the program. – pledx Commented Feb 6 at 14:53
  • @Tedinoz Thanks, I've been looking for solutions specifically to these issues. – pledx Commented Feb 6 at 14:57
Add a comment  | 

1 Answer 1

Reset to default 2

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.

Detect Image Selection Demo

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.

发布评论

评论列表(0)

  1. 暂无评论