Skip to content

Instantly share code, notes, and snippets.

@bennettscience
Last active March 22, 2018 11:46
Show Gist options
  • Select an option

  • Save bennettscience/798cf726cd73eca206e9a45a09693ad1 to your computer and use it in GitHub Desktop.

Select an option

Save bennettscience/798cf726cd73eca206e9a45a09693ad1 to your computer and use it in GitHub Desktop.
Main files for an apps script bot and webapp pair
/**
* getLookup - Get an array of videos matching the search key request
*
* @param {Object[]} keys - Array of search terms
* @returns {Object[]} matches - Array of video objects matched to the tag
*/
function getLookup(keys) {
var sheet = ss.getSheetByName("db");
var data = sheet.getDataRange().getValues();
// Create an array to hold matching results
var matches = [];
// Build the regex
var expr = '^';
for(var s=0; s<keys.length; s++) {
expr += '(?=.*\\b' + keys[s] + '.*\\b)';
}
expr += '.*$'
expr = new RegExp(expr, "gi");
for(var i=0; i<data.length;i++) {
var string = data[i][0].concat(", ", data[i][1]);
// Use string.match instead of expr.test() because the latter advances the index,
// resulting in incomplete results.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
if(string.match(expr)) {
matches.push({"url":data[i][4], "title":data[i][2]})
}
}
// process the matches array to delete duplicate URLs
var matches = uniqBy(matches, JSON.stringify);
// Remove any matches that have a blank URL field.
matches = matches.filter(function(a) { return a.url !== "" });
return matches;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment