8
n8n 中文网amn8n.com

使用GPT-4o将自然语言转换为Google Sheets SQL查询

中级

这是一个Document Extraction, Multimodal AI领域的自动化工作流,包含 12 个节点。主要使用 HttpRequest, Agent, GoogleSheetsTool, ChatTrigger, LmChatOpenAi 等节点。 使用GPT-4o将自然语言转换为Google Sheets SQL查询

前置要求
  • 可能需要目标 API 的认证凭证
  • Google Sheets API 凭证
  • OpenAI API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "meta": {
    "instanceId": "ad0113c344ee237399e44e9f11798b05baeb83a6196d514a9ae9d2ad71c3b5c9",
    "templateCredsSetupCompleted": true
  },
  "nodes": [
    {
      "id": "5a4c2536-9070-40b4-9db1-24e3b6f440fa",
      "name": "AI Agent3",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        224,
        1872
      ],
      "parameters": {
        "options": {
          "systemMessage": "# MVP System Prompt — Google Sheets Ask-Data\n\nRule, you must always use the column tool for every question and followup question to know the column names and column letters. \n\nwrite a sql query to send to google sheets based on the location of the google column on the google sheet. The output should look something like this. \n\nmust include google sheets entire url before the query. do not truncate. \"https://docs.google.com/spreadsheets/d/19aUQYZq02qHsCelO4eeV4sx_MTJJupC5qe0gDLQBtRA/edit?gid=365710158#gid=365710158\" \n\nexample: \nhttps://docs.google.com/spreadsheets/d/19aUQYZq02qHsCelO4eeV4sx_MTJJupC5qe0gDLQBtRA/gviz/tq?tqx=out:csv&gid=365710158&tq=SELECT E, SUM(H), SUM(F) GROUP BY E\n\nYou can only use column letters, no names\nouptut data like this. \n\n{\n\t\"query\": \"https://docs.google.com/spreadsheets/d/19aUQYZq02qHsCelO4eeV4sx_MTJJupC5qe0gDLQBtRA/gviz/tq?tqx=out:csv&gid=365710158&tq=SELECT E, SUM(H), SUM(F) GROUP BY E\"\n}"
        },
        "hasOutputParser": true
      },
      "typeVersion": 2.2
    },
    {
      "id": "766b4958-6e0b-4d99-99ce-8fe98a44b019",
      "name": "获取列信息2",
      "type": "n8n-nodes-base.googleSheetsTool",
      "position": [
        368,
        2160
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 467321788,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/19aUQYZq02qHsCelO4eeV4sx_MTJJupC5qe0gDLQBtRA/edit#gid=467321788",
          "cachedResultName": "Columns"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "19aUQYZq02qHsCelO4eeV4sx_MTJJupC5qe0gDLQBtRA",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/19aUQYZq02qHsCelO4eeV4sx_MTJJupC5qe0gDLQBtRA/edit?usp=drivesdk",
          "cachedResultName": "Sample Marketing Data"
        },
        "descriptionType": "manual",
        "toolDescription": "Extracts a sheet’s column metadata so downstream steps (querying, validation, docs) can rely on a clean, typed schema. It returns each column’s name, inferred data type, and human-readable description, plus helpful profiling hints."
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "HlBW2puZbuCCq8jJ",
          "name": "Google Sheets account 3"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "96e7c115-ecfa-40b1-90ac-96760588a56b",
      "name": "简单记忆2",
      "type": "@n8n/n8n-nodes-langchain.memoryBufferWindow",
      "position": [
        192,
        2144
      ],
      "parameters": {},
      "typeVersion": 1.3
    },
    {
      "id": "e3fec4d3-f499-4a6e-ac00-70d2f784762c",
      "name": "OpenAI 聊天模型1",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        16,
        2128
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4o",
          "cachedResultName": "gpt-4o"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "4l6TDfLZVFS24g3X",
          "name": "OpenAi account 4"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "09aab886-f1fd-447f-aded-70242aa27821",
      "name": "便签5",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -352,
        1440
      ],
      "parameters": {
        "color": 5,
        "width": 1584,
        "height": 1008,
        "content": "## 📬 需要帮助或想要自定义此工作流?"
      },
      "typeVersion": 1
    },
    {
      "id": "94911852-7bba-4178-a4bd-1461bf08d3db",
      "name": "结构化输出解析器 1",
      "type": "@n8n/n8n-nodes-langchain.outputParserStructured",
      "position": [
        528,
        2160
      ],
      "parameters": {
        "jsonSchemaExample": "{\n\t\"query\": \"sql query for google sheets\"\n}"
      },
      "typeVersion": 1.3
    },
    {
      "id": "262e5d0d-c616-4332-a0ab-f1bc9d7225ef",
      "name": "OpenAI 聊天模型2",
      "type": "@n8n/n8n-nodes-langchain.lmChatOpenAi",
      "position": [
        880,
        1840
      ],
      "parameters": {
        "model": {
          "__rl": true,
          "mode": "list",
          "value": "gpt-4.1-mini"
        },
        "options": {}
      },
      "credentials": {
        "openAiApi": {
          "id": "4l6TDfLZVFS24g3X",
          "name": "OpenAi account 4"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "734f8a72-054b-4d04-bd9b-dc85ba160683",
      "name": "当收到聊天消息时",
      "type": "@n8n/n8n-nodes-langchain.chatTrigger",
      "position": [
        -160,
        1744
      ],
      "webhookId": "edca0f0a-77c3-43e5-8ece-e514a29446f5",
      "parameters": {
        "options": {}
      },
      "typeVersion": 1.3
    },
    {
      "id": "be5f8fc5-0d06-4acf-8fa8-efca6310f0ec",
      "name": "从 Google Sheets 获取数据",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        640,
        1888
      ],
      "parameters": {
        "url": "={{ $json.output.query }}",
        "options": {},
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {}
          ]
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "71029e6d-3933-49a0-b31b-1461ef3a176f",
      "name": "写入表格输出1",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        864,
        1616
      ],
      "parameters": {
        "text": "={{ $json.data }}",
        "options": {
          "systemMessage": "write this into one table. output as dimensions, then metrics"
        },
        "promptType": "define"
      },
      "typeVersion": 2.2
    },
    {
      "id": "43d7c1a8-0fc0-4877-ab9c-908c3836239a",
      "name": "便签 12",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1920,
        1440
      ],
      "parameters": {
        "width": 752,
        "height": 1008,
        "content": "## 分步设置说明"
      },
      "typeVersion": 1
    },
    {
      "id": "0081f73b-ae42-424d-8cd1-9f9e0b30c64f",
      "name": "便签13",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1136,
        1440
      ],
      "parameters": {
        "width": 752,
        "height": 1008,
        "content": ""
      },
      "typeVersion": 1
    }
  ],
  "pinData": {},
  "connections": {
    "AI Agent3": {
      "main": [
        [
          {
            "node": "Get Data from Google Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Simple Memory2": {
      "ai_memory": [
        [
          {
            "node": "AI Agent3",
            "type": "ai_memory",
            "index": 0
          }
        ]
      ]
    },
    "Get Column Info2": {
      "ai_tool": [
        [
          {
            "node": "AI Agent3",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model1": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent3",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "OpenAI Chat Model2": {
      "ai_languageModel": [
        [
          {
            "node": "Write into Table Output1",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Structured Output Parser1": {
      "ai_outputParser": [
        [
          {
            "node": "AI Agent3",
            "type": "ai_outputParser",
            "index": 0
          }
        ]
      ]
    },
    "Get Data from Google Sheet": {
      "main": [
        [
          {
            "node": "Write into Table Output1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When chat message received": {
      "main": [
        [
          {
            "node": "AI Agent3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

复制上方的 JSON 配置代码,在您的 n8n 实例中创建新工作流并选择「从 JSON 导入」,粘贴配置后根据需要修改凭证设置即可。

这个工作流适合什么场景?

中级 - 文档提取, 多模态 AI

需要付费吗?

本工作流完全免费,您可以直接导入使用。但请注意,工作流中使用的第三方服务(如 OpenAI API)可能需要您自行付费。

工作流信息
难度等级
中级
节点数量12
分类2
节点类型8
难度说明

适合有一定经验的用户,包含 6-15 个节点的中等复杂度工作流

作者
Robert Breen

Robert Breen

@rbreen

Professional services consultant with over 10 years of experience solving complex business problems across industries. I specialize in n8n and process automation—designing custom workflows that integrate tools like Google Calendar, Airtable, GPT, and internal systems. Whether you need to automate scheduling, sync data, or streamline operations, I build solutions that save time and drive results.

外部链接
在 n8n.io 查看

分享此工作流