{
  "name": "Auto Label Success Cases from Sheets",
  "nodes": [
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "13iOvpUd6OjZPgMeFKL42RlPk326JCLNzT8tyEM-SY8I",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "OK",
          "mode": "name"
        },
        "options": {
          "range": "A:Z"
        }
      },
      "id": "read-ok-sheet",
      "name": "Read OK Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        240,
        200
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "operation": "read",
        "documentId": {
          "__rl": true,
          "value": "13iOvpUd6OjZPgMeFKL42RlPk326JCLNzT8tyEM-SY8I",
          "mode": "id"
        },
        "sheetName": {
          "__rl": true,
          "value": "Check",
          "mode": "name"
        },
        "options": {
          "range": "A:Z"
        }
      },
      "id": "read-check-sheet",
      "name": "Read Check Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.5,
      "position": [
        240,
        400
      ],
      "credentials": {
        "googleSheetsOAuth2Api": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "mode": "combine",
        "mergeByFields": {
          "values": []
        },
        "options": {}
      },
      "id": "merge-sheets",
      "name": "Merge OK + Check",
      "type": "n8n-nodes-base.merge",
      "typeVersion": 3,
      "position": [
        480,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "// Extract unique email addresses\nconst items = $input.all();\nconst emails = new Set();\n\nfor (const item of items) {\n  // Try common email field names\n  const email = item.json.Email || item.json.email || item.json['Email Address'] || item.json['\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9'] || '';\n  if (email && email.includes('@')) {\n    emails.add(email.toLowerCase().trim());\n  }\n}\n\n// Return as array of objects\nreturn Array.from(emails).map(email => ({ json: { email } }));"
      },
      "id": "extract-emails",
      "name": "Extract Unique Emails",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        700,
        300
      ]
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "getAll",
        "returnAll": false,
        "limit": 100,
        "filters": {
          "q": "=from:{{ $json.email }} OR to:{{ $json.email }}"
        },
        "options": {}
      },
      "id": "search-gmail",
      "name": "Search Gmail Messages",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        920,
        300
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "resource": "message",
        "operation": "addLabels",
        "messageId": "={{ $json.id }}",
        "labelIds": [
          "Label_XXXXXXXXXX"
        ]
      },
      "id": "add-label",
      "name": "Add Success Cases Label",
      "type": "n8n-nodes-base.gmail",
      "typeVersion": 2.1,
      "position": [
        1140,
        300
      ],
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      }
    },
    {
      "parameters": {
        "content": "## Auto Label Success Cases\n\n### \u8a2d\u5b9a\u624b\u9806\n\n1. **Google Sheets\u8a8d\u8a3c**\n   - Read OK Sheet, Read Check Sheet \u30ce\u30fc\u30c9\u306b\u8a8d\u8a3c\u8a2d\u5b9a\n\n2. **Gmail\u8a8d\u8a3c**\n   - Search Gmail Messages, Add Success Cases Label \u30ce\u30fc\u30c9\u306b\u8a8d\u8a3c\u8a2d\u5b9a\n\n3. **Label ID\u3092\u53d6\u5f97**\n   - Add Success Cases Label \u30ce\u30fc\u30c9\u3092\u30af\u30ea\u30c3\u30af\n   - Label IDs \u3067\u300csuccess cases\u300d\u3092\u9078\u629e\n\n4. **\u30b7\u30fc\u30c8\u540d\u306e\u78ba\u8a8d**\n   - OK, Check \u30b7\u30fc\u30c8\u304c\u5b9f\u969b\u306e\u540d\u524d\u3068\u4e00\u81f4\u3059\u308b\u304b\u78ba\u8a8d\n\n5. **\u30e1\u30fc\u30eb\u5217\u540d\u306e\u78ba\u8a8d**\n   - Extract Unique Emails \u306e\u30b3\u30fc\u30c9\u3067\n   - \u5b9f\u969b\u306e\u5217\u540d\u306b\u5408\u308f\u305b\u3066\u4fee\u6b63",
        "height": 400,
        "width": 350
      },
      "id": "sticky-note",
      "name": "Setup Instructions",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        240,
        -100
      ]
    }
  ],
  "connections": {
    "Read OK Sheet": {
      "main": [
        [
          {
            "node": "Merge OK + Check",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Read Check Sheet": {
      "main": [
        [
          {
            "node": "Merge OK + Check",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Merge OK + Check": {
      "main": [
        [
          {
            "node": "Extract Unique Emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Extract Unique Emails": {
      "main": [
        [
          {
            "node": "Search Gmail Messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search Gmail Messages": {
      "main": [
        [
          {
            "node": "Add Success Cases Label",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}