Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.