使用Apps脚本从Google Site搜索Google云端硬盘文件夹 [英] Search Google Drive folder from Google Site using Apps Script

查看:126
本文介绍了使用Apps脚本从Google Site搜索Google云端硬盘文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



无论如何,我在网上看,我碰到这段代码:

 函数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);
返回应用程序;
}

我可以在Google Site中使用Google Search Appliance启动并运行此脚本。
(链接: Google Site with changed Searchbutton。(script is embedded in page:zoeken ==>只需将/ zoeken添加到url即可)
但是,每次我搜索时,都会得到一个TypeError。是否有人能够修正上面的脚本或知道一个片段的代码,这将允许我从Google网站搜索Google云端硬盘文件夹?任何帮助将不胜感激。

试图做同样的事情,并通过谷歌发现这篇文章没有很多在那里,所以想我会添加我发现的东西马特的解决方案工作,除了UiApp已经贬值。这是同样的事情没有使用UiApp一些jquery和一个tablesorter我的一个同事建议。希望其他人可以使用它,解决他们发现的任何问题,并提高一些。

  //此代码旨在列出谷歌驱动器文件夹中的文件并输出结果s 
函数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();
}
函数getDriveFiles(文件夹,搜索){
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() :f.getOwner(),parents:f.getParents()});
}

//获取所有子文件夹并迭代
var folderIt = folder.getFolders();
while(folderIt.hasNext()){
fs = getDriveFiles(folderIt.next(),search);
for(var i = 0; i< fs.length; i ++){
files.push(fs [i]);
}
}
返回文件;

$ / code>

这是
的index.html

 <!DOCTYPE html> 
< html>
< head>
< base target =_ blank>
< link rel =stylesheethref =https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.26.2/css/theme.blue.min.css>
< script type =text / javascriptsrc =https://code.jquery.com/jquery-1.12.4.min.js>< / script>
< script type =text / javascriptsrc =https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.26.2/js/jquery.tablesorter.min.js> ;< /脚本>
< script type =text / javascriptsrc =https://cdnjs.cloudflare.com/ajax/libs/jquery.tablesorter/2.26.2/js/jquery.tablesorter.widgets.min.js >< /脚本>
< / head>
< body>
< b>搜寻:< / b> <?= query?>
< table>
< thead>
< tr>
< th>档案< / th>
< th>目录< / th>
< th>拥有者< / th>
< th>上次更新< / th>
< th>文件类型< / 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, 斑马],
widgetOptions:{
zebra:[even,odd],
},
});
});
< / script>
< / body>
< / html>


I have made a Google Site that contains a lot of Google Drive folders. Whenever I do a search using the Google Sites search thing, I can only find words that are one the Google Site. So the Google Drive Folders are not included in the search results.

Anyway, 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;
}

I can get this script up and running using Google Search Appliance in the Google Site. (link:Google Site with changed Searchbutton. (script is embedded in page: zoeken ==> just add "/zoeken" to the url.) However, everytime I do a search, I get a TypeError. Is there anybody who is able to correct the script above or knows a piece of code that will allow me to search a Google Drive folder from a Google Site? Any help would be muchly appreciated.

解决方案

Was trying to do the same thing and found this post via google. Wasn't a lot out there so thought I'd add what I found. Matt's solution worked except UiApp has since be depreciated. Here is the same thing done not using UiApp with some jquery and a tablesorter a colleague of my suggested. Hoping others can use it,fix any issue they find, and enhance it some more.

// 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;
}

Here is the index.html for this

<!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>

这篇关于使用Apps脚本从Google Site搜索Google云端硬盘文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆