Advertisement
RemcoE33

Dictionary

Sep 18th, 2021
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2. * Returns definition, example and synonym.
  3. *
  4. * @param {A1:A5} range - Input range.
  5. * @param {true} synonyms - Add synonyms.
  6. * @return {array} range definitions, examples and synonyms.
  7. * @customfunction
  8. */
  9. function DICTIONARY(words, synonyms){
  10.   if(!Array.isArray(words)){
  11.     words = [[words]]
  12.   }
  13.   const output = [];
  14.   const urls = words.flat().map(word => `https://api.dictionaryapi.dev/api/v2/entries/en/${word}`)
  15.  
  16.   const response = UrlFetchApp.fetchAll(urls);
  17.   response.forEach(url => {
  18.     const data = JSON.parse(url.getContentText());
  19.     const definition = data[0].meanings[0].definitions[0].definition
  20.     const example = data[0].meanings[0].definitions[0].example
  21.     if (synonyms){
  22.       const synonym = data[0].meanings[0].definitions[0].synonyms.join(', ')
  23.       output.push([definition, example, synonym])
  24.     } else {
  25.     output.push([definition, example])
  26.     }
  27.   })
  28.  
  29.   return output;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement