Skip to content

Instantly share code, notes, and snippets.

@erikgregorywebb
Created April 28, 2020 17:42
Show Gist options
  • Save erikgregorywebb/0e745087196f04ec81410cc0977e8afc to your computer and use it in GitHub Desktop.
Save erikgregorywebb/0e745087196f04ec81410cc0977e8afc to your computer and use it in GitHub Desktop.
// WordsAPI: https://www.wordsapi.com/
// RapidAPI Documentation: https://rapidapi.com/dpventures/api/wordsapi/endpoints
// Custom Functions: https://developers.google.com/apps-script/guides/sheets/functions
// External APIs: https://developers.google.com/apps-script/guides/services/external
// Tutorial: https://www.benlcollins.com/apps-script/api-tutorial-for-beginners/
function wordsAPI(url) {
var response = UrlFetchApp.fetch(url, {
"method": "GET",
"headers": {
"x-rapidapi-host": "wordsapiv1.p.rapidapi.com",
"x-rapidapi-key": "YOUR-KEY-HERE"
}
});
return response;
}
function getWordDefinition(input) {
if(input.map) {
return input.map(getWordDefinition)
}
else {
// make api call
var url = 'https://wordsapiv1.p.rapidapi.com/words/' + input
var response = wordsAPI(url)
// save contents
var json = response.getContentText();
var data = JSON.parse(json);
// store, return results
try{var definition = data["results"][0]["definition"]} catch (e) {console.error('myFunction() yielded an error: ' + e)};
return definition;
}
}
function getWordSynonym(input) {
if(input.map) {
return input.map(getWordSynonym)
}
else {
// make api call
var url = 'https://wordsapiv1.p.rapidapi.com/words/' + input
var response = wordsAPI(url)
// save contents
var json = response.getContentText();
var data = JSON.parse(json);
// store, return results
try{var synonym = data["results"][0]["synonyms"][0]} catch (e) {console.error('myFunction() yielded an error: ' + e)};
return synonym;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment