简体   繁体   中英

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

This question is an extension of " Is 'chaining' functions available in 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. 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. 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:

var value = eventInfo.parameter.listBoxName;

However, I'm having trouble passing the grid that the list box sits within into 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():

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. 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? 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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