0

对谷歌应用脚​​本来说还是相当新的,并且看到了一个很棒的课程,里面有蹩脚的文档。

https://developers.google.com/apps-script/class_docslistdialog。我希望使用它来允许我的用户从他们的 Google 收藏中选择一个文档。

这是我开始的:

var app = UiApp.createApplication().setHeight(400).setWidth(600);
var ss = SpreadsheetApp.getActiveSpreadsheet();
var doclisthandler = app.createServerHandler('selectionhandler');
var doclist = app.createDocsListDialog().showDocsPicker().addSelectionHandler(doclisthandler); 
ss.show(app);}

function selectionhandler(e)
{    var app = UiApp.getActiveApplication();
var selname = e.parameter.doclist;           
toastDoc.toast(selname,'here in selectionhandler',4);
return app;
}

我的 toastDoc.toast 只是为了验证我的 selectionhandler 函数是否有效并查看返回的内容。selname 返回“未定义”。

除了上面的 url 中的内容之外,我已经在这个类上寻找了示例,但我什么也没得到:(

理想情况下,我想返回选择的文档 ID,以便我的脚本可以进行进一步处理......

任何援助将不胜感激

4

2 回答 2

4

要检查它返回的内容,您可以使用以下函数。一切都将在 Logger 中返回

function selectionhandler(e){
  var app = UiApp.getActiveApplication();
  Logger.log(e.parameter.items[0].id);
  Logger.log(e.parameter.items[0].name);
  Logger.log(e.parameter.items[0].url);
//Or to check everything which is returned in e.parameter
  Logger.log(e.parameter);
  return app;
}

希望有帮助

于 2012-05-13T06:01:00.283 回答
0

您似乎忘记向处理程序添加回调元素...

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
于 2012-05-13T12:52:16.347 回答