Jump to content

Recommended Posts

Posted

Hi

 

Our school website is built on Google Sites, which is working well for us apart from the search function (not really the bit of a Google product you'd expect problems with!). I've set up the website so that as much of the mostly static information is on the site, but anything that needs regularly updating is stored on Google Drive and embedded, so that the latest version is always shown. The issue is that the site search doesn't search drive - not even the titles of documents, never mind the content.

 

The only method I've found to fix this without massive spending is using a script and adding an extra search appliance, which is configured using a Google Apps Script to search a folder on our Drive. My issue is that I've taken a template script from the interwebs and I can't make it work. Please can anyone help?

 

My script is

 

function doGet(e) {
 var results = DriveApp.getFolderById('0B0ohdu2dgmsBdTBOX2dJNGZqa00').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(200);
 app.add(scrollPanel);
 return app;
}

 

And the error message I get when I run it is:

TypeError: Cannot read property "parameter" from undefined. (line 2, file "Code")

 

where line 2 is:

var results = DriveApp.getFolderById('0B0ohdu2dgmsBdTBOX2dJNGZqa00').searchFiles

and '0B0ohdu2dgmsBdTBOX2dJNGZqa00' within that line is the folder ID I've taken from Google Drive. I've tried changing that and I then get a different error message showing

No item with the given ID could be found, or you do not have permission to access it. (line 2, file "Code")
which suggests to me that the url and permissions on the folder are correct in my script.

 

Any ideas? :confused:

 

Thanks everyone

Posted
Are you sure it's not just the line space causing errors?

 

The example doesn't have a space after "searchfiles":

 

var results = DriveApp.getFolderById('0B0ohdu2dgmsBdTBOX2dJNGZqa00').searchFiles('fullText contains "' + e.parameter.q + '"');

 

 

 

https://sites.google.com/site/scriptsexamples/learn-by-example/other-examples/advanced-search-on-google-sites

 

 

Actually that makes more sense - I didn't realise the 4th line was a continuation of the second due to the spacing...

  • Thanks 1
Posted
Are you sure it's not just the line space causing errors?

 

The example doesn't have a space after "searchfiles":

 

var results = DriveApp.getFolderById('0B0ohdu2dgmsBdTBOX2dJNGZqa00').searchFiles('fullText contains "' + e.parameter.q + '"');

 

 

 

https://sites.google.com/site/scriptsexamples/learn-by-example/other-examples/advanced-search-on-google-sites

 

That's the site I nicked it from :) You've definitely picked up one of the errors - I've now noticed that depending on the width of the window that the example is displayed in, line breaks and spacing changes...

 

It's starting to make more sense to me, but I'm still getting the same error message on the same line.

Posted
I checked the permissions are correct (they are) and I did a "diff" against the code you pasted and the Google example - the whitespace problem was the only issue i could see.
  • Thanks 1
Posted

Where are you running it from? If you are running directly from the script editor then maybe it cannot define the paremeter q as no search has been done. I had this problem scripting a form response - I got errors when running within the editor as there was no responses for it to read from.

 

Try running it from the actual search box and searching for something.

  • Thanks 1
Posted
Where are you running it from? If you are running directly from the script editor then maybe it cannot define the paremeter q as no search has been done. I had this problem scripting a form response - I got errors when running within the editor as there was no responses for it to read from.

 

Try running it from the actual search box and searching for something.

 

Looks like a big step in the right direction :)

 

Next issue... I've followed the instructions to the best of my understanding:

 

I created a new page on the site called "Search Documents" and on that page I have embedded the apps script and I now see a box with the title of my App, but otherwise entirely blank. Previously there was an error message here, so it feels like progress...

 

I have added (with the updated script) a search appliance, so at the top of each page where it used to say search, it now says "Search this site" and there's a new drop down with the option to "Search school documents" - so that looks like it's working. If I enter a search term there, it re-directs me to the "Search Documents" page = still showing a blank box.

 

Thanks for the help so far - it feels like progress...

Posted

OK I have had a bit of a play with your script and I had the same blank box until I realised that the Drive folder I was searching only contained other folders... When I added a file and then used the search to find that file it did return the correct document.

 

So my question is - does the Drive folder that your ID relates to actually contain any files?

 

If not then this is why nothing is being returned. The searchFiles looks like it just ignores folders completely.

  • Thanks 1
Posted
Just as a test what happens if you search for Email?

 

In theory I would expect it to return the Email signature file...

 

I don't think that would work because you'd need an admin account to search email accounts, and you'd have to add the authorisation separately to search using the api.

Searching a populated and openly shared folder might be a better idea.

Posted
I don't think that would work because you'd need an admin account to search email accounts, and you'd have to add the authorisation separately to search using the api.

Searching a populated and openly shared folder might be a better idea.

 

Sorry not explaining myself very well - I literally mean type the word 'email' into the search box as the only file in the folder is called 'Email Signature.jpg'

Posted (edited)

I've just tested it myself on your folder and it does indeed return the 'Email Signature.jpg' file

 

So now we just need to find a way to search subfolders as well - which seems like it might be a bit tricky. Someone suggested the following on this site but I cant get it to work for me...

 

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

Edited by HC_Netman
Added link to reference site
Posted
Sorry not explaining myself very well - I literally mean type the word 'email' into the search box as the only file in the folder is called 'Email Signature.jpg'

 

Oh yes sorry. I've spent too much time searching email!

Posted

Thanks for spending so much time on this.

 

I'd directed this to the top level folder. I'll have to think about how to organise them. I really want one place to search, but I've got a folder for each page on the website at the minute.

Posted
I've just tested it myself on your folder and it does indeed return the 'Email Signature.jpg' file

 

So now we just need to find a way to search subfolders as well - which seems like it might be a bit tricky. Someone suggested the following on this site but I cant get it to work for me...

 

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

 

It works:D

 

Thank you so, so much :beer:

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...