{
  "id": "Hbn25OnndpfNGOuu",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Automate Image Transfer from Google Drive to Google Docs with Resizing",
  "tags": [],
  "nodes": [
    {
      "id": "d3e3e121-15b9-4a85-bebe-0915116bf310",
      "name": "When clicking \u2018Execute workflow\u2019",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        -940,
        -140
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "bf6a7390-7fb0-4df3-bc15-04b5842f5d9e",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        -180,
        220
      ],
      "parameters": {
        "options": {}
      },
      "typeVersion": 3
    },
    {
      "id": "0e92fa37-e4b6-4fe8-b394-bb8f7f3fc6a2",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1000,
        100
      ],
      "parameters": {
        "color": 6,
        "width": 700,
        "height": 320,
        "content": "## Filtering URI And Size Image\n**The task is to retrieve the URI and ID**"
      },
      "typeVersion": 1
    },
    {
      "id": "7a3911db-e786-4f12-bf8d-ff27be32407f",
      "name": "Sticky Note1",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -240,
        100
      ],
      "parameters": {
        "color": 4,
        "width": 460,
        "height": 320,
        "content": "## Sending Images to Docs\nDouble click"
      },
      "typeVersion": 1
    },
    {
      "id": "d056ab42-1891-4574-8383-4ca6306249de",
      "name": "Sticky Note2",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1000,
        -240
      ],
      "parameters": {
        "width": 1220,
        "height": 280,
        "content": "## Trigger and Download Image from Google Drive"
      },
      "typeVersion": 1
    },
    {
      "id": "6504dc49-1732-418d-a187-722b767699ce",
      "name": "Download File",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -300,
        -140
      ],
      "parameters": {
        "fileId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.id }}"
        },
        "options": {
          "fileName": "={{ $json.name }}"
        },
        "operation": "download"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "e33f5b2e-fc15-4500-90e7-22cc5207aec2",
      "name": "Filtering URI",
      "type": "n8n-nodes-base.code",
      "position": [
        -920,
        220
      ],
      "parameters": {
        "jsCode": "return items\n  .sort((a, b) => {\n    const getNumber = name => parseInt(name?.json?.name?.match(/\\d+/)?.[0] || 0, 10);\n    return getNumber(b) - getNumber(a); // Descending\n  })\n  .map(item => {\n    const imageId = item.json.id;\n    return {\n      json: {\n        uri: `https://drive.google.com/uc?export=view&id=${imageId}`\n      }\n    };\n  });\n"
      },
      "typeVersion": 2
    },
    {
      "id": "9add85c8-dd91-4cbb-b324-4471f4839ca8",
      "name": "Sticky Note3",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1380,
        -240
      ],
      "parameters": {
        "width": 340,
        "height": 280,
        "content": "**How it works**\n\n1. Retrieves image files from a Google Drive folder.\n2. Filters and sorts files based on numeric order in the filename.\n3. Generates direct image URIs and resizes them automatically (width & height).\n4. Inserts the resized images into the target Google Docs document via API.\n5. Uses a batch loop to avoid timeouts and ensure all images are uploaded successfully."
      },
      "typeVersion": 1
    },
    {
      "id": "ff79d864-25db-40eb-9b65-2fe4a45b989d",
      "name": "Sticky Note4",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        -1380,
        100
      ],
      "parameters": {
        "color": 5,
        "width": 340,
        "height": 580,
        "content": "**Setup Steps**\n\n1. Google Drive Credential\nConnect your Google Drive account in n8n to grant access to the folder containing the images.\n2. Google Docs Credential\nConnect your Google Docs account to allow image insertion into the document.\n3. Folder & File Filter\nIn the Search File node, replace the placeholder {{YOUR_FOLDER_ID}} with your Google Drive folder ID.\n4. Google Docs Document ID\nIn the Insert Image (HTTP Request) node, replace {{YOUR_DOCUMENT_ID}} with your target Google Docs document ID.\n(Make sure you rename this node to something descriptive, e.g., Insert Image to Google Doc.)\n5. Batch Loop\nThe workflow includes a batch processing loop to prevent timeout errors when dealing with large sets of images. You can adjust the batch size if needed.\n6. Run the workflow\nExecute the workflow, and images will be automatically retrieved, resized, and inserted into the document."
      },
      "typeVersion": 1
    },
    {
      "id": "b87b0631-9508-4653-97fc-70ba845de464",
      "name": "Search File in Google Drive",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        -720,
        -140
      ],
      "parameters": {
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "list",
            "value": "1bMnN5da0iMxMPv1IgSlsRvXgdt5PGq84",
            "cachedResultUrl": "https://drive.google.com/drive/folders/1bMnN5da0iMxMPv1IgSlsRvXgdt5PGq84",
            "cachedResultName": "19 MEI 2025"
          }
        },
        "options": {},
        "resource": "fileFolder",
        "returnAll": true
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "447148b6-2a71-44e5-9c8c-a08d035ea2af",
      "name": "Resize Image",
      "type": "n8n-nodes-base.code",
      "position": [
        -500,
        220
      ],
      "parameters": {
        "jsCode": "return items.map((item, index) => {\n  const name = item.json.name || \"\"; // fallback jika tidak ada\n  const isThumbnail = name.includes(\"thumb\");\n\n  const width = isThumbnail ? 200 : 400;\n  const height = isThumbnail ? 150 : 250;\n\n  return {\n    json: {\n      requests: [{\n        insertInlineImage: {\n          uri: item.json.uri,\n          location: {\n            index: 1\n          },\n          objectSize: {\n            height: { magnitude: height, unit: 'PT' },\n            width: { magnitude: width, unit: 'PT' }\n          }\n        }\n      }]\n    }\n  }\n});\n"
      },
      "typeVersion": 2
    },
    {
      "id": "bdd774c5-4c00-4865-9356-539c3f8f18d4",
      "name": "Insert Image to Google Doc",
      "type": "n8n-nodes-base.httpRequest",
      "position": [
        40,
        240
      ],
      "parameters": {
        "url": "https://docs.googleapis.com/v1/documents/1IkozrnIqirwn4mwbHdMl25f013P59Nvd-Fw8kg_Eyw4:batchUpdate",
        "method": "POST",
        "options": {
          "timeout": 10000,
          "response": {
            "response": {
              "fullResponse": true,
              "responseFormat": "text",
              "outputPropertyName": "={{ $json.requests[0].insertInlineImage.uri }}"
            }
          }
        },
        "sendBody": true,
        "authentication": "predefinedCredentialType",
        "bodyParameters": {
          "parameters": [
            {
              "name": "requests",
              "value": "={{ $json.requests }}"
            }
          ]
        },
        "nodeCredentialType": "googleDocsOAuth2Api"
      },
      "credentials": {
        "googleDocsOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "notesInFlow": true,
      "typeVersion": 4.2
    },
    {
      "id": "f08e9f64-f715-4d55-956b-f94e3f6cb8b1",
      "name": "Batch Wait Timer-1",
      "type": "n8n-nodes-base.wait",
      "position": [
        -500,
        -140
      ],
      "parameters": {},
      "typeVersion": 1.1
    },
    {
      "id": "4e90733e-9d50-4119-a256-28422ad4c324",
      "name": "Batch Wait Timer-2",
      "type": "n8n-nodes-base.wait",
      "position": [
        -80,
        -140
      ],
      "parameters": {
        "amount": 3
      },
      "typeVersion": 1.1
    },
    {
      "id": "c5f057c2-adf2-4c9d-8b35-ed79fec70a15",
      "name": "Batch Wait Timer-3",
      "type": "n8n-nodes-base.wait",
      "position": [
        -720,
        220
      ],
      "parameters": {
        "amount": 3
      },
      "typeVersion": 1.1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "2d5fcbc0-db8f-45b7-8cc6-66ae54a4f06d",
  "connections": {
    "Resize Image": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Download File": {
      "main": [
        [
          {
            "node": "Batch Wait Timer-2",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Filtering URI": {
      "main": [
        [
          {
            "node": "Batch Wait Timer-3",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Insert Image to Google Doc",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Batch Wait Timer-1": {
      "main": [
        [
          {
            "node": "Download File",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Batch Wait Timer-2": {
      "main": [
        [
          {
            "node": "Filtering URI",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Batch Wait Timer-3": {
      "main": [
        [
          {
            "node": "Resize Image",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Insert Image to Google Doc": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search File in Google Drive": {
      "main": [
        [
          {
            "node": "Batch Wait Timer-1",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Search File in Google Drive",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}