如何减少谷歌脚本的搜索时间

时间:2018-05-30 18:20:14

标签: google-apps-script google-sheets google-drive-api full-text-search

我的代码类似于文件搜索引擎,它接收您要查找的关键字,对用户google驱动器中的所有文件进行全文搜索,并通过显示返回文件名,网址源和文件夹它在电子表格中。 代码:

function search(Phrase, FolderID) {
  // Prompt the user for a search term
  var searchTerm = Browser.inputBox("Enter the string to search for:");

  // Get the active spreadsheet and the active sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();

  // Set up the spreadsheet to display the results
  var headers = [["File Name", "Folder", "URL"]];
  sheet.clear();
  sheet.getRange("A4:C4").setValues(headers);

  // Search the files in the user's Google Drive for the search term based on if the word is included in thefile or name
  // Search Reference Guide: https://developers.google.com/apps-script/reference/drive/drive-app#searchFiles(String)
  var files = DriveApp.searchFiles("fullText contains '"+searchTerm.replace("'","\'")+"'");
  //var SearchString = 'fullText contains "' + Phrase + '" and "' + FolderID + '" in parents';
  //var files = DriveApp.searchFiles(SearchString);
  // create an array to store our data to be written to the sheet 
  var output = [];
  // Loop through the results and get the file name, URL, and Folder
  while (files.hasNext()) {
    var file = files.next();

    var name = file.getName();
    //var type = file.getMimeType(); //File type
    var url = file.getUrl();
    var folderNames = "";
    var folders = file.getParents();
    while (folders.hasNext()) {
        var folder = folders.next();
        //Logger.log(folder.getName());
        folderNames += folder.getName() + ", ";
    }
    // push the file details to our output array (essentially pushing a row of data)
    output.push([name, folderNames, url]);
  }
  // write data to the sheet
  sheet.getRange(5, 1, output.length, 3).setValues(output);
}

我绝对是编码/ google脚本的初学者所以我只想提供一些建议(如果有的话),以帮助减少代码完成一次运行并获得结果所需的时间,因为目前我必须等待5-15秒的结果。

1 个答案:

答案 0 :(得分:0)

如何使用Drive API?虽然我不确定此提案是否对您的情况有用,但您可以尝试使用此脚本吗?修改后的脚本如下。使用此脚本时,请在高级Google服务和API控制台中启用Drive API。

在高级Google服务中启用Drive API v2

  • 在脚本编辑器上
    • 资源 - >高级Google服务
    • 启用Drive API v2

Enable Drive API at API console

  • 在脚本编辑器上
    • 资源 - >云平台项目
    • 查看API控制台
    • 在“入门”中,单击“启用API”并获取密钥等凭据。
    • 在左侧,单击“库”。
    • 在搜索API&服务,输入" Drive"。然后点击Drive API。
    • 单击“启用”按钮。
    • 如果API已启用,请不要关闭。

修改后的脚本:

function search(Phrase, FolderID) {
  // Prompt the user for a search term
  var searchTerm = Browser.inputBox("Enter the string to search for:");

  // Get the active spreadsheet and the active sheet
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getActiveSheet();
  sheet.clear();

  // Below script was added.
  var optionalArgs = {
    q: "fullText contains '"+searchTerm.replace("'","\'")+"'",
    fields: "items(alternateLink,parents/id,title)"
  };
  var files = Drive.Files.list(optionalArgs).items; // Retrieve files by search text.
  var res = files.map(function(e){return [e.title, e.parents.map(function(f){return DriveApp.getFolderById(f.id).getName()}).join(", ") , e.alternateLink]});
  var values = [["File Name", "Folder", "URL"]];
  Array.prototype.push.apply(values, res);
  sheet.getRange(4, 1, values.length, 3).setValues(values);
}

参考文献:

如果这对你的情况没用,我很抱歉。