Access widgets in a VerticalPanel in Google Apps Scripts?
Is there a way to access the widgets within a panel in GAS?
Something like:
function c开发者_如何学PythonlickHandler_(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;
}
There isn't something simple like this. You have to give all widgets an id when adding them and save them somewhere so you can retrieve it later. For example, as a tag on the panel:
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;
}
By the way, you probably know that, but your code had a mistake:
It's not UiApp.getElementById
, but app.getElementById
.
精彩评论