{
  "meta": {
    "templateCredsSetupCompleted": false
  },
  "name": "Automatically generate an Invoice PDF from Google Sheets data with DocuPotion",
  "tags": [],
  "nodes": [
    {
      "id": "487323b0-8936-4a05-bdc9-f54a0836a3fc",
      "name": "Invoice Status Updated to Active",
      "type": "n8n-nodes-base.googleSheetsTrigger",
      "position": [
        16,
        368
      ],
      "parameters": {
        "event": "rowUpdate",
        "options": {
          "columnsToWatch": [
            "status"
          ]
        },
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1441531062,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QDxomX6g1ycS_4p9MU3vdEuveKGwGyw0eqnsMzDo0ho/edit#gid=1441531062",
          "cachedResultName": "invoices"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1QDxomX6g1ycS_4p9MU3vdEuveKGwGyw0eqnsMzDo0ho",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QDxomX6g1ycS_4p9MU3vdEuveKGwGyw0eqnsMzDo0ho/edit?usp=drivesdk",
          "cachedResultName": "invoices"
        }
      },
      "typeVersion": 1
    },
    {
      "id": "8b686e35-af70-41f4-931b-cf1b48d9300e",
      "name": "Filter for Updated Invoice",
      "type": "n8n-nodes-base.filter",
      "position": [
        224,
        368
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "ebf2d281-ae77-43a8-a40a-bdab7b8dbb86",
              "operator": {
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.status }}",
              "rightValue": "Active"
            }
          ]
        }
      },
      "typeVersion": 2.3
    },
    {
      "id": "05f9174f-5025-4878-870f-7b4da57c6094",
      "name": "Loop Over Invoices",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        480,
        368
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "68d816b5-26d8-4007-9132-6ee7adc71335",
      "name": "Get Invoice Items",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        704,
        384
      ],
      "parameters": {
        "options": {},
        "filtersUI": {
          "values": [
            {
              "lookupValue": "={{ $json.invoice_number }}",
              "lookupColumn": "invoice_number"
            }
          ]
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1852295381,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QDxomX6g1ycS_4p9MU3vdEuveKGwGyw0eqnsMzDo0ho/edit#gid=1852295381",
          "cachedResultName": "line items"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1QDxomX6g1ycS_4p9MU3vdEuveKGwGyw0eqnsMzDo0ho",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QDxomX6g1ycS_4p9MU3vdEuveKGwGyw0eqnsMzDo0ho/edit?usp=drivesdk",
          "cachedResultName": "invoices"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "ecd31ef9-ff28-4a67-974c-b94d3e964039",
      "name": "Create JSON Object From Items",
      "type": "n8n-nodes-base.aggregate",
      "position": [
        880,
        384
      ],
      "parameters": {
        "options": {},
        "aggregate": "aggregateAllItemData"
      },
      "typeVersion": 1
    },
    {
      "id": "a1cba617-6079-4f2e-aadb-f68c7eec26e1",
      "name": "Generate PDF",
      "type": "n8n-nodes-docupotion.docupotion",
      "position": [
        1040,
        384
      ],
      "parameters": {
        "data": "={\n  \"invoice_number\": \"{{ $('Invoice Status Updated to Active').item.json.invoice_number }}\",\n  \"invoice_date\": \"{{ $('Invoice Status Updated to Active').item.json.invoice_date }}\",\n  \"due_date\": \"{{ $('Invoice Status Updated to Active').item.json.due_date }}\",\n  \"customer_name\": \"{{ $('Invoice Status Updated to Active').item.json.customer_name }}\",\n  \"customer_company\": \"{{ $('Invoice Status Updated to Active').item.json.customer_company }}\",\n  \"customer_address\": \"{{ $('Invoice Status Updated to Active').item.json.customer_address }}\",\n  \"customer_email\": \"{{ $('Invoice Status Updated to Active').item.json.customer_email }}\",\n  \"total\": \"{{ $('Create JSON Object From Items').item.json.data.reduce((sum, item) => sum + (item.quantity * item.unit_price), 0).toFixed(2) }}\",\n  \"line_items\": {{ JSON.stringify($('Create JSON Object From Items').item.json.data) }}\n} ",
        "templateId": "YOUR_DOCUPOTION_TEMPLATE_ID"
      },
      "typeVersion": 1
    },
    {
      "id": "de59529a-1d63-4bde-bd17-446bf921e8b5",
      "name": "Upload to Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        1056,
        672
      ],
      "parameters": {
        "name": "=Invoice-{{ $('Loop Over Invoices').item.json.invoice_number }}.pdf",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "root",
          "cachedResultName": "/ (Root folder)"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "5d9d2160-d4cd-4275-b3a4-278dcfaef7a7",
      "name": "Add Link to Google Sheet",
      "type": "n8n-nodes-base.googleSheets",
      "position": [
        1056,
        896
      ],
      "parameters": {
        "columns": {
          "value": {
            "status": "Sent",
            "pdf_url": "={{ $json.webViewLink }}",
            "invoice_number": "={{ $('Loop Over Invoices').item.json.invoice_number }}"
          },
          "schema": [
            {
              "id": "invoice_number",
              "type": "string",
              "display": true,
              "removed": false,
              "required": false,
              "displayName": "invoice_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "invoice_date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "invoice_date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "due_date",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "due_date",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "status",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "status",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "customer_name",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "customer_name",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "customer_email",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "customer_email",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "customer_company",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "customer_company",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "customer_address",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "customer_address",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "pdf_url",
              "type": "string",
              "display": true,
              "required": false,
              "displayName": "pdf_url",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            },
            {
              "id": "row_number",
              "type": "number",
              "display": true,
              "removed": true,
              "readOnly": true,
              "required": false,
              "displayName": "row_number",
              "defaultMatch": false,
              "canBeUsedToMatch": true
            }
          ],
          "mappingMode": "defineBelow",
          "matchingColumns": [
            "invoice_number"
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {},
        "operation": "update",
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": 1441531062,
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QDxomX6g1ycS_4p9MU3vdEuveKGwGyw0eqnsMzDo0ho/edit#gid=1441531062",
          "cachedResultName": "invoices"
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": "1QDxomX6g1ycS_4p9MU3vdEuveKGwGyw0eqnsMzDo0ho",
          "cachedResultUrl": "https://docs.google.com/spreadsheets/d/1QDxomX6g1ycS_4p9MU3vdEuveKGwGyw0eqnsMzDo0ho/edit?usp=drivesdk",
          "cachedResultName": "invoices"
        }
      },
      "typeVersion": 4.7
    },
    {
      "id": "6706b40e-13eb-42c4-805b-914146499702",
      "name": "Sticky Note - Overview",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -576,
        176
      ],
      "parameters": {
        "color": 4,
        "width": 540,
        "height": 656,
        "content": "## Generate PDF Invoices from Google Sheets\n\n**Who's it for:** Freelancers, small businesses, or anyone issuing invoices from a spreadsheet.\n\n**What it does:** When you change the status of an invoice to `Active` in Google Sheets, this workflow creates a PDF of an invoice from a reusable DocuPotion template.\n\n**How it works:**\n1. Google Sheets Trigger watches the `invoices` sheet for status changes (polls every minute)\n2. Filter: only rows where `status = Active` proceed\n3. Loop over each updated invoice:\n   - Fetch matching rows from the `line items` sheet\n   - Aggregate line items into a single array\n   - Generate PDF via DocuPotion template\n   - Upload to Google Drive\n   - Update the invoice row: set `pdf_url` and status `Sent`\n\n**Setup (see sticky notes below):**\n- Copy the example Google Sheet (link in template description) and replace the document ID in all three Google Sheets nodes\n- Install the DocuPotion community node\n- Create a DocuPotion invoice template and paste its ID into the `Generate PDF` node\n- Connect Google Sheets, Google Drive, and DocuPotion credentials"
      },
      "typeVersion": 1
    },
    {
      "id": "6472b160-a55f-4bc5-8480-ac8b92295c6a",
      "name": "Sticky Note - Sheets Setup",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        0,
        0
      ],
      "parameters": {
        "color": 5,
        "width": 532,
        "height": 300,
        "content": "### \u2699\ufe0f Setup: Google Sheets\nReplace the **document ID** in this node (and in `Get Invoice Items` + `Add Link to Google Sheet`) with your own.\n\nYour Sheet needs two tabs:\n- **invoices**: `invoice_number`, `invoice_date`, `due_date`, `status`, `customer_name`, `customer_email`, `customer_company`, `customer_address`, `pdf_url`\n- **line items**: `invoice_number`, `description`, `quantity`, `unit_price`\n\nTo trigger: set a row's `status` to `Active`."
      },
      "typeVersion": 1
    },
    {
      "id": "39cdb2fd-220f-4c13-81d3-3ad0ccba02ff",
      "name": "Sticky Note - DocuPotion Setup",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1200,
        384
      ],
      "parameters": {
        "color": 3,
        "width": 404,
        "height": 184,
        "content": "### \u2699\ufe0f Setup: DocuPotion\nThis is a **community node**. Install via:\nSettings \u2192 Community Nodes \u2192 DocuPotion\n\nThen create your invoice template in DocuPotion and replace the `YOUR_DOCUPOTION_TEMPLATE_ID` placeholder in this node's **Template ID** field."
      },
      "typeVersion": 1
    },
    {
      "id": "2e7c0c29-1ee8-457b-94fd-0da1ebc19b4e",
      "name": "Sticky Note - Drive Setup",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        1216,
        672
      ],
      "parameters": {
        "color": 6,
        "width": 320,
        "height": 172,
        "content": "### \u2699\ufe0f Optional: Google Drive\nPDFs are saved to the **root** of your Drive with filename `Invoice-{invoice_number}.pdf`.\n\nTo save them to a subfolder instead, change the **Folder** field in this node."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "binaryMode": "separate",
    "executionOrder": "v1"
  },
  "connections": {
    "Generate PDF": {
      "main": [
        [
          {
            "node": "Upload to Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get Invoice Items": {
      "main": [
        [
          {
            "node": "Create JSON Object From Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Invoices": {
      "main": [
        [],
        [
          {
            "node": "Get Invoice Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload to Google Drive": {
      "main": [
        [
          {
            "node": "Add Link to Google Sheet",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Add Link to Google Sheet": {
      "main": [
        [
          {
            "node": "Loop Over Invoices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filter for Updated Invoice": {
      "main": [
        [
          {
            "node": "Loop Over Invoices",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create JSON Object From Items": {
      "main": [
        [
          {
            "node": "Generate PDF",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Invoice Status Updated to Active": {
      "main": [
        [
          {
            "node": "Filter for Updated Invoice",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}