{
  "name": "S2D recipe - google-sheets-invoice-pdf",
  "nodes": [
    {
      "id": "d1000000-0000-4000-8000-000000000001",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "typeVersion": 1,
      "position": [
        -540,
        -360
      ],
      "parameters": {
        "width": 600,
        "height": 320,
        "content": "## Invoice PDF from a Google Sheets row\nMark an invoice row Ready and it becomes one SourceToDocs run: the invoice from your Google Docs template, with its line items from the Lines tab, saved to Google Drive as a Doc and a PDF. Both links are written back to the row and Status changes to Generated.\n\n**Set up before you activate**\n- Credentials: Google Sheets on the trigger and both Google Sheets nodes, SourceToDocs (API token) on the SourceToDocs node\n- SourceToDocs document: your invoice document with the data sets Invoices and Lines, a Google Drive distribution and Also generate PDF on. Pick it in the SourceToDocs node, then the data set Invoices and, under Options, Additional Data Sets, the data set Lines\n- Your sheet: the invoicing spreadsheet in the trigger and both Google Sheets nodes (tabs Invoices and Lines)\n- Execute Once is on in the SourceToDocs node's Settings. Keep it on.\n\nGuide: https://sourcetodocs.com/automations/google-sheets-invoice-pdf/"
      }
    },
    {
      "id": "d1000000-0000-4000-8000-000000000002",
      "name": "Google Sheets Trigger",
      "type": "n8n-nodes-base.googleSheetsTrigger",
      "typeVersion": 1,
      "position": [
        -480,
        0
      ],
      "parameters": {
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        },
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "event": "anyUpdate",
        "includeInOutput": "new",
        "options": {
          "columnsToWatch": [
            "Status"
          ]
        }
      }
    },
    {
      "id": "d1000000-0000-4000-8000-000000000003",
      "name": "If",
      "type": "n8n-nodes-base.if",
      "typeVersion": 2.4,
      "position": [
        -260,
        0
      ],
      "parameters": {
        "conditions": {
          "options": {
            "version": 3,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "d1c00000-0000-4000-8000-000000000001",
              "operator": {
                "name": "filter.operator.equals",
                "type": "string",
                "operation": "equals"
              },
              "leftValue": "={{ $json.Status }}",
              "rightValue": "Ready"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "d1000000-0000-4000-8000-000000000004",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "typeVersion": 3,
      "position": [
        -40,
        -20
      ],
      "parameters": {
        "batchSize": 1,
        "options": {}
      }
    },
    {
      "id": "d1000000-0000-4000-8000-000000000005",
      "name": "Get line items",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        200,
        0
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "read",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "filtersUI": {
          "values": [
            {
              "lookupColumn": "Invoice number",
              "lookupValue": "={{ $json['Invoice number'] }}"
            }
          ]
        },
        "options": {}
      }
    },
    {
      "id": "d1000000-0000-4000-8000-000000000006",
      "name": "SourceToDocs",
      "type": "@twovmodules/n8n-nodes-sourcetodocs.sourceToDocs",
      "typeVersion": 1.1,
      "position": [
        440,
        0
      ],
      "executeOnce": true,
      "parameters": {
        "resource": "document",
        "operation": "generate",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "rowsFrom": "mapped",
        "mappedDataSetId": "",
        "output": "simplified",
        "options": {
          "additionalDataSets": {
            "values": [
              {
                "dataSetId": "",
                "rows": "={{ $('Get line items').all().map(i => ({ 'Description': i.json['Description'], 'Qty': i.json['Qty'], 'Unit price': i.json['Unit price'], 'Amount': i.json['Amount'] })) }}"
              }
            ]
          }
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Invoice number": "={{ $('Loop Over Items').item.json['Invoice number'] }}",
            "Client": "={{ $('Loop Over Items').item.json['Client'] }}",
            "Client address": "={{ $('Loop Over Items').item.json['Client address'] }}",
            "Issue date": "={{ $('Loop Over Items').item.json['Issue date'] }}",
            "Due date": "={{ $('Loop Over Items').item.json['Due date'] }}",
            "Total": "={{ $('Loop Over Items').item.json['Total'] }}"
          },
          "matchingColumns": [],
          "schema": [
            {
              "id": "Invoice number",
              "displayName": "Invoice number",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "display": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "Client",
              "displayName": "Client",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "display": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "Client address",
              "displayName": "Client address",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "display": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "Issue date",
              "displayName": "Issue date",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "display": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "Due date",
              "displayName": "Due date",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "display": true,
              "type": "string",
              "removed": false
            },
            {
              "id": "Total",
              "displayName": "Total",
              "required": false,
              "defaultMatch": false,
              "canBeUsedToMatch": false,
              "display": true,
              "type": "string",
              "removed": false
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        }
      }
    },
    {
      "id": "d1000000-0000-4000-8000-000000000007",
      "name": "Google Sheets",
      "type": "n8n-nodes-base.googleSheets",
      "typeVersion": 4.7,
      "position": [
        680,
        0
      ],
      "parameters": {
        "resource": "sheet",
        "operation": "update",
        "documentId": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "sheetName": {
          "__rl": true,
          "mode": "list",
          "value": ""
        },
        "columns": {
          "mappingMode": "defineBelow",
          "value": {
            "Invoice number": "={{ $('Loop Over Items').item.json['Invoice number'] }}",
            "Invoice link": "={{ $json.file_url }}",
            "PDF link": "={{ $json.deliveries[0].result.pdf_url }}",
            "Status": "Generated"
          },
          "matchingColumns": [
            "Invoice number"
          ],
          "schema": [
            {
              "id": "Invoice number",
              "displayName": "Invoice number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Client",
              "displayName": "Client",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Client address",
              "displayName": "Client address",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Issue date",
              "displayName": "Issue date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Due date",
              "displayName": "Due date",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Total",
              "displayName": "Total",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": true
            },
            {
              "id": "Status",
              "displayName": "Status",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "Invoice link",
              "displayName": "Invoice link",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "PDF link",
              "displayName": "PDF link",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "string",
              "canBeUsedToMatch": true,
              "removed": false
            },
            {
              "id": "row_number",
              "displayName": "row_number",
              "required": false,
              "defaultMatch": false,
              "display": true,
              "type": "number",
              "canBeUsedToMatch": true,
              "readOnly": true,
              "removed": true
            }
          ],
          "attemptToConvertTypes": false,
          "convertFieldsToString": false
        },
        "options": {}
      }
    }
  ],
  "connections": {
    "Google Sheets Trigger": {
      "main": [
        [
          {
            "node": "If",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "If": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ],
        []
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Get line items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get line items": {
      "main": [
        [
          {
            "node": "SourceToDocs",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "SourceToDocs": {
      "main": [
        [
          {
            "node": "Google Sheets",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Google Sheets": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  }
}
