Google Apps Script to search Google Drive

旧城冷巷雨未停 提交于 2019-12-03 08:44:05

问题


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


回答1:


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]);
  }

}



回答2:


Try this piece of code

function searchDrive() {
  var folderToSearch = "FolderName";
  var folders = DriveApp.getFoldersByName(folderToSearch);
  Logger.log(folders);

  var fileToSearch = "fileName";
  var files = DriveApp.getFilesByName(fileToSearch);
  Logger.log(files);
}

This example can be found here.



来源:https://stackoverflow.com/questions/19722229/google-apps-script-to-search-google-drive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!