Skip to content

Instantly share code, notes, and snippets.

@nkthiebaut
Created December 1, 2020 19:10
Show Gist options
  • Save nkthiebaut/a42b077c4f5b472997bd1055f7b76c49 to your computer and use it in GitHub Desktop.
Save nkthiebaut/a42b077c4f5b472997bd1055f7b76c49 to your computer and use it in GitHub Desktop.
Fetch the definition of a word when added to a Google Spreadsheet
function onNewRow(e) {
var sheet = SpreadsheetApp.getActiveSheet();
Logger.log("Trigger: new row inserted:" + sheet.getLastRow());
// Get index of row inserted
var row = sheet.getLastRow();
// Get word/phrase inserted
var range = sheet.getRange(row, 1);
var phrase = range.getValue();
Logger.log("Row insertion detected");
if (sheet.getName() == "Expressions") {
addDate(sheet, row, 3);
}
else if (alreadyExists(sheet, phrase, row)) {
sheet.deleteRow(row);
Logger.log(phrase + " already exists!");
} else {
// fillDictionaryLink(sheet, row, phrase);
range.setValue(phrase.charAt(0).toUpperCase() + phrase.slice(1));
addDefinition(sheet, row, phrase, 2);
addDate(sheet, row, 3);
// Sort words in ascending chronological order
sheet.sort(3);
Logger.log(phrase + " added!");
}
}
function addDate(sheet, row, dateColumn) {
sheet.getRange(row, dateColumn).setValue(new Date()).setNumberFormat("MM/dd/YY");
}
function alreadyExists(sheet, word, latestRow) {
var lastRowIndex = sheet.getLastRow();
var words = sheet.getRange(2,1,lastRowIndex).getValues();
var wordsFlat = words.map(function(row) {
return row[0];
});
indexOf = wordsFlat.indexOf(word) + 2;
Logger.log(latestRow + ", " + indexOf);
if (indexOf != -1 && indexOf != latestRow) {
return true;
}
return false;
}
function addDefinition(sheet, row, phrase, column) {
var example_column = column + 1;
var base_url = 'https://www.dictionaryapi.com/api/v3/references/learners/json/';
// To lowercase and substitute whitespace for '_'
phrase.toLowerCase();
phrase = phrase.replace(/\s/g, '_');
// Encode word/phrase
phrase = encodeURI(phrase);
// Append phrase to base URL
var url = (base_url.concat(phrase)).concat('?key=097dfbf9-96ba-49f5-b81d-704ac2a51861');
// Make a GET request to Dictionary API and retrieve the definition/s.
var options = {
"method": "GET",
};
var response = UrlFetchApp.fetch(url, options);
var responseObj = JSON.parse(response.getContentText());
try {
var definition = responseObj[0]["shortdef"][0];
// Add an example if available
try {
var example = responseObj[0]["def"][0]["sseq"][0][0][1]["dt"][1][1][0]["t"];
example = example.replace("{it}", "").replace("{/it}", "");
definition = definition + "\n\n" + "Example: " + example
}
catch(err) {
Logger.log("No example found for: " + phrase);
}
// Insert definition
sheet.getRange(row, column).setValue(definition);
}
catch(err) {
Logger.log("No definition found for: " + phrase);
}
try {
var example = responseObj[0]["def"][0]["sseq"][0][0][1]["dt"][1][1][0]["t"];
example = example.replace("{it}", "").replace("{/it}", "");
// Insert example
sheet.getRange(row, example_column).setValue(example);
}
catch(err) {
Logger.log("No example found for: " + phrase);
}
}
function fillDictionaryLink(sheet, row, phrase) {
var base_url = 'https://www.merriam-webster.com/dictionary/';
//Substitute whitespace for '-'
phrase = phrase.replace(/\s/g, '-');
// Append phrase to base URL
var link = base_url.concat(phrase);
// Insert link
sheet.getRange(row, 4).setValue(link);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment