简体   繁体   English

链接函数如何访问Google Apps脚本中的表单数据?

[英]How can chained functions access form data in Google Apps Scripts?

This question is an extension of " Is 'chaining' functions available in GAS? ." 此问题是“ GAS中是否提供“链接”功能? ”的扩展。

I need to do something similar... copy/pasting the code example which answered the previous question: 我需要做类似的事情...复制/粘贴可以回答上一个问题的代码示例:

var app = null;
function firstHandler(e) {
  if( app == null )
    app = UiApp.getActiveApplication();
  //do your thing
  //now, instead "return app;" you return the second handler
  return secondHandler(e);
}

function secondHandler(e) {
  if( app == null )
    app = UiApp.getActiveApplication();
  //do your job
  return app;
}

Where firstHandler() is the click handler for a button. 其中firstHandler()是按钮的单击处理程序。 The difference between this example and what I need to do is that, in my case, secondHandler() needs to pull a piece of information from a form. 此示例与我需要做的区别是,就我而言,secondHandler()需要从表单中提取一条信息。 Specifically, I need to get the selected value from a List box. 具体来说,我需要从“列表”框中获取选定的值。

Normally, the way I would have secondHandler() pull that info in would be to pass the parent object containing the list box into the function, then have a line like this: 通常,我要让secondHandler()获取该信息的方式是将包含列表框的父对象传递到函数中,然后有如下一行:

var value = eventInfo.parameter.listBoxName;

However, I'm having trouble passing the grid that the list box sits within into secondHandler(). 但是,我无法将列表框所在的网格传递到secondHandler()中。 Here's what I've tried: 这是我尝试过的:

var app = null;
function firstHandler(e) {
  if( app == null )
    app = UiApp.getActiveApplication();
  //do your thing
  //get the object now, pass it into the chained function
  var myGrid = app.getElementById("gridId");
  return secondHandler(myGrid);
}

function secondHandler(e) {
  if( app == null )
    app = UiApp.getActiveApplication();
  var value = e.parameter.listBoxName.toString();
  //do your job
  return app;
}

But this hasn't been working. 但是,这没有奏效。 I've also tried modifying secondHandler() to be completely argument free, such that I can bypass having to pass the grid into secondHandler() within firstHandler(): 我还尝试将secondHandler()修改为完全无参数,这样我就可以绕过将网格传递到firstHandler()中的secondHandler()中的麻烦:

var app = null;
function firstHandler(e) {
  if( app == null )
    app = UiApp.getActiveApplication();
  //do your thing
  return secondHandler(e);
}

function secondHandler(e) {
  if( app == null )
    app = UiApp.getActiveApplication();
  var value = app.getElementById('listBoxId').value;
  //do your job
  return app;
}

This doesn't work either... "app.getElementById('listBoxId').value" doesn't seem to return anything. 这也不起作用...“ app.getElementById('listBoxId')。value”似乎不返回任何内容。 I have no idea why not :( 我不知道为什么不:(

Any thoughts about how I can do this? 关于我该如何做的任何想法? Anyone know why my app.getElementById workaround isn't behaving as expected? 有人知道为什么我的app.getElementById解决方法无法按预期进行吗? Any and all help appreciated!!! 任何和所有帮助表示赞赏!!!

Try this. 尝试这个。

function firstHandler(e) {
  var app = UiApp.getActiveApplication();
  //do your thing
  return secondHandler(e);
}

function secondHandler(e) {
  var app = UiApp.getActiveApplication();
  var value = e.parameter.listBoxName;
  //do your job
  return app;
}

Be sure that you use this when creating the handler. 确保在创建处理程序时使用此功能。

app.createServerHandler("firstHandler").addCallbackElement(gridContainingListBox);

If you're retrieving values from the page, you have to use e.parameter which is properly set when you add the proper callback element. 如果要从页面中检索值,则必须使用e.parameter ,在添加适当的回调元素时已正确设置。 If you're going to be modifying elements in the UI, then you can certainly use app.getElementById(...).whateverFunction() , but just note that you can't call getValue() , etc. 如果要在UI中修改元素,那么可以使用app.getElementById(...).whateverFunction() ,但是请注意,您不能调用getValue()等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM