8
n8n 中文网amn8n.com

使用子工作流的嵌套循环

中级

这是一个Engineering领域的自动化工作流,包含 9 个节点。主要使用 Set, Code, ManualTrigger, SplitInBatches, ExecuteWorkflow 等节点。 使用n8n子工作流创建嵌套数据处理循环

前置要求
  • 无特殊前置要求,导入即可使用

分类

工作流预览
可视化展示节点连接关系,支持缩放和平移
导出工作流
复制以下 JSON 配置到 n8n 导入,即可使用此工作流
{
  "id": "WX9Oa7y6UMwGUYBM",
  "meta": {
    "instanceId": "8981479cb588889c05b145eaed421551d37a4fff11ab279d3f4744a6577c6002"
  },
  "name": "使用子工作流的嵌套循环",
  "tags": [],
  "nodes": [
    {
      "id": "f7e5c300-fad6-4fc7-a7ff-79f7fac144f0",
      "name": "当点击“执行工作流”时",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        0,
        80
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "19f41efd-26ff-4950-9449-d52b8a98f040",
      "name": "颜色",
      "type": "n8n-nodes-base.code",
      "position": [
        224,
        80
      ],
      "parameters": {
        "jsCode": "return [\n  {\n    \"color\": \"yellow\"\n  },\n  {\n    \"color\": \"blue\"\n  },\n  {\n    \"color\": \"green\"\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "88109d36-2486-4b98-9377-893330490fb7",
      "name": "遍历颜色",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        448,
        80
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "c2b45eaa-eae7-45f6-b15f-6e863dedc71c",
      "name": "当被其他工作流执行时",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        0,
        304
      ],
      "parameters": {
        "workflowInputs": {
          "values": [
            {
              "name": "color"
            }
          ]
        }
      },
      "typeVersion": 1.1
    },
    {
      "id": "96372e82-0484-441b-a6d5-12ce5a1ea921",
      "name": "整数",
      "type": "n8n-nodes-base.code",
      "position": [
        224,
        304
      ],
      "parameters": {
        "jsCode": "return [\n  {\n    \"number\": 1\n  },\n  {\n    \"number\": 2\n  },\n  {\n    \"number\": 3\n  }\n];"
      },
      "typeVersion": 2
    },
    {
      "id": "f7a531e8-8fe1-4cf0-9a6a-3cd9ef57fdb8",
      "name": "遍历整数",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        448,
        304
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "37b24ff0-165f-4a18-ae8a-c6054295fa87",
      "name": "编辑字段",
      "type": "n8n-nodes-base.set",
      "position": [
        672,
        304
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "3640152d-340b-4c27-b6f9-5735dcc93844",
              "name": "result",
              "type": "string",
              "value": "={{ $('When Executed by Another Workflow').item.json.color }} - {{ $('Loop Over Integers').item.json.number }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "679311c7-3401-4f5c-b08b-a6bbf7fee61b",
      "name": "便签",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -672,
        80
      ],
      "parameters": {
        "width": 592,
        "height": 512,
        "content": "## 使用子工作流的嵌套循环"
      },
      "typeVersion": 1
    },
    {
      "id": "51a07d4a-a6f5-4061-99ad-3379411c643b",
      "name": "执行子工作流",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        672,
        80
      ],
      "parameters": {
        "options": {},
        "workflowId": {
          "__rl": true,
          "mode": "list",
          "value": "WX9Oa7y6UMwGUYBM",
          "cachedResultName": "Nested Loops with Sub-workflows"
        },
        "workflowInputs": {
          "value": {
            "color": "={{ $json.color }}"
          },
          "schema": [
            {
              "id": "color",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "color",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "color"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "typeVersion": 1.2
    }
  ],
  "active": false,
  "pinData": {},
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "63176d07-d2a5-49fb-90bb-4c6cd8910fd0",
  "connections": {
    "Colors": {
      "main": [
        [
          {
            "node": "Loop Over Colors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Integers": {
      "main": [
        [
          {
            "node": "Loop Over Integers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Edit Fields": {
      "main": [
        [
          {
            "node": "Loop Over Integers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Colors": {
      "main": [
        [],
        [
          {
            "node": "Execute Sub-workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Integers": {
      "main": [
        [],
        [
          {
            "node": "Edit Fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Sub-workflow": {
      "main": [
        [
          {
            "node": "Loop Over Colors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Integers",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "Colors",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}
常见问题

如何使用这个工作流?

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

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

中级 - 工程

需要付费吗?

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

工作流信息
难度等级
中级
节点数量9
分类1
节点类型7
难度说明

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

外部链接
在 n8n.io 查看

分享此工作流