Google apps script 得到一个词的定义

Google apps script 得到一个词的定义,google-apps-script,google-sheets,Google Apps Script,Google Sheets,在谷歌表单中 我需要把一个单词的定义输入到它的相邻单元格中。我过去在VBA中使用的公共免费url是url=”https://api.dictionaryapi.dev/api/v1/entries/en/“&wordToDefine。我创建了一个类似于=GetDefinition(C2)的函数 我曾尝试创建应用程序脚本,但不断出现错误,对如何引用函数参数感到困惑。回答: 您可以使用UrlFetchApp调用API 代码: 必须首先使用以下代码在应用程序脚本编辑器中创建自定义函数: functio

在谷歌表单中
我需要把一个单词的定义输入到它的相邻单元格中。我过去在VBA中使用的公共免费url是
url=”https://api.dictionaryapi.dev/api/v1/entries/en/“&wordToDefine
。我创建了一个类似于
=GetDefinition(C2)
的函数

我曾尝试创建应用程序脚本,但不断出现错误,对如何引用函数参数感到困惑。

回答: 您可以使用
UrlFetchApp
调用API

代码: 必须首先使用以下代码在应用程序脚本编辑器中创建自定义函数:

function getDefinition(cell) {
  var url = "https://api.dictionaryapi.dev/api/v1/entries/en/"
  return UrlFetchApp.fetch(url + cell).getContentText();
}
确保运行此操作一次,以授权使用
UrlFetchApp

然后,在表格中,例如,假设单词“Hello”在单元格
B3
中,在
C3
中,您可以使用以下公式:

=getDefinition(B3)
您将收到API的响应,然后可以根据需要进行处理:

"[
    {
        ""word"": ""hello"",
        ""phonetics"": [
            {
                ""text"": ""/həˈloʊ/"",
                ""audio"": ""https://lex-audio.useremarkable.com/mp3/hello_us_1_rr.mp3""
            },
            {
                ""text"": ""/hɛˈloʊ/"",
                ""audio"": ""https://lex-audio.useremarkable.com/mp3/hello_us_2_rr.mp3""
            }
        ],
        ""meaning"": {
            ""exclamation"": [
                {
                    ""definition"": ""Used as a greeting or to begin a phone conversation."",
                    ""example"": ""hello there, Katie!""
                }
            ],
            ""noun"": [
                {
                    ""definition"": ""An utterance of “hello”; a greeting."",
                    ""example"": ""she was getting polite nods and hellos from people"",
                    ""synonyms"": [
                        ""greeting"",
                        ""welcome"",
                        ""salutation"",
                        ""saluting"",
                        ""hailing"",
                        ""address"",
                        ""hello"",
                        ""hallo""
                    ]
                }
            ],
            ""intransitive verb"": [
                {
                    ""definition"": ""Say or shout “hello”; greet someone."",
                    ""example"": ""I pressed the phone button and helloed""
                }
            ]
        }
    }
]"
我希望这对你有帮助

参考资料:

添加您尝试的代码和出现的文本错误。您是否正在尝试创建自定义函数(使用您的Google Apps脚本函数作为公式)?谢谢。这是可行的,你的解释很清楚