8
n8n 中文网amn8n.com

电子邮件扫描和Google表格采购订单创建

中级

这是一个Document Extraction, AI Summarization领域的自动化工作流,包含 15 个节点。主要使用 If, Set, Code, Cron, Gmail 等节点。 使用Gemini AI从Gmail提取采购订单并保存至Google表格

前置要求
  • Google 账号和 Gmail API 凭证
  • Google Sheets API 凭证
  • Google Gemini API Key
工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "xfKuFCSndnxYSb8t",
  "meta": {
    "instanceId": "bbc3fa3cd7d64d8ff0c4877d98dee68ce7dadacc5e089546680c915b3e5a212b",
    "templateCredsSetupCompleted": true
  },
  "name": "电子邮件扫描和 Google Sheet 中的采购订单创建",
  "tags": [],
  "nodes": [
    {
      "id": "7fa9ff57-fb2c-4db0-a3ff-4f2fa1d2184f",
      "name": "获取多个消息",
      "type": "n8n-nodes-base.gmail",
      "position": [
        -2608,
        -592
      ],
      "webhookId": "38e977e5-65c8-4b40-b201-bbe71fea8aea",
      "parameters": {
        "limit": 100,
        "simple": false,
        "filters": {
          "readStatus": "unread",
          "receivedAfter": "={{ $today.minus({ days: 1 }).toISODate() }}"
        },
        "options": {
          "downloadAttachments": true
        },
        "operation": "getAll"
      },
      "typeVersion": 2.1
    },
    {
      "id": "e87d2e42-8bd6-473b-87ed-aeb6ec643e58",
      "name": "Cron",
      "type": "n8n-nodes-base.cron",
      "position": [
        -2784,
        -592
      ],
      "parameters": {
        "triggerTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "typeVersion": 1
    },
    {
      "id": "aad75605-ee41-4fc8-8e61-5e5932a30d67",
      "name": "如果",
      "type": "n8n-nodes-base.if",
      "position": [
        -2112,
        -592
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "60e07c88-8125-4a64-8212-14935fc3d73a",
              "operator": {
                "type": "object",
                "operation": "exists",
                "singleValue": true
              },
              "leftValue": "={{ $('Filter emails').item.binary}}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "f1d9876b-8e9d-4824-9650-a3641f294532",
      "name": "AI 代理",
      "type": "@n8n/n8n-nodes-langchain.agent",
      "position": [
        -1648,
        -576
      ],
      "parameters": {
        "text": "=# Role  \nYou are an Expert AI Agent specialized in reading emails, extracting purchase order details, enriching them with product information using the Google Sheet tool, and normalizing dates into calendar weeks (Kalenderwoche).  \n\n# Task  \n- Input: {{ $('If').item.json.text }}  \n- Context: The email may contain purchase order details either in:  \n  - A formatted table, or  \n  - A freeform paragraph.  \n- Language: The email text can be in any language. You must understand and process it.  \n\n# Instructions  \n1. Read the email text carefully.  \n2. Identify purchase order details, including:  \n   - Product/package name  \n   - Quantity / number of items  \n   - Any other order-related details (sizes, variants, etc. if available).  \n3. Detect **dates or time expressions** that refer to delivery, booking, or campaign execution.  \n   - If the text contains a direct week reference (e.g., *KW36*), use it directly.  \n   - If the text contains a month or vague time reference (e.g., *end of October*), convert it into the appropriate calendar week(s).  \n     - *“Start of X month”* → first calendar week of that month.  \n     - *“Mid of X month”* → middle calendar week of that month.  \n     - *“End of X month”* → last calendar week of that month.  \n   - Use ISO week numbering (Monday as first day of the week).  \n4. Populate both `Kalenderwoche Start` and `Kalenderwoche Ende`.  \n   - If only one week is identified, set both Start and End to the same week.  \n   - If a date range is mentioned, map Start and End accordingly.  \n5. Query the **Google Sheet tool** to fetch full product details (e.g., product code, price, description, stock availability).  \n6. Merge the extracted order information with the Google Sheet product details.  \n7. **Return only the final `items` array as JSON. Do not include order_id, customer, or notes.**  \n8. Ensure all keys are translated into the email’s language or standardized consistently.  \n\n# Output Format  \nReturn **only this JSON array**:  \n```json\n[\n  { \"Laufende Nummer\":\"<string>\",\n    \"Lieferant\":\"<string>\",\n    \"Lieferanten-Nr\": \"<number or null>\",\n    \"Marke\": \"<string or null>\",\n    \"Marken-Nr\": \"<number or null>\",\n    \"Kalenderwoche Start\": \"<string, e.g., KW36>\",\n    \"Kalenderwoche Ende\": \"<string, e.g., KW36>\",\n    \"Marketing Status\": \"<string or null>\",\n    \"Paket\": \"<string>\",\n    \"Produkt\": \"<string>\",\n    \"Länder-Aktivierung\": \"<string or null>\",\n    \"Kosten\": \"<number or null>\",\n    \"quantity\": \"<number>\"\n  }\n]\n",
        "options": {},
        "promptType": "define"
      },
      "typeVersion": 2.2
    },
    {
      "id": "68af1754-d9c2-4ab3-b3e6-a72deaea11df",
      "name": "Google Gemini 聊天模型",
      "type": "@n8n/n8n-nodes-langchain.lmChatGoogleGemini",
      "position": [
        -1664,
        -384
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 1
    },
    {
      "id": "b3adb22f-622c-48c1-83eb-2d3f4551251e",
      "name": "在 Google Sheets 中获取行",
      "type": "n8n-nodes-base.googleSheetsTool",
      "position": [
        -1488,
        -368
      ],
      "parameters": {
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "",
          "cachedResultUrl": "",
          "cachedResultName": ""
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "="
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "a6ef8668-1b13-4f96-820d-1dd5d25b693f",
      "name": "在表格中追加行",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        -1088,
        -576
      ],
      "parameters": {
        "columns": {
          "value": {
            "Marke": "={{ $json.Marke }}",
            "Paket": "={{ $json.Paket }}",
            "Kosten": "={{ $json.Kosten }}",
            "Produkt": "={{ $json.Produkt }}",
            "Lieferant": "={{ $json.Lieferant }}",
            "Marken-Nr": "={{ $json[\"Marken-Nr\"] }}",
            "Lieferanten-Nr": "={{ $json[\"Lieferanten-Nr\"] }}",
            "Laufende Nummer": "={{ $json[\"Laufende Nummer\"] }}",
            "Marketing Status": "={{ $json[\"Marketing Status\"] }}",
            "Kalenderwoch Start": "={{ $json[\"Kalenderwoche Start\"] }}",
            "Kalenderwoche Ende": "={{ $json[\"Kalenderwoche Ende\"] }}",
            "Länder-Aktivierung": "={{ $json[\"Länder-Aktivierung\"] }}"
          },
          "schema": [
            {
              "id": "Laufende Nummer",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Laufende Nummer",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Lieferant",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Lieferant",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Lieferanten-Nr",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Lieferanten-Nr",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Marke",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Marke",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Marken-Nr",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Marken-Nr",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Kalenderwoch Start",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Kalenderwoch Start",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Kalenderwoche Ende",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "Kalenderwoche Ende",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Marketing Status",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Marketing Status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Paket",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Paket",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Produkt",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Produkt",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Länder-Aktivierung",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Länder-Aktivierung",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "Kosten",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "Kosten",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "append",
        "sheetName": {
          "__rl": true,
          "mode": "id",
          "value": "="
        },
        "documentId": {
          "__rl": true,
          "mode": "id",
          "value": "="
        }
      },
      "credentials": {
        "googleSheetsOAuth2Api": {
          "id": "Hqjs4o2PKY8T8cY1",
          "name": "Google Sheets account"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "90d90230-7fea-431e-af80-e22753b41856",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "disabled": true,
      "position": [
        -2800,
        -704
      ],
      "parameters": {
        "width": 528,
        "height": 320,
        "content": "## 每分钟扫描电子邮件并读取新邮件"
      },
      "typeVersion": 1
    },
    {
      "id": "de0dcfdd-bb42-4170-8506-d2fa8a5846cd",
      "name": "便签1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -2144,
        -736
      ],
      "parameters": {
        "width": 400,
        "height": 368,
        "content": "## 检查文档是否有附件"
      },
      "typeVersion": 1
    },
    {
      "id": "24b56bad-9b5a-4710-9cc8-8fdcb07bd864",
      "name": "设置最终输出键",
      "type": "n8n-nodes-base.set",
      "position": [
        -1888,
        -576
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "a05c046c-6afb-421d-8b9e-128d0960d006",
              "name": "final_json_keys",
              "type": "array",
              "value": "=[\n  \"Laufende Nummer\",\n  \"Lieferant\",\n  \"Lieferanten-Nr\",\n  \"Marke\",\n  \"Marken-Nr\",\n  \"Kalenderwoche\\nStart\",\n  \"Kalenderwoche\\nEnde\",\n  \"Marketing Status\",\n  \"Paket\",\n  \"Produkt\",\n  \"Länder-Aktivierung\",\n  \"Kosten\"\n]\n"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "24616df2-e7e4-4105-830f-8bc095f818f7",
      "name": "筛选邮件",
      "type": "n8n-nodes-base.filter",
      "position": [
        -2416,
        -592
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "or",
          "conditions": [
            {
              "id": "6b9ca560-7c08-40e0-9d9b-8a916ff1368c",
              "operator": {
                "type": "string",
                "operation": "contains"
              },
              "leftValue": "={{ $json.subject }}",
              "rightValue": "=Marketing"
            },
            {
              "id": "518e65fe-c130-4a27-821b-20f6c51b2dd7",
              "operator": {
                "type": "string",
                "operation": "contains"
              },
              "leftValue": "={{ $json.subject }}",
              "rightValue": "Buchungsanfrage"
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "0c0cb36a-2ff1-4b53-8830-90d000ddbe51",
      "name": "重新格式化以上传到 Google Sheet",
      "type": "n8n-nodes-base.code",
      "position": [
        -1264,
        -576
      ],
      "parameters": {
        "jsCode": "// Input from previous node\nconst input = items[0].json.output;\n\n// Remove markdown ```json ... ``` wrappers if present\nconst cleaned = input.replace(/```json|```/g, '').trim();\n\nlet parsed;\ntry {\n  parsed = JSON.parse(cleaned);\n} catch (error) {\n  throw new Error(`Failed to parse JSON: ${error.message}\\nRaw input: ${cleaned}`);\n}\n\n// Return each object as separate item in n8n\nreturn parsed.map(obj => ({ json: obj }));\n"
      },
      "typeVersion": 2
    },
    {
      "id": "6867e395-dcc0-491d-b406-c914418d30b0",
      "name": "便签2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1712,
        -672
      ],
      "parameters": {
        "width": 352,
        "height": 464,
        "content": "## AI 智能体读取并汇总订单"
      },
      "typeVersion": 1
    },
    {
      "id": "4c38683c-1398-432e-b780-ad7d51a0d238",
      "name": "便签3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1312,
        -688
      ],
      "parameters": {
        "width": 352,
        "height": 304,
        "content": "## 将采购订单追加到 Google Sheet"
      },
      "typeVersion": 1
    },
    {
      "id": "06417501-207f-45f9-8239-3676f14f9457",
      "name": "便签4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -3152,
        -1280
      ],
      "parameters": {
        "width": 480,
        "height": 544,
        "content": "## 📧 电子邮件读取与采购订单创建(AI 驱动)"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "6ef223ac-0d4b-424d-b174-f3072229fbe6",
  "connections": {
    "If": {
      "main": [
        [],
        [
          {
            "node": "Set final output keys",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Cron": {
      "main": [
        [
          {
            "node": "Get many messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "AI Agent": {
      "main": [
        [
          {
            "node": "Reformatted to upload in Google sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter emails": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get many messages": {
      "main": [
        [
          {
            "node": "Filter emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Set final output keys": {
      "main": [
        [
          {
            "node": "AI Agent",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Gemini Chat Model": {
      "ai_languageModel": [
        [
          {
            "node": "AI Agent",
            "type": "ai_languageModel",
            "index": 0
          }
        ]
      ]
    },
    "Get row(s) in sheet in Google Sheets": {
      "ai_tool": [
        [
          {
            "node": "AI Agent",
            "type": "ai_tool",
            "index": 0
          }
        ]
      ]
    },
    "Reformatted to upload in Google sheet": {
      "main": [
        [
          {
            "node": "Append row in sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级 - 文档提取, AI 摘要总结

需要付费吗?

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

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

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

作者
Sayone Technologies

Sayone Technologies

@sayonetech

SayOne Technologies is a digital transformation and IT services company headquartered in India, with a strong focus on web, mobile, and AI-driven solutions for the retail tech space. With over a decade of experience, SayOne partners with global businesses to build scalable applications, optimize inventory and operations using next-gen AI, and deliver customer-centric digital products.

外部链接
在 n8n.io 查看

分享此工作流