{ "name": "Log Unread Gmail Classifications to Sheets", "nodes": [ { "parameters": { "pollTimes": { "item": [ { "mode": "everyMinute", "minute": 5 } ] }, "simple": false, "filters": { "readStatus": "unread", "includeDrafts": false } }, "id": "log-unread-gmail-classifications-to-sheets-0", "name": "Gmail: New Email", "type": "n8n-nodes-base.gmailTrigger", "typeVersion": 1.2, "position": [ 200, 400 ] }, { "name": "Process one email", "type": "n8n-nodes-base.splitInBatches", "typeVersion": 3, "parameters": { "batchSize": 1, "options": {} }, "id": "log-unread-gmail-classifications-to-sheets-1" }, { "parameters": { "mode": "runOnceForEachItem", "jsCode": "const e = $json,\n v = e.from?.value?.[0];\nif (!v?.address) throw Error(\"Missing sender\");\nreturn {\n json: {\n messageId: e.id,\n from: v.address,\n fromName: v.name || \"\",\n subject: e.subject || \"\",\n snippet: String(e.html || e.text || \"\").slice(0, 500),\n date: e.date || \"\",\n },\n};\n" }, "id": "log-unread-gmail-classifications-to-sheets-2", "name": "Parse Email", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 420, 400 ] }, { "parameters": { "resource": "text", "operation": "message", "modelId": { "__rl": true, "mode": "list", "value": "gpt-4o-mini" }, "messages": { "values": [ { "role": "system", "content": "Classify this email as urgent, important, informational, or spam. Return JSON with category and one-sentence summary." }, { "role": "user", "content": "=From: {{$json.fromName}} <{{$json.from}}>\nSubject: {{$json.subject}}\n\n{{$json.snippet}}" } ] }, "jsonOutput": true, "simplify": true, "options": { "temperature": 0.2, "maxTokens": 200 } }, "id": "log-unread-gmail-classifications-to-sheets-3", "name": "AI: Classify", "type": "@n8n/n8n-nodes-langchain.openAi", "typeVersion": 1.8, "position": [ 640, 400 ], "retryOnFail": true, "maxTries": 3, "waitBetweenTries": 2000 }, { "parameters": { "mode": "runOnceForEachItem", "jsCode": "const email = $(\"Parse Email\").item.json;\nlet ai = $json.message?.content;\nif (typeof ai === \"string\") {\n try {\n ai = JSON.parse(ai);\n } catch {\n throw Error(\"Invalid classification JSON\");\n }\n}\nif (\n ![\"urgent\", \"important\", \"informational\", \"spam\"].includes(ai.category) ||\n typeof ai.summary !== \"string\" ||\n !ai.summary.trim()\n)\n throw Error(\"Invalid classification\");\nreturn { json: { ...email, category: ai.category, summary: ai.summary } };\n" }, "id": "log-unread-gmail-classifications-to-sheets-4", "name": "Merge Result", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [ 860, 400 ] }, { "parameters": { "operation": "append", "documentId": { "__rl": true, "mode": "id", "value": "YOUR_SPREADSHEET_ID" }, "sheetName": { "__rl": true, "mode": "name", "value": "Lite Log" }, "columns": { "mappingMode": "defineBelow", "value": { "Date": "={{ $json.date }}", "From": "={{ $json.from }}", "Subject": "={{ $json.subject }}", "Category": "={{ $json.category }}", "Summary": "={{ $json.summary }}" }, "schema": [ { "id": "Date", "displayName": "Date", "type": "string", "required": false, "defaultMatch": false, "display": true, "canBeUsedToMatch": true }, { "id": "From", "displayName": "From", "type": "string", "required": false, "defaultMatch": false, "display": true, "canBeUsedToMatch": true }, { "id": "Subject", "displayName": "Subject", "type": "string", "required": false, "defaultMatch": false, "display": true, "canBeUsedToMatch": true }, { "id": "Category", "displayName": "Category", "type": "string", "required": false, "defaultMatch": false, "display": true, "canBeUsedToMatch": true }, { "id": "Summary", "displayName": "Summary", "type": "string", "required": false, "defaultMatch": false, "display": true, "canBeUsedToMatch": true } ] }, "options": { "cellFormat": "RAW", "useAppend": true } }, "id": "log-unread-gmail-classifications-to-sheets-5", "name": "Log to Sheets", "type": "n8n-nodes-base.googleSheets", "typeVersion": 4.5, "position": [ 1080, 400 ] } ], "connections": { "Gmail: New Email": { "main": [ [ { "node": "Process one email", "type": "main", "index": 0 } ] ] }, "Process one email": { "main": [ [], [ { "node": "Parse Email", "type": "main", "index": 0 } ] ] }, "Parse Email": { "main": [ [ { "node": "AI: Classify", "type": "main", "index": 0 } ] ] }, "AI: Classify": { "main": [ [ { "node": "Merge Result", "type": "main", "index": 0 } ] ] }, "Merge Result": { "main": [ [ { "node": "Log to Sheets", "type": "main", "index": 0 } ] ] }, "Log to Sheets": { "main": [ [ { "node": "Process one email", "type": "main", "index": 0 } ] ] } }, "settings": { "executionOrder": "v1", "saveManualExecutions": true }, "tags": [ { "name": "InboxZero", "id": "inboxzero" }, { "name": "AutoFlow", "id": "autoflow" }, { "name": "Lite", "id": "lite" } ], "active": false, "pinData": {} }