You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Google Sheets中通过OpenAI调用Bing实现互联网访问?

GPT-4/ChatGPT联网能力及Google Sheets集成问题解答

核心结论

  • OpenAI原生GPT-4/ChatGPT(包括API服务)不具备直接联网或调用Bing获取实时数据的能力。Bing的联网功能是微软Copilot(原Bing Chat)专属的集成特性,和OpenAI原生服务无关。
  • 在Google Sheets中通过OpenAI API调用模型时提示“无法访问互联网”是普遍的预期行为,不属于操作错误——因为OpenAI模型本身没有联网权限,无法主动发起网络请求。

可行实现方案

如果需要在Google Sheets中实现「AI+实时网络数据」的功能,需要自行搭建“搜索引擎API获取数据→传入OpenAI模型生成回答”的中间逻辑,具体步骤如下:

1. 准备必要的API密钥

  • 申请Bing Search API密钥(或其他搜索引擎API,如Google Custom Search API)
  • 确保你拥有有效的OpenAI API密钥

2. 编写Google Apps Script代码

通过Google Apps Script的UrlFetchApp工具,先调用搜索引擎API获取实时数据,再将数据作为上下文传入OpenAI API。示例代码如下:

function getAIResponseWithWebData(userQuery) {
  // 配置API密钥(建议用PropertiesService存储,避免硬编码)
  const props = PropertiesService.getScriptProperties();
  const bingApiKey = props.getProperty("BING_API_KEY");
  const openaiApiKey = props.getProperty("OPENAI_API_KEY");

  // 1. 调用Bing Search API获取网络搜索结果
  const searchUrl = `https://api.bing.microsoft.com/v7.0/search?q=${encodeURIComponent(userQuery)}`;
  const searchOptions = {
    headers: { "Ocp-Apim-Subscription-Key": bingApiKey },
    muteHttpExceptions: true
  };
  const searchResponse = UrlFetchApp.fetch(searchUrl, searchOptions);
  if (searchResponse.getResponseCode() !== 200) {
    return "网络搜索请求失败:" + searchResponse.getContentText();
  }
  const searchData = JSON.parse(searchResponse.getContentText());
  const webSnippets = searchData.webPages?.value?.map(item => `${item.name}\n${item.snippet}`).join("\n---\n") || "未找到相关网络数据";

  // 2. 将搜索结果传入OpenAI API生成回答
  const openaiUrl = "https://api.openai.com/v1/chat/completions";
  const openaiOptions = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      "Authorization": `Bearer ${openaiApiKey}`
    },
    payload: JSON.stringify({
      model: "gpt-4",
      messages: [
        { role: "system", content: "请基于以下网络搜索结果回答用户问题:\n" + webSnippets },
        { role: "user", content: userQuery }
      ]
    }),
    muteHttpExceptions: true
  };
  const openaiResponse = UrlFetchApp.fetch(openaiUrl, openaiOptions);
  if (openaiResponse.getResponseCode() !== 200) {
    return "AI请求失败:" + openaiResponse.getContentText();
  }
  const openaiData = JSON.parse(openaiResponse.getContentText());
  return openaiData.choices[0].message.content.trim();
}

3. 配置与使用

  • 将API密钥存入Google Apps Script的脚本属性:依次点击「项目设置」→「脚本属性」→「添加属性」,分别添加BING_API_KEY和OPENAI_API_KEY
  • 在Google Sheets中自定义函数,直接调用上述脚本:比如在单元格中输入=getAIResponseWithWebData("2024年最新的AI发展趋势")即可获取结果

注意事项

  • 确保Google Apps Script的UrlFetchApp服务已启用(默认启用,若报错可在「编辑器」→「服务」中检查)
  • 注意API配额限制,避免超出搜索引擎和OpenAI的调用额度
  • 处理异常情况,比如网络请求超时、API密钥无效等

内容的提问来源于stack exchange,提问作者Evgeniy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 05:40:18