Synchronize your Google Sheets with Postgres

工作流概述

这是一个包含10个节点的复杂工作流,主要用于自动化处理各种任务。

工作流源代码

下载
{
  "id": "wDD4XugmHIvx3KMT",
  "meta": {
    "instanceId": "149cdf730f0c143663259ddc6124c9c26e824d8d2d059973b871074cf4bda531"
  },
  "name": "Synchronize your Google Sheets with Postgres",
  "tags": [],
  "nodes": [
    {
      "id": "44171bad-84b6-49f8-b538-fb0c2d52db43",
      "name": "Schedule Trigger",
      "type": "n8n-nodes-base.scheduleTrigger",
      "position": [
        900,
        360
      ],
      "parameters": {
        "rule": {
          "interval": [
            {
              "field": "hours"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "1d1558cc-523b-4985-81e2-da49e3d0f4b7",
      "name": "Compare Datasets",
      "type": "n8n-nodes-base.compareDatasets",
      "position": [
        1820,
        380
      ],
      "parameters": {
        "options": {},
        "resolve": "preferInput1",
        "mergeByFields": {
          "values": [
            {
              "field1": "first_name",
              "field2": "first_name"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "b4442fd7-6817-40bb-a76e-851659c836ec",
      "name": "Split Out Relevant Fields",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        1460,
        240
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "first_name, last_name, town, age"
      },
      "typeVersion": 1
    },
    {
      "id": "b63899bd-f842-4ead-a590-9bdacdc9b3c0",
      "name": "Retrieve Sheets Data",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1200,
        240
      ],
      "parameters": {
        "options": {},
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": "gid=0",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1jhUobbdaEuX093J745TsPFMPFbzAIIgx6HnIzdqYqhg/edit#gid=0",
          "cachedResultName": "Sheet1"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1jhUobbdaEuX093J745TsPFMPFbzAIIgx6HnIzdqYqhg",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1jhUobbdaEuX093J745TsPFMPFbzAIIgx6HnIzdqYqhg/edit?usp=drivesdk",
          "cachedResultName": "Testing_Sheet"
        }
      },
      "typeVersion": 4.2
    },
    {
      "id": "ae4918fb-07ef-48db-ba25-ea34c5af43af",
      "name": "Select Rows in Postgres",
      "type": "n8n-nodes-base.postgres",
      "position": [
        1200,
        540
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "testing",
          "cachedResultName": "testing"
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "options": {},
        "operation": "select",
        "returnAll": true
      },
      "typeVersion": 2.3
    },
    {
      "id": "4d08d771-0e80-445e-92db-08197418512d",
      "name": "Insert Rows",
      "type": "n8n-nodes-base.postgres",
      "position": [
        2300,
        260
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "testing",
          "cachedResultName": "testing"
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "columns": {
          "value": {},
          "schema": [
            {
              "id": "first_name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "first_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "last_name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "last_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "town",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "town",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "age",
              "type": "number",
              "display": true,
              "required": false,
              "displayName": "age",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "autoMapInputData",
          "matchingColumns": []
        },
        "options": {}
      },
      "typeVersion": 2.3
    },
    {
      "id": "3fd7baa1-72c7-4587-a557-02eb4dfa92f5",
      "name": "Update Rows",
      "type": "n8n-nodes-base.postgres",
      "position": [
        2300,
        460
      ],
      "parameters": {
        "table": {
          "__rl": true,
          "mode": "list",
          "value": "testing",
          "cachedResultName": "testing"
        },
        "schema": {
          "__rl": true,
          "mode": "list",
          "value": "public"
        },
        "columns": {
          "value": {
            "age": "={{ $json.age }}",
            "town": "={{ $json.town }}",
            "last_name": "={{ $json.last_name }}",
            "first_name": "={{ $json.first_name }}"
          },
          "schema": [
            {
              "id": "first_name",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "first_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "last_name",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "last_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "town",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "town",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "age",
              "type": "number",
              "display": true,
              "required": false,
              "displayName": "age",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "first_name",
            "last_name"
          ]
        },
        "options": {},
        "operation": "update"
      },
      "typeVersion": 2.3
    },
    {
      "id": "fc8dbe79-a54d-46fb-8ef7-4bb8b2a402ee",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        360,
        260
      ],
      "parameters": {
        "width": 485.5994596522446,
        "height": 350.08576009540855,
        "content": "## Setup ##
In order to make this automation work for you, you need to make a few adjustments:

1. Add your Postgres & Google Sheets Credentials to the respective Nodes

2. Select the Sheet (Google Sheets) and the table (Postgres) you want to sync

3. Update the Insert & Update Queries so that the data is updated in the table you also selected the rows from in the first step"
      },
      "typeVersion": 1
    },
    {
      "id": "3719112b-1ec7-4402-a366-b1b845819e8d",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        2080,
        160
      ],
      "parameters": {
        "width": 485.5994596522446,
        "height": 486.693620858174,
        "content": "## Updating Your Database 
Using Insert Rows & Update Rows as Separate Postgres Node's"
      },
      "typeVersion": 1
    },
    {
      "id": "7742972b-7996-4f9a-9c1d-700737b94eec",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1080,
        140
      ],
      "parameters": {
        "width": 543.3950930518761,
        "height": 553.2461684092643,
        "content": "## Retrieving Data & Spitting Out Fields 
Get the Data you want to compare and split out the relevant fields"
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "ac0f0ed3-3f25-4672-a34a-29b5f4402e63",
  "connections": {
    "Compare Datasets": {
      "main": [
        [
          {
            "node": "Insert Rows",
            "type": "main",
            "index": 0
          }
        ],
        [],
        [
          {
            "node": "Update Rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Schedule Trigger": {
      "main": [
        [
          {
            "node": "Retrieve Sheets Data",
            "type": "main",
            "index": 0
          },
          {
            "node": "Select Rows in Postgres",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Retrieve Sheets Data": {
      "main": [
        [
          {
            "node": "Split Out Relevant Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Select Rows in Postgres": {
      "main": [
        [
          {
            "node": "Compare Datasets",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Split Out Relevant Fields": {
      "main": [
        [
          {
            "node": "Compare Datasets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}

功能特点

  • 自动检测新邮件
  • AI智能内容分析
  • 自定义分类规则
  • 批量处理能力
  • 详细的处理日志

技术分析

节点类型及作用

  • Scheduletrigger
  • Comparedatasets
  • Splitout
  • Googlesheets
  • Postgres

复杂度评估

配置难度:
★★★★☆
维护难度:
★★☆☆☆
扩展性:
★★★★☆

实施指南

前置条件

  • 有效的Gmail账户
  • n8n平台访问权限
  • Google API凭证
  • AI分类服务订阅

配置步骤

  1. 在n8n中导入工作流JSON文件
  2. 配置Gmail节点的认证信息
  3. 设置AI分类器的API密钥
  4. 自定义分类规则和标签映射
  5. 测试工作流执行
  6. 配置定时触发器(可选)

关键参数

参数名称 默认值 说明
maxEmails 50 单次处理的最大邮件数量
confidenceThreshold 0.8 分类置信度阈值
autoLabel true 是否自动添加标签

最佳实践

优化建议

  • 定期更新AI分类模型以提高准确性
  • 根据邮件量调整处理批次大小
  • 设置合理的分类置信度阈值
  • 定期清理过期的分类规则

安全注意事项

  • 妥善保管API密钥和认证信息
  • 限制工作流的访问权限
  • 定期审查处理日志
  • 启用双因素认证保护Gmail账户

性能优化

  • 使用增量处理减少重复工作
  • 缓存频繁访问的数据
  • 并行处理多个邮件分类任务
  • 监控系统资源使用情况

故障排除

常见问题

邮件未被正确分类

检查AI分类器的置信度阈值设置,适当降低阈值或更新训练数据。

Gmail认证失败

确认Google API凭证有效且具有正确的权限范围,重新进行OAuth授权。

调试技巧

  • 启用详细日志记录查看每个步骤的执行情况
  • 使用测试邮件验证分类逻辑
  • 检查网络连接和API服务状态
  • 逐步执行工作流定位问题节点

错误处理

工作流包含以下错误处理机制:

  • 网络超时自动重试(最多3次)
  • API错误记录和告警
  • 处理失败邮件的隔离机制
  • 异常情况下的回滚操作