Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script to search Google Drive

Is it possible to use Google Apps Script to search Google Drive for both documents and folders?

Google have killed their own docs/drive search gadget as it appears to rely on iGoogle and Google Enterprise support have admitted this.

Thank you

like image 646
Adam157 Avatar asked Nov 01 '13 07:11

Adam157


People also ask

How do I run a script in Google Drive app?

To run a function from the script editor, at the top, select the name of the function you want to execute and click Run.

Can I run script at Google Drive?

Yes, a script from Google Apps Script on any Google Workspace Editor (Docs, Forms, Slides, Sheets) or any other place can access all your Google Drive files if they have requested the corresponding permission and you authorized the script to run.


1 Answers

I think you are looking for SearchFile and SearchFolder of the DriveApp. The full list of parameters is available in the Google Drive SDK documentation

I've run some tests and seems like it's not possible to do 1 search and get files and folders like it's possible calling the search function from the Google Drive API.

Here a code that list the files and folders with a title that have 2013 in it

function myFunction() {
  var searchFor ='title contains "2013"';
  var names =[];
  var files = DriveApp.searchFiles(searchFor);
  while (files.hasNext()) {
    var file = files.next();
    names.push(file.getName());
  }
  var folders = DriveApp.searchFolders(searchFor);
  while (folders.hasNext()) {
    var file = folders.next();
    names.push(file.getName());
  }
  for (var i=0;i<names.length;i++){
    Logger.log(names[i]);
  }

}
like image 56
smokybob Avatar answered Nov 23 '22 05:11

smokybob