DocsListDialog示例

时间:2012-05-12 23:56:41

标签: google-apps-script

谷歌应用程序脚本仍然相当新,并且看到了一个很糟糕的文档课程。

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返回'undefined'。

除了上面的网址之外,我在这个课上寻找了一些例子,但我什么也没得到:(

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

任何帮助将不胜感激

2 个答案:

答案 0 :(得分: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;
}

希望有所帮助

答案 1 :(得分: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