将驱动器文件夹列表发送到列表框HTML

时间:2015-09-26 00:43:21

标签: google-apps-script google-apps

我有一个脚本,它会拉动我的驱动器中的所有文件夹。我想将此数据推送到以HTML格式创建的列表框中(不推荐使用UiApp)

我的代码是(请注意,sheet.appendRow行不起作用,如果你知道为什么并且感觉解释会很棒!)

function updateFoldersListbox(FoldersListBox) {

var rootFolder = DriveApp.getRootFolder();
var driveFolders = rootFolder.getFolders();

  while (driveFolders.hasNext()) {
  var addMonitoredFolder = driveFolders.next();
  Logger.log(addMonitoredFolder);
  //sheet.appendRow(addMonitoredFolder);
}

日志的输出是:

[15-09-26 01:38:46:603 BST] Test Folder
[15-09-26 01:38:46:605 BST] Untitled folder
[15-09-26 01:38:46:606 BST] Notes

现在将其推送到列表框并添加测试文件夹,无标题文件夹和备注作为选项:

  <select name="sometext" size="5">
    <option>option1</option>
  </select> 

怎么做的?

1 个答案:

答案 0 :(得分:1)

将以下代码放在脚本文件中:

function onOpen() {
  SpreadsheetApp.getUi().createMenu('Dialog').addItem('Open', 'openDialog').addToUi();
};

function openDialog() {
  var html = HtmlService.createHtmlOutputFromFile('Index')
  .setSandboxMode(HtmlService.SandboxMode.IFRAME);
  SpreadsheetApp.getUi().showModalDialog(html, 'Select a Google Drive Folder:');
};

function updateFoldersListbox() {  
  var rootFolder = DriveApp.getRootFolder();
  var driveFolders = rootFolder.getFolders();
  var listnames = new Array();
  while (driveFolders.hasNext()) {
    var addMonitoredFolder = driveFolders.next().getName();
    listnames.push([addMonitoredFolder]);
    //Logger.log(addMonitoredFolder);
    //sheet.appendRow(addMonitoredFolder);
  }
  return listnames;
};

并将以下代码放在HTML文件'Index.html'中:

<!DOCTYPE html>
<html>
  <head>
  <base target="_top">
  </head>
  <body>

  <select id="menu">  </select>

  <script
  src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
  </script>
  <script>
  // The code in this function runs when the page is loaded.
  $(function() {
    google.script.run.withSuccessHandler(showThings)
    .updateFoldersListbox();
    google.script.run.withSuccessHandler(showMenu)
    .updateFoldersListbox();
    });

  function showThings(things) {
    var list = $('#things');
    list.empty();
    for (var i = 0; i < things.length; i++) {
      list.append('<li>' + things[i] + '</li>');
    }
  }

  function showMenu(menuItems) {
    var list = $('#menu');
    list.find('option').remove();  // remove existing contents

    for (var i = 0; i < menuItems.length; i++) {
      list.append('<option>' + menuItems[i] + '</option>');
    }
  }

  </script>
  </body>
</html>

现在,从脚本编辑器运行'openDialog'功能 或者刷新电子表格并从自定义菜单“对话框”&gt;运行该功能。 '打开'。