使用应用脚本

时间:2015-05-07 19:04:47

标签: google-apps-script google-drive-api google-sites

我制作了一个包含大量Google云端硬盘文件夹的Google网站。 每当我使用Google协作平台搜索进行搜索时,我只能找到属于Google网站的字词。因此,Google云端硬盘文件夹不会包含在搜索结果中。

无论如何,I was looking on the web and I came across this piece of code:

function doGet(e) {
  var results = DriveApp.getFolderById('File ID').searchFiles('fullText contains "' + e.parameter.q + '"');
  var app = UiApp.createApplication();
  var panel = app.createVerticalPanel();

  while(results.hasNext()) {
    var file = results.next();
    panel.add(app.createAnchor(file.getName(), file.getUrl()));
  }

  var scrollPanel = app.createScrollPanel(panel).setHeight(800);
  app.add(scrollPanel);
  return app;
}

我可以在Google Site中使用Google Search Appliance启动并运行此脚本。 (联系:Google Site with changed Searchbutton. (script is embedded in page: zoeken ==> just add "/zoeken" to the url.) 但是,每次我进行搜索时,都会遇到TypeError。有没有人能够纠正上面的脚本或知道一段代码,允许我从Google网站搜索Google云端硬盘文件夹?任何帮助都会非常感激。

2 个答案:

答案 0 :(得分:1)

试图做同样的事情并通过谷歌发现这篇文章。在那里不是很多,所以以为我会添加我发现的东西。 Matt的解决方案起作用,但UiApp已被折旧。以下是不使用UiApp的一些jquery和一个tableorter我的建议的同事。希望其他人可以使用它,修复他们发现的任何问题,并进一步增强它。

// This code is designed to list files in a google drive folder and output the results as a table.
function doGet(e) {
  var gotResults = getDriveFiles(DriveApp.getFolderById('File ID'), e.parameter.q);

 var output = HtmlService.createTemplateFromFile('index.html');
 output.results = gotResults;
 output.query = e.parameter.q;

  return output.evaluate();
}
function getDriveFiles(folder,search) {
  var files = [];
  var fileIt = folder.searchFiles('fullText contains "' + search + '"');;
  while ( fileIt.hasNext() ) {
    var f = fileIt.next();
    files.push({id: f.getId(), name: f.getName(), URL: f.getUrl(), lastupdate: f.getLastUpdated(), MIME: f.getMimeType(), owner: f.getOwner(), parents: f.getParents()});
  }

  // Get all the sub-folders and iterate
  var folderIt = folder.getFolders();
  while(folderIt.hasNext()) {
    fs = getDriveFiles(folderIt.next(),search);
    for (var i = 0; i < fs.length; i++) {
      files.push(fs[i]);
    }
  }
    return files;
}

这是

的index.html
<!DOCTYPE html>
<html>
  <head>
    <base target="_blank">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.26.2/css/theme.blue.min.css">
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.26.2/js/jquery.tablesorter.min.js"></script>
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.26.2/js/jquery.tablesorter.widgets.min.js"></script>   
  </head>
  <body>
    <b>Search:</b> <?= query ?>
    <table>
          <thead>
           <tr>
            <th>File</th>
            <th>Directory</th>
            <th>Owner</th>
            <th>Last Updated</th>
            <th>File Type</th>
           </tr>
          </thead>
          <tbody>
          <? 
          for(var x=0; x<results.length; x++){
            ?><tr>
            <td><a href="<?= results[x].URL ?>" target="_blank"><?= results[x].name ?></a></td>
            <td> <? while (results[x].parents.hasNext()) { ?>
              <?= results[x].parents.next().getName() ?>/
            <? }  ?> </td>
            <td><?= results[x].owner.getName() ?></td>
            <td><?= Utilities.formatDate(results[x].lastupdate, "EDT", "yyyy-MM-dd h:mm a ") ?></td>
            <td><?= results[x].MIME ?></td>
            </tr>
           <? } ?>
           </tbody>
    </table>

    <script>
       $(document).ready(function() { 
          $("table").tablesorter({
            theme: 'blue',
            widgets: ["uitheme","zebra"],
            widgetOptions : {
               zebra : ["even", "odd"],         
            },                  
          });
       });      
    </script>
  </body>
</html>

答案 1 :(得分:0)

以下是一些用于搜索子文件夹和指定文件夹的工作代码。请注意,如果要搜索大型目录,则需要一段时间才能运行并显示为空白。可能值得添加某种&#34;处理&#34;通知和错误检查。希望这有助于某人。随意纠正任何错误或不良行为代码。

    /* adapted origional code and code from here: http://qiita.com/atsaki/items/60dbdfe5ab5133a5f875 */
    function doGet(e) {
  var results = getDriveFiles(DriveApp.getFolderById('File Id'), e.parameter.q);
  var app = UiApp.createApplication();
  var panel = app.createVerticalPanel();
  for(var x=0; x<results.length; x++){
    panel.add(app.createAnchor(results[x].name, results[x].URL));
  }
  var scrollPanel = app.createScrollPanel(panel).setHeight(200);
  app.add(scrollPanel);
  return app;
}
function getDriveFiles(folder,search) {
    var files = [];
    var fileIt = folder.searchFiles('fullText contains "' + search + '"');;
    while ( fileIt.hasNext() ) {
        var f = fileIt.next();
        files.push({id: f.getId(), name: f.getName(), URL: f.getUrl()});
    }

    // Get all the sub-folders and iterate
    var folderIt = folder.getFolders();
    while(folderIt.hasNext()) {
        fs = getDriveFiles(folderIt.next(),search);
        for (var i = 0; i < fs.length; i++) {
            files.push(fs[i]);
        }
    }

    return files;
}