Skip to content

Instantly share code, notes, and snippets.

@TechMky
Created September 16, 2020 11:33
Show Gist options
  • Save TechMky/5d6e010b261bd070c201cdd363fc27a4 to your computer and use it in GitHub Desktop.
Save TechMky/5d6e010b261bd070c201cdd363fc27a4 to your computer and use it in GitHub Desktop.
Get word meaning in Google Sheets
/**
* Get the word meaning of a word.
*
* @param {String} word for which the meaning needs to fetched.
* @return {String} meaning of the word
*/
function WORDMEANING(word) {
word = encodeURI(word);
var options = { muteHttpExceptions: true }
try{
const key = "YOUR_API_KEY" //auth key obtained from sources like Merriam Webster
const endpoint = "https://www.dictionaryapi.com/api/v3/references/collegiate/json/" //API end point URL
const url = endpoint + word + "?key=" + key //create the actual url for the 3rd Party API endpoint
const response = UrlFetchApp.fetch( url, options); //make request
Logger.log(response); //log it just in case
//parse the response and get the word meaning out from the response accordingly based on the 3rd party format
// I am using Merriam Webster so, I have extracted the result accordingly
const w = JSON.parse(response.getContentText());
Logger.log(w);
const {shortdef} = w[0]
const result = shortdef.map(def => `"${def}"\n` ).join(' ')
Logger.log(result)
//retrun the final result string
return result
}catch(err){
// incase any exception occurs check the console
Logger.log(err);
return "Some Error Occurred. Check AppScript Executions"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment