Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

google apps script - Have a listbox populate with every folder in myDrive

I am trying to define where my file is to be saved to. I am starting out at the end user and moving inwards, so that means the start of the UI. I have the Label and the listbox but I am having troubles populating the listbox with the folders and sub folders that are in my Google Drive. The end product will be: Click the drop down menu -> chose folder to save file into -> click "submit" and it saves it into the folder.

Here is what i have so far: (Don't mind all my notes. I have zero JS experience and no programming experience so i am learning from the code that i already have from the original template maker. This is all of the code if you need it: http://pastebin.com/rbvu5Pie )

    //look here for code about the listbox to show folders
  grid.setWidget(2, 0, app.createLabel('Folder:')); //makes the label "folder" next to the listbox
  var list = app.createListBox(); //defines what to do when i say list
  grid.setWidget(2, 1, list); //puts the listbox to the right of the label

  var folder = DocsList.getAllFolders()[0]; //defines that when i say "folder" it is supposed to get all folders
  for (var i = 0; i < folder.length; i++) {
    list.addItem(folder[i].getName(),folder[i].getId())
  }
  //this is the end of the code for the listbox showing folders

Thanks for your help everyone, i really appreciate it!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Here is a test code that seems to work. I didn't test it thoroughly but it seems to do what it has to do...

I left the textBox with ID visible to make it easier to debug but you should set it invisible in the final code.

There are probably a few improvements to add but it gives the general idea...

function doGet(){
  var app = UiApp.createApplication();
  var curFN = app.createTextBox().setText('MyDrive/').setName('curFN').setId('curFN').setWidth('400');
  var curFID = app.createTextBox().setText('x').setName('curFID').setId('curFID').setWidth('400');
  var list = app.createListBox().setName('list').setId('list').addItem('please select a folder','x');
  var grid = app.createGrid(3,2).setText(0,0,'Choose a folder in your drive').setWidget(0,1,curFN).setWidget(2,1,curFID).setWidget(1,1,list);
  var folders = DocsList.getRootFolder().getFolders();
  for (var i = 0; i < folders.length; i++) {
    list.addItem(folders[i].getName(),folders[i].getId())
  } 
  var handler = app.createServerHandler('folderSelect').addCallbackElement(grid);
  list.addChangeHandler(handler);
  app.add(grid);
  return app;
}


function folderSelect(e){
  var app = UiApp.getActiveApplication();
  var currentFN = e.parameter.curFN;
  var currentFID = e.parameter.list;
  Logger.log(currentFID);
  var list = app.getElementById('list');
  var curFN = app.getElementById('curFN');
  var curFID = app.getElementById('curFID');
  if(currentFID=='x'){currentFID=DocsList.getRootFolder().getId() ; curFN.setText('MyDrive/')};
  var startFolder = DocsList.getFolderById(currentFID);
  var folders = startFolder.getFolders();
  list.clear().addItem('no other subFolder','x').addItem('Go back to Root','x');
  if(folders.length>0){list.clear(); list.addItem('please select a subFolder','x')};
  for (var i = 0; i < folders.length; i++) {
    list.addItem(folders[i].getName(),folders[i].getId())
  } 
  curFN.setText(currentFN+DocsList.getFolderById(currentFID).getName()+'/');
  if(currentFID==DocsList.getRootFolder().getId()){curFN.setText('MyDrive/')};
  curFID.setText(currentFID);
  return app;
}

App online here, needs authorization for Docslist Service


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...