Find the Google Drive folder from Google site using Script apps

I have created a google site containing many google drive folders. Whenever I search using the Google Sites search object, I can only find words that are a Google site. Therefore, Google Drive folders are not included in the search results.

Anyway I was looking on the internet 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 run this script using Google Search Appliance on google site. (link: Google Site with changed Searchbutton. (script embedded in page: zoeken ==> just add "/ zoeken" to url.) However, every time I do a search, I get a TypeError. Is there anyone who can fix the script above or know a piece of code that will allow me to search the google drive folder from google site? Any help would be much appreciated.

+3


source to share


2 answers


Here is some working code to find subfolders as well as the specified folder. Note that if you are looking for a large directory it takes a while to start and appears empty. It might be worth adding some kind of notification "handling" and error checking. Hope this helps someone. Feel free to fix bugs or bad practice code.



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

      

0


source


tried to do the same and found this post via google. There wasn't much, so I thought I'd add what I found. Matt worked, except the UiApp has been amortized ever since. Here's the same as not using UiApp with some jquery and tablesorter, a colleague from my suggestions. Hoping others can use it, fix any problem they find and improve it even 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 that

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

      

0


source







All Articles