Google应用程序脚本 - 回调混淆 [英] Google app script - callback confusion

查看:118
本文介绍了Google应用程序脚本 - 回调混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对以下代码示例的行为感到困惑。
为什么我不能通过应用程序对象在回调中访问statusLabelU?

I'm confused about the behavior of the following code sample. Why can't I access statusLabelU in the callback via the app object ?

它在参数中可用

顺便说一句,回调中变量e的类型是什么?

BTW, what is the type of the argument variable e in the callback ?

function doGet() {
  var app = UiApp.createApplication();

  var button = app.createButton('Enter Symbol');
  app.add(button);

  var symbolText = app.createTextBox().setName('symbolText').setId('symbolText');
  app.add(symbolText);

  var labelU = app.createLabel('Unknown symbol U')
                 .setId('statusLabelU');

  var labelK = app.createLabel('Unknown symbol K')
                 .setId('statusLabelK');
  app.add(labelU);
  app.add(labelK);

  var handler = app.createServerHandler('myClickHandler');

  handler.addCallbackElement(symbolText);
  button.addClickHandler(handler);

  return app;
}

function myClickHandler(e) {
  var app = UiApp.getActiveApplication();

  var symU = app.getElementById('symbolText');
  var symK = e.parameter.symbolText;

  var financeU = FinanceApp.getStockInfo(symU);
  var financeK = FinanceApp.getStockInfo(symK);

  var label = app.getElementById('statusLabelU');
  label.setText(financeU.name);

  var label = app.getElementById('statusLabelK');
  label.setText(financeK.name);

  app.close();
  return app;
}


推荐答案

>

If you run

labelU.setName('labelU');
handler.addCallbackElement(labelU);

您将能够像回覆一样访问回调中的标签值:

you will be be able to access the value of the label in the callback like so:

var value = e.parameter.labelU;

参数'e'(或'eventInfo')包含有关如何触发回调的信息。有一些关于用户ID,游标的x / y位置以及触发回调的源元素的一般信息。除此之外,显式添加到处理程序的小部件的值将作为参数访问。您可以通过执行

The argument 'e' (or 'eventInfo') contains information about how the callback was triggered. There is some general information about user ID, x/y position of cursor, and also the source element that triggered the callback. Apart from that, values from widgets that are explicitly added to the handler will be accessible as parameters. You can always check out the content by doing a


Logger.log(e);

并从编码环境(cmd / ctrl + return)中检出日志。

and check out the log from the coding environment (cmd/ctrl + return).

这篇关于Google应用程序脚本 - 回调混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆