在Google Apps脚本中的VerticalPanel中访问小部件?

时间:2011-09-22 16:57:40

标签: google-apps-script

有没有办法在GAS中访问面板中的小部件?

类似的东西:

function clickHandler_(e) {
  var app = UiApp.getActiveApplication();
  var panel = app.getElementById(e.parameter.whatever); // short-cutting here
  for (var i=0; i<panel.widgets.length; i++) {
    var widget = panel.widgets[i];
    // do something with them
  }
  return app;
}

1 个答案:

答案 0 :(得分:0)

没有像这样简单的东西。 您必须在添加它们时为所有窗口小部件提供ID,并将它们保存在某处,以便稍后检索它。例如,作为面板上的标签:

function doGet(e) {
  var app = UiApp.createApplication();
  ...
  var panel = app.createXYZPanel().setId('mypanel');
  var IDs = ''; //list of child widgets on this panel
  panel.add(widget.setId('id1'));
  IDs += ',id1';
  panel.add(widget2.setId('id2'));
  IDs += ',id2';
  //and so on...
  panel.setTag(IDs); //save the IDs list as a tag on the panel
  ...
  return app;
}

//...later on a handler
function handler(e) {
  var app = UiApp.getActiveApplication();
  //the panel or a parent must be added as callback of the handler
  var IDs = e.parameter.mypanel_tag.split(',');
  for( var i = 1; i < IDs.length; ++i ) { //skipping the 1st empty element
    var widget = app.getElementById(IDs[i]);
    //do something
  }
  return app;
}

顺便说一句,您可能知道,但您的代码有错误: 它不是UiApp.getElementById,而是app.getElementById