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.
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
Enable Drive API at API console
Modified script :
References :
If this was not useful for your situation, I'm sorry.