Ways to reduce search time on google script

2019-08-21 12:56发布

My code is similar to a file search engine, it takes in a keyword you are looking for, does a full text search of all the files in the users google drive and returns the files name, url source, and folder by displaying it in a spreadsheet. Code:

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

I am definitely a beginner in coding/google scripts so I would just like some suggestions (if any) to help reduce the amount of time it takes for the code to finish one run and get the results because currently I have to wait 5-15 seconds for the results.

1条回答
疯言疯语
2楼-- · 2019-08-21 13:51

How about using Drive API? Although I'm not sure whether this proposal is useful for your situation,could you please try this script? The modified script is as follows. When you use this script, please enable Drive API at Advanced Google Services and API console.

Enable Drive API v2 at Advanced Google Services

  • On script editor
    • Resources -> Advanced Google Services
    • Turn on Drive API v2

Enable Drive API at API console

  • On script editor
    • Resources -> Cloud Platform project
    • View API console
    • At Getting started, click Enable APIs and get credentials like keys.
    • At left side, click Library.
    • At Search for APIs & services, input "Drive". And click Drive API.
    • Click Enable button.
    • If API has already been enabled, please don't turn off.

Modified script :

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

References :

If this was not useful for your situation, I'm sorry.

查看更多
登录 后发表回答