{
  "id": "bdgsGmailCsvLab1",
  "name": "Keep the CSV attachments and their Gmail message IDs",
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "nodes": [
    {
      "id": "b45d5607-3ec8-58ec-9f45-f861984e0c14",
      "name": "Run the fictional inbox",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        0,
        80
      ],
      "typeVersion": 1,
      "parameters": {}
    },
    {
      "id": "9ae0be30-dc5d-5b27-8eda-b515d4b6cf4b",
      "name": "Generate sample messages",
      "type": "n8n-nodes-base.code",
      "position": [
        240,
        80
      ],
      "typeVersion": 2,
      "parameters": {
        "jsCode": "const specs = [\n  {id:'example-A', threadId:'thread-demo', files:[\n    ['attachment_0','signature.png','image/png','fictional signature'],\n    ['attachment_1','orders.csv','text/csv','item,units\\n\"Pencils, blue\",3\\nErasers,2\\n'],\n    ['attachment_3','returns.CSV','application/octet-stream','item,units\\nFolders,1\\n']]},\n  {id:'example-B', threadId:'thread-demo', files:[\n    ['attachment_4','orders.csv','text/csv','item,units\\nLabels,4\\n'],\n    ['attachment_5','orders.csv.txt','text/plain','fictional note']]},\n  {id:'example-C', threadId:'thread-other', files:[\n    ['attachment_8','signature.png','image/png','fictional signature']]}\n];\nconst items = [];\nfor (const message of specs) {\n  const binary = {};\n  for (const [key, name, mime, text] of message.files) {\n    const bytes = mime === 'image/png'\n      ? Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+/5yUAAAAASUVORK5CYII=', 'base64')\n      : Buffer.from(text, 'utf8');\n    binary[key] = await this.helpers.prepareBinaryData(bytes, name, mime);\n  }\n  items.push({json:{id:message.id,threadId:message.threadId},binary});\n}\nreturn items;"
      }
    },
    {
      "id": "61f669e1-0765-5615-a94c-2cf6b6e30ddc",
      "name": "Select CSV attachments",
      "type": "n8n-nodes-base.code",
      "position": [
        480,
        80
      ],
      "typeVersion": 2,
      "parameters": {
        "jsCode": "/*\nMIT License\n\nCopyright (c) 2026 QuasiLife\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies\nof the Software, and to permit persons to whom the Software is furnished to do\nso, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n*/\n\nconst selected = [];\nfor (const [index, item] of $input.all().entries()) {\n  for (const [property, file] of Object.entries(item.binary ?? {})) {\n    if (!file.fileName || !/\\.csv$/i.test(file.fileName)) continue;\n    if (!item.json.id) throw new Error('A matching attachment needs the source Gmail message ID in json.id.');\n    selected.push({\n      json: {messageId: item.json.id, attachmentProperty: property, filename: file.fileName},\n      binary: {data: file},\n      pairedItem: {item: index}\n    });\n  }\n}\nreturn selected;"
      }
    },
    {
      "id": "1b620071-f28b-53d5-91a7-6c647d051c72",
      "name": "Parse CSV rows",
      "type": "n8n-nodes-base.extractFromFile",
      "position": [
        720,
        80
      ],
      "typeVersion": 1,
      "parameters": {
        "operation": "csv",
        "binaryPropertyName": "data",
        "options": {
          "headerRow": true
        }
      }
    },
    {
      "id": "864b73ac-76cb-5860-9d9a-6d2570b7c889",
      "name": "Keep message and filename",
      "type": "n8n-nodes-base.code",
      "position": [
        960,
        80
      ],
      "typeVersion": 2,
      "parameters": {
        "jsCode": "return $input.all().map((row, index) => {\n  const source = $('Select CSV attachments').itemMatching(index).json;\n  return {\n    json: {source: {\n      messageId: source.messageId,\n      filename: source.filename,\n      attachmentProperty: source.attachmentProperty\n    }, row: row.json},\n    pairedItem: {item: index}\n  };\n});"
      }
    },
    {
      "id": "3109bd12-6e5c-5cac-8856-40f1710921da",
      "name": "How to use this rehearsal",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        220,
        -240
      ],
      "typeVersion": 1,
      "parameters": {
        "width": 760,
        "height": 210,
        "content": "## Parse only the CSV attachments from a Gmail message\nClick **Execute Workflow** using the fictional inbox. Expect 3 CSV files and 4 output rows. The repeat filename belongs to two different messages.\n\nIn a copy of your regular workflow, feed **one item per Gmail message** with **Download Attachments** enabled into **Select CSV attachments**. Images and other extensions are ignored. Use UTF-8 comma-separated CSV with a header. Inspect the output before connecting any destination.\n\nOriginal workflow by bulldo.gs, MIT. https://bulldo.gs/demos/gmail-csv/"
      }
    }
  ],
  "connections": {
    "Run the fictional inbox": {
      "main": [
        [
          {
            "node": "Generate sample messages",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Generate sample messages": {
      "main": [
        [
          {
            "node": "Select CSV attachments",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Select CSV attachments": {
      "main": [
        [
          {
            "node": "Parse CSV rows",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Parse CSV rows": {
      "main": [
        [
          {
            "node": "Keep message and filename",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "pinData": {}
}
