Last active
March 26, 2018 12:09
-
-
Save consideRatio/4275bb5badbca4bdddbb2bdf24dc6d6a to your computer and use it in GitHub Desktop.
API request example from google sheets
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
// Use this function from a cell by writing for example getSynonyms("happy") | |
// TODO: Make the function return the desired amount of available synonyms. | |
// It is currently being limited, always returning one single synonym. | |
// Try returning an array of words! | |
function getSynonyms(word) { | |
try { | |
var response = UrlFetchApp.fetch("https://api.datamuse.com/words?rel_syn=" + word); | |
var data = JSON.parse(response.getContentText()); | |
} | |
catch (error) { | |
return "Synonym API failure..."; | |
} | |
// Press Ctrl+Enter to view log after running code. | |
var synonyms = data.length; | |
Logger.log(synonyms); | |
Logger.log(data); | |
if (synonyms >= 1) | |
{ | |
return data[0].word; | |
} | |
else | |
{ | |
return "No responses..."; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment