3
votes

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.

2
After you get the TypeError, go into the script editor, and under the VIEW menu, choose EXECUTION TRANSCRIPT. At the bottom of the log, look for a message that states that the script failed, and on what line. If you can find out what line the code is failing on, that will help. - Alan Wells
Hi, I implemented the script as a web-app, and I got rid of the TypeError... However, when I execute the script (by doing a search for: 'Blogger' on this site, It shows me a blank page. It's probably something really stupid, but I can't get it to work. - Jasper Cuvelier
Maybe putting some error handling into your code would help; like a try, catch block. - Alan Wells
Hi, I wouldn't have a clue on how to do so... :-) - Jasper Cuvelier
In the doGet(e) function name, there is the e in the parenthesis. That is getting some data. Then the data is retrieved with e.parameter.q. Put in this line of code, right at the top: Logger.log('e.parameter.q: ' + e.parameter.q) Then run your code, go into the script editor, and use the VIEW, menu and choose LOGS. Hopefully there will be a line in there that will tell you what is getting retrieved with e.parameter.q. Comment back with what it is. - Alan Wells

2 Answers

2
votes

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>
0
votes

Here is some working code for searching subfolders as well as specified folder. Note that if you are searching a large directory this takes a while to run and appears blank. Might be worth adding some sort of "processing" notification and error checking as well. Hope this helps someone. Feel free to correct any mistakes 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;
}