为什么我会收到工作表中函数的错误代码?几分钟前还有效

问题描述 投票:0回答:1

这是我尝试在工作表内使用脚本运行的第一个代码。

无论出于何种原因,我无法使用附加组件,因此我尝试找到一种解决方法,以便能够对工作表使用 GPT。很方便,解决后就复制吧。

出现的错误代码是

“类型错误:无法读取未定义的属性(读取“0”)(第 30 行)。”

第 30 行是这样的:console.log(response.choices[0].message.content);

但我不知道要改变什么,因为它以前有效......

这是代码:

`function GPT(Input) {
 const GPT_API = "INSERT API KEY";
 const BASE_URL = "https://api.openai.com/v1/chat/completions";

 const headers = {
 "Content-Type": "application/json",
 "Authorization": `Bearer ${GPT_API}`
 };

 const options = {
 headers,
 method: "POST",
 muteHttpExceptions: true,
 payload: JSON.stringify({
 "model": "gpt-4",
 "messages": [{
 "role": "system",
 "content": ""
},
{
"role": "user",
"content": Input
}
],
"temperature": 0
})
}
const response = JSON.parse(UrlFetchApp.fetch(BASE_URL, options));
console.log(response.choices[0].message.content);
return response.choices[0].message.content
}`

我希望能够用这个在几分钟内丰富 1000 个列。

一开始它也有效,但第二次我尝试将它应用到很多地方,但同时它就停止了。

在使用此代码之前尝试使用 GPTforSheets 插件,但它们现在对我不起作用。

另一方面,我不是一个很好的编码员,所以没有在那里尝试太多。

google-sheets error-handling scripting
1个回答
0
投票

错误信息表明

choices
中没有
response
属性,或者它不是数组。

要检查响应是否有效并查找其结构,请将最后三行替换为以下内容:

  const response = JSON.parse(UrlFetchApp.fetch(BASE_URL, options));
  const test = JSON.stringify(response);
  console.log(test);
  return test;
© www.soinside.com 2019 - 2024. All rights reserved.