[英]Google Apps Script Find function caller id
I have a Google Apps Script that dynamically generates buttons and assigns for each a ClickHandler which in turn calls a function. 我有一个Google Apps脚本,可以动态生成按钮并为每个按钮分配一个ClickHandler,然后依次调用一个函数。 My problem is that because every button calls the same function I can't find a way to indentify which of them actually made the call.
我的问题是,因为每个按钮都调用相同的函数,所以我无法找到一种方法来确定哪个按钮真正进行了调用。 Here is a code sample:
这是一个代码示例:
var handler = app.createServerHandler("buttonAction");
for (i=1,...) {
app.createButton(...).setId(i).addClickHandler(handler);
}
function buttonAction() {
//How do I know what button made the call?
}
Another option is to use the e.parameter.source value to determine the ID of the element that triggered the serverHandler to be called. 另一个选择是使用e.parameter.source值来确定触发serverHandler被调用的元素的ID。
Here's an example: 这是一个例子:
function doGet(e) {
var app = UiApp.createApplication();
var handler = app.createServerHandler("buttonAction");
for (var i = 0; i < 4; i++) {
app.add(app.createButton('button'+i).setId(i).addClickHandler(handler));
}
return app;
}
function buttonAction(e) {
var app = UiApp.getActiveApplication();
Logger.log(e.parameter.source);
}
e.parameter.source will contain the ID of the element, which you could then use to call app.getElementById(e.parameter.source) ... e.parameter.source将包含元素的ID,然后可以使用该ID来调用app.getElementById(e.parameter.source)...
You could create multiple handlers, each for one button: 您可以创建多个处理程序,每个处理程序用于一个按钮:
for (i=1,...) {
var handler = app.createServerHandler("buttonAction" + i);
app.createButton(...).setId(i).addClickHandler(handler);
}
function buttonAction1() {
// code to handle button 1
}
function buttonAction2() {
// code to handle button 2
}
function buttonAction...
I wouldn't recommend of having these sort of "anonymous" action handlers though, as you might be having troubles later in remembering which actionX does what. 不过,我不建议您使用这类“匿名”动作处理程序,因为稍后您可能很难记住哪个actionX在做什么。
(eg have a different approach, w/oa loop, or prepare a dictionary-like/array object of meaningful handler names before that loop.) (例如,采用不同的方法,使用w / oa循环,或者在该循环之前准备有意义的处理程序名称的类似字典/数组的对象。)
OTOH, you could use event object argument provided to your callback function: OTOH,您可以使用提供给回调函数的事件对象参数:
function buttonAction(event) {
// use event object here to identify where this event came from
}
The thing is the above event
object properties depends on where your callback is being called from. 关键是上述
event
对象的属性取决于调用回调的位置。 For instance, if it were a submit button where you had a Form, then you could access parameters submitted by that from like so: event.parameter.myParamName
. 例如,如果它是您拥有表单的提交按钮,那么您可以通过如下方式访问由该按钮提交的参数:
event.parameter.myParamName
。 See code sample here . 在这里查看代码示例 。
So, if you have a variable number of buttons, you could use a hidden element + the button: 因此,如果按钮的数量可变,则可以使用隐藏元素+按钮:
for (i=1,...) {
var hiddenAction = app.createHidden("action", "action"+i);
var handler = app.createServerHandler("buttonAction");
handler.addCallbackElement(hiddenAction);
var btn = app.createButton("Button text", handler);
// you'll need to add both btn and hidden field
// to the UI
app.add(hiddenAction);
app.add(btn);
}
Then, your buttonAction
might look like this: 然后,您的
buttonAction
可能看起来像这样:
function buttonAction(e) {
var action = e.parameter.action;
// do something based on action value here
// which will be one of "action1", "action2", ...
}
The above is a copy & paste from Hidden class sample. 上面是从Hidden类示例中复制并粘贴的内容。
The above might not work out of the box, but you get the idea: create a hidden element that holds the info you need in your callback, and attach that hidden to your server handler. 上面的方法可能不是开箱即用的,但您可以理解:创建一个隐藏元素,该元素保存您在回调中所需的信息,然后将其附加到服务器处理程序中。 You could even create multiple hidden elements or a Form panel.
您甚至可以创建多个隐藏元素或“表单”面板。
I have the same issue. 我有同样的问题。 It works using Tag.
它可以使用标签。
EG 例如
var button = addButton(app
,panel
,"buttonActiveTrelloProjects_" + i.toString()
,appVars.buttonWidth() + "px"
,appVars.level2ButtonHeight().toString() + "px"
,false
,false
,"Trello"
,"buttonActiveTrelloProjectsHandler"
,(appVars.buttonLhsGap() * buttonCntr) + (appVars.buttonWidth() * (buttonCntr - 1 ) + 9)
,(appVars.level2ButtonTopGap() * 34)
,3
,"button");
button.setTag(projectName );
function buttonActiveProjectsChartHandler_1(button){
...
buttonTag = getButtonTag(button);
chartType = buttonTag.split(";")[1];
activeProject = buttonTag.split(";")[0];
...
}
function getButtonTag(button){
var jsonButton = JSON.stringify(button);
var source = button.parameter.source;
var tagPtr = source + "_tag";
return button.parameter[tagPtr];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.