Last active
March 22, 2018 11:46
-
-
Save bennettscience/798cf726cd73eca206e9a45a09693ad1 to your computer and use it in GitHub Desktop.
Main files for an apps script bot and webapp pair
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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