Google Sheets调用韦氏词典API出现fetch未定义引用错误
Google Sheets调用韦氏词典API报错:fetch is not defined 解决方法
问题描述
我有一个存储词汇列表的Google Sheet,尝试编写三个函数从Merriam-Webster API获取单词的定义、同义词和反义词。项目编辑器中有五个脚本——一个ImportJSON脚本以及以下四个脚本。但在Google Sheet中输入=runjs(shortdef)、=runjs(synonyms)或=runjs(antonyms)时,出现错误:
ReferenceError: fetch is not defined (line 4)
原代码如下:
RUNJS 函数
/** * Evaluates JS code. * * @param {code} code The code to evaluate. * @return The result from evaluating the code. * @customfunction */ function RUNJS(code) { return eval(code); }
获取定义的代码
const API_KEY = 'mykeyhere'; const WORD = 'dance'; fetch(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${WORD}?key=${API_KEY}`) .then(response => response.json()) .then(data => { const definition = data[0].shortdef[0]; console.log(definition); }) .catch(error => console.log(error));
获取同义词的代码
const API_KEY = 'mykeyhere'; const WORD = 'set'; fetch(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${WORD}?key=${API_KEY}&synonyms`) .then(response => response.json()) .then(data => { const synonyms = data[0].meta.syns[0].join(', '); console.log(`Synonyms: ${synonyms}`); }) .catch(error => console.log(error));
获取反义词的代码
const API_KEY = 'mykeyhere'; const WORD = 'against'; fetch(`https://www.dictionaryapi.com/api/v3/references/collegiate/json/${WORD}?key=${API_KEY}&antonyms`) .then(response => response.json()) .then(data => { const antonyms = data[0].meta.ants[0].join(', '); console.log(`Antonyms: ${antonyms}`); }) .catch(error => console.log(error));
错误原因
Google Apps Script不支持浏览器端的fetch API,必须使用其内置的UrlFetchApp服务发起HTTP请求。另外,通过RUNJS+eval的方式执行代码既不安全,也不符合Google Apps Script自定义函数的设计规范。
修正方案
直接编写专用的自定义函数,替换原有的实现方式:
1. 统一配置API密钥
const API_KEY = '你的实际API密钥'; // 替换为你的Merriam-Webster API密钥
2. 获取单词定义的函数
/** * 获取单词的韦氏词典定义 * @param {string} word 要查询的单词 * @return {string} 单词的首个定义 * @customfunction */ function GET_DEFINITION(word) { const encodedWord = encodeURIComponent(word); const url = `https://www.dictionaryapi.com/api/v3/references/collegiate/json/${encodedWord}?key=${API_KEY}`; const response = UrlFetchApp.fetch(url); const data = JSON.parse(response.getContentText()); // 处理无结果或数据格式异常的情况 if (!data.length || !data[0].shortdef) { return '未找到对应定义'; } return data[0].shortdef[0]; }
3. 获取单词同义词的函数
/** * 获取单词的韦氏词典同义词 * @param {string} word 要查询的单词 * @return {string} 逗号分隔的同义词列表 * @customfunction */ function GET_SYNONYMS(word) { const encodedWord = encodeURIComponent(word); const url = `https://www.dictionaryapi.com/api/v3/references/collegiate/json/${encodedWord}?key=${API_KEY}&synonyms`; const response = UrlFetchApp.fetch(url); const data = JSON.parse(response.getContentText()); // 处理无结果或数据格式异常的情况 if (!data.length || !data[0].meta?.syns?.[0]) { return '未找到对应同义词'; } return data[0].meta.syns[0].join(', '); }
4. 获取单词反义词的函数
/** * 获取单词的韦氏词典反义词 * @param {string} word 要查询的单词 * @return {string} 逗号分隔的反义词列表 * @customfunction */ function GET_ANTONYMS(word) { const encodedWord = encodeURIComponent(word); const url = `https://www.dictionaryapi.com/api/v3/references/collegiate/json/${encodedWord}?key=${API_KEY}&antonyms`; const response = UrlFetchApp.fetch(url); const data = JSON.parse(response.getContentText()); // 处理无结果或数据格式异常的情况 if (!data.length || !data[0].meta?.ants?.[0]) { return '未找到对应反义词'; } return data[0].meta.ants[0].join(', '); }
使用方法
在Google Sheet的单元格中直接调用函数,例如:
- 获取A2单元格单词的定义:
=GET_DEFINITION(A2) - 获取A2单元格单词的同义词:
=GET_SYNONYMS(A2) - 获取A2单元格单词的反义词:
=GET_ANTONYMS(A2)
内容的提问来源于stack exchange,提问作者Saber
相关产品推荐
相关产品推荐

