Get the row/col of a clicked drawing?

北战南征 提交于 2021-01-29 13:49:55

问题


I assign to a cell in each row a png icon and a script with

sheet.insertImage(blob, 11, i).assignScript("ClickMe");

At the end of the process I have a few hundred rows with buttons at the end. What I'm struggling to do at the moment is work out which row gets clicked on. I've tried a few likely candidates like

  var ss = SpreadsheetApp.getActiveSheet();
  var sheetName = ss.getSheetName();
  var activeRange = ss.getActiveRange();
  var selection = ss.getSelection();
  var ar = activeRange.getA1Notation();
  var cc = ss.getCurrentCell().getA1Notation();
  var scr = selection.getCurrentCell().getA1Notation();

  var ac = ss.getActiveCell().getA1Notation();
  Logger.log("scr=%s, ar=%s, cc=%s, ac=%s", scr, ar, cc, ac);

but having clicked on the 9th line, the Logger.log says

[19-06-20 22:39:01:123 HKT] scr=A1, ar=A1, cc=A1, ac=A1 

So the question is this: if someone clicks on the button on the 21st line, can the script figure out what line the click occurred on?


回答1:


It’s not possible to achieve exactly what you want in your code. This is because when you use the functions “getActiveRange” and “getSelection” [1], it returns the cell or cells that are being selected (have the focus on it). When you click in the inserted image, you’re not clicking the cell but only the image, so the focus is still in the first cell.

To obtain the cell where the image is anchored you can use the following snippet:

var image = sheet.insertImage(blob, 11, i).assignScript("ClickMe");
image.getAnchorCell().getA1Notation());

The problem is that you can’t pass any parameters in the assignScript function (like the cell position in this case), only the name of the function to run [2].

A possible workaround is to adjust the size of the cell to contain the complete image. Once this is done, request to your users that they must first click in the cell and then in the image:

var image = sheet.insertImage(blob, 11, i).setHeight(20).setWidth(20).assignScript("ClickMe");

[1] https://developers.google.com/apps-script/reference/spreadsheet/range

[2] https://developers.google.com/apps-script/reference/spreadsheet/over-grid-image




回答2:


The other answer is that you need to make 21 helper functions,

what I imagine you have now (> binding script)

Button1 > script
Button2 > script
Button3 > script
Button4 > script

function script(){

//Do something
}

Instead you need

Button1 > scriptOne
Button2 > scriptTwo
Button3 > scriptThree
Button4 > scriptFour

with carrier functions:

function scriptOne (){script(1);}
function scriptTwo (){script(2);}
function scriptThree (){script(3);}
function scriptFour(){script(4);}

and then you have that info in the script function

    function script(input){
    var row = input;

    //Do something
    }

This does get tedious, and isn't exactly easy to generate in bulk, but if you have a fixed sheet, that might work.

You can also consider making the button a checkbox, and then making your script be "onEdit" and checking for which row the change occurred in. But the way you are currently thinking won't work because there is no way to bind the scripts passing a variable from the button itself. The script will see all of your current buttons as the exact same thing.



来源:https://stackoverflow.com/questions/56688428/get-the-row-col-of-a-clicked-drawing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!