您似乎忘记向处理程序添加回调元素...
addCallbackElement(name);
问候, 塞尔吉
编辑:嗨乔,我也在做这样的事情......我认为展示我到目前为止所拥有的东西可能会很有趣......还没有完成;-)我粘贴了整个东西,这样你就可以轻松地测试它:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [ {name: "Doclist UI", functionName: "doclistUI"},
];
ss.addMenu("Utilities", menuEntries);//
}
//
function doclistUI(){
var folderlist = new Array();
var folders=DocsList.getFolders()
for(ff=0;ff<folders.length;++ff){
folderlist.push(folders[ff].getName());
}
var app = UiApp.createApplication().setHeight(260).setWidth(700).setStyleAttribute('background', 'beige')
.setStyleAttribute("position", "relative").setStyleAttribute("left", "10").setStyleAttribute("top", "12px");
app.setTitle("Doclist UI");
var panel = app.createVerticalPanel()
var hpanel = app.createHorizontalPanel();
var hpanel2 = app.createHorizontalPanel();
var Flist= app.createListBox(false).setName("Flb").setId("Flb").setVisibleItemCount(4).setWidth("180");
var Dlist= app.createListBox(false).setName("Dlb").setId("Dlb").setVisibleItemCount(8).setWidth("280");
var Flab=app.createLabel('Folder List').setWidth("80");
var Dlab=app.createLabel('Document List').setWidth("100");
var spacer=app.createLabel(' ').setWidth("30");
var spacer2=app.createLabel(' ').setWidth("15");
//
Flist.addItem('Choose a folder');
for(ff=0;ff<folderlist.length;++ff){
Flist.addItem(folderlist[ff]);
}
var anchor = app.createAnchor('open ', "www.google.com").setId('anchor').setVisible(false);
hpanel.add(Flab).add(Flist).add(spacer).add(Dlab).add(Dlist)
var docname = app.createLabel().setId('doc').setSize("360", "40");
hpanel2.add(anchor).add(spacer2).add(docname);
var dwnld = app.createButton("Download").setId("dwnld");
panel.add(hpanel).add(hpanel2).add(dwnld);
//
var FHandler = app.createServerHandler("click");
Flist.addChangeHandler(FHandler)
FHandler.addCallbackElement(hpanel);
//
var DHandler = app.createServerHandler("showlab");
Dlist.addChangeHandler(DHandler);
DHandler.addCallbackElement(hpanel);
//
var keyHandler = app.createServerHandler("download");
dwnld.addClickHandler(keyHandler)
keyHandler.addCallbackElement(panel);
//
app.add(panel);
var doc = SpreadsheetApp.getActive();
doc.show(app);
}
//
function click(e){
var app = UiApp.getActiveApplication();
var Dlist = app.getElementById("Dlb");
var label = app.getElementById('doc')
var folderName = e.parameter.Flb
if (folderName=='Choose a folder'){Dlist.clear();label.setText(" ");return app}
var doclist=new Array();
doclist=DocsList.getFolder(folderName).getFiles(0,2000)
if(doclist.length==0){Dlist.clear();Dlist.addItem(' No files');return app}
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
names.sort();
Dlist.clear();
for(dd=0;dd<names.length;++dd){
Dlist.addItem(names[dd][0]+" (doc Nr:"+dd+")");
}
return app ;// update UI
}
//
function showlab(e){
var app = UiApp.getActiveApplication();
var label = app.getElementById('doc')
var link = app.getElementById('anchor')
var Dlist = app.getElementById("Dlb");
var folderName = e.parameter.Flb
var docname = e.parameter.Dlb
var doclist=new Array();
Logger.log(doclist.length)
doclist=DocsList.getFolder(folderName).getFiles(0,2000)
if(doclist.length==0){return app}
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
names.sort();
var filename = e.parameter.Dlb
var ID= Number(filename.substring(filename.lastIndexOf(":")+1).replace(")",""));
var docN = docname.substr(0,docname.lastIndexOf("("))
label.setText(docN).setEnabled(false).setStyleAttribute('fontSize', '15')
link.setVisible(true).setHref("https://docs.google.com/spreadsheet/ccc?key="+names[ID][1]+"#gid=0");
return app ;// update UI
}
//
function download(e){ // this part not implemented yet :-/
var app = UiApp.getActiveApplication();
var Dlist = app.getElementById("Dlb");
var folderName = e.parameter.Flb
var doclist=new Array();
doclist=DocsList.getFolder(folderName).getFiles(0,2000)
var names = new Array();
for (nn=0;nn<doclist.length;++nn){
names.push([doclist[nn].getName(),doclist[nn].getId()]);
}
names.sort();
//
var filename = e.parameter.Dlb
var ID= Number(filename.substring(filename.lastIndexOf(":")+1).replace(")",""));
Browser.msgBox(names[ID][1])
return app ;// update UI
}
//eof