Building custom Google Sheets functions to fetch word data from dictionary web service

I’m working on a vocabulary spreadsheet in Google Sheets and need to build custom functions that pull word meanings, related terms, and opposite terms from a dictionary web service.

I have these scripts in my Apps Script project but when I try to use them in my sheet cells, I keep getting an error that says “fetch is not defined”. Can someone help me fix this issue?

Here’s my main function:

/**
 * Executes JavaScript code
 * @param {string} script The script to run
 * @return Result of the executed script
 * @customfunction
 */
function EXECUTEJS(script) {
  return eval(script);
}

For word meanings:

const SECRET_KEY = 'my-api-key';
const TARGET_WORD = 'happy';

fetch(`https://api.dictionary.com/v2/entries/english/${TARGET_WORD}?key=${SECRET_KEY}`)
  .then(result => result.json())
  .then(info => {
    const meaning = info[0].definitions[0];
    console.log(meaning);
  })
  .catch(err => console.log(err));

For similar words:

const SECRET_KEY = 'my-api-key';
const TARGET_WORD = 'run';

fetch(`https://api.dictionary.com/v2/entries/english/${TARGET_WORD}?key=${SECRET_KEY}&related`)
  .then(result => result.json())
  .then(info => {
    const similarWords = info[0].related.similar.join(', ');
    console.log(`Similar: ${similarWords}`);
  })
  .catch(err => console.log(err));

For opposite words:

const SECRET_KEY = 'my-api-key';
const TARGET_WORD = 'hot';

fetch(`https://api.dictionary.com/v2/entries/english/${TARGET_WORD}?key=${SECRET_KEY}&opposites`)
  .then(result => result.json())
  .then(info => {
    const oppositeWords = info[0].related.opposites.join(', ');
    console.log(`Opposites: ${oppositeWords}`);
  })
  .catch(err => console.log(err));

The problem is Google Apps Script runs server-side, not in browsers, so regular JavaScript APIs like fetch don’t work. I hit this same issue building a vocab tool last year. You’ll need to swap all those fetch calls for UrlFetchApp.fetch() and rewrite your code to handle synchronous responses instead of promises. That EXECUTEJS function with eval() won’t work either - Apps Script blocks dynamic code execution for security. Create specific custom functions like =GETMEANING(word) that call your dictionary API directly with UrlFetchApp. Also, add proper error handling since API calls fail, and validate results because dictionary APIs sometimes return weird data structures. Learned that one the hard way.

yeah, apps script doesn’t support fetch - you need to use UrlFetchApp.fetch() instead. that EXECUTEJS approach is risky too. better to create separate functions for each dictionary request and call UrlFetchApp directly.

You’re mixing browser JavaScript with Google Apps Script - that won’t work. Apps Script doesn’t have the fetch API, so you need to use UrlFetchApp.fetch() instead and handle responses synchronously. That eval() function in EXECUTEJS is also a problem since Apps Script sandboxes everything for security. I’d make dedicated custom functions like =GETDEFINITION(word) that use UrlFetchApp for API calls. Just remember custom functions have time limits, so add some basic caching to avoid hitting the API repeatedly for the same words.