Format Google Doc via HTML in N8N – AI Automation

Workflow JSON Code:

{
  "name": "Format Google Doc Via HTML",
  "nodes": [
    {
      "parameters": {
        "modelId": {
          "__rl": true,
          "value": "gpt-4.1-mini",
          "mode": "list",
          "cachedResultName": "GPT-4.1-MINI"
        },
        "responses": {
          "values": [
            {
              "content": "=Create a proposal for Shopify Store development. Client is looking to work with someone who can help me set up a clean, minimal Shopify store connected with Printify for a small Christian apparel brand.\n\n1. Use <h3> tags for Main Headings (e.g., <h3>SUMMARY</h3>).\n2. Use <ul> and <li> tags for bullet points.\n3. Use <b> for bold text (like timestamps or key phrases).\n4. Use <hr> for dividers.\n6. Return only the raw HTML string. Do not use markdown formatting, do not use code blocks, and do not include the ```html wrapper.\n"
            }
          ]
        },
        "builtInTools": {},
        "options": {}
      },
      "type": "@n8n/n8n-nodes-langchain.openAi",
      "typeVersion": 2,
      "position": [
        -144,
        -32
      ],
      "id": "1293cc8e-b8d0-4271-9c90-1d99689ddfa0",
      "name": "Message a model",
      "credentials": {
        "openAiApi": {
          "id": "7bkdYwjP8QAqF7O6",
          "name": "OpenAi account"
        }
      }
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://www.googleapis.com/upload/drive/v3/files",
        "authentication": "predefinedCredentialType",
        "nodeCredentialType": "googleDriveOAuth2Api",
        "sendQuery": true,
        "queryParameters": {
          "parameters": [
            {
              "name": "uploadType",
              "value": "multipart"
            },
            {
              "name": "supportsAllDrives",
              "value": "true"
            }
          ]
        },
        "sendBody": true,
        "contentType": "raw",
        "rawContentType": "multipart/related; boundary=divider",
        "body": "={{ $json.rawData }}",
        "options": {}
      },
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        640,
        -32
      ],
      "id": "5a88a088-cfaf-47da-8b58-5b8e17336434",
      "name": "create-google-doc",
      "notesInFlow": true,
      "credentials": {
        "googleDriveOAuth2Api": {
          "id": "rmLddus1BEwVHyRF",
          "name": "Google Drive account"
        }
      }
    },
    {
      "parameters": {
        "jsCode": "const boundary = 'divider';\nconst docName = $input.first().json.document_name;\nconst folderId = $input.first().json.drive_folder_id;\nconst htmlContent = $input.first().json.html_content;\n\nconst metadata = JSON.stringify({\n  name: docName,\n  mimeType: \"application/vnd.google-apps.document\",\n  parents: [folderId]\n});\n\nconst htmlWithStyles = `\n<!DOCTYPE html>\n<html>\n<head>\n  <meta charset=\"UTF-8\">\n  <style>\n    /* Add bottom margin to block elements for spacing */\n    p,\n    ul,\n    ol,\n    table,\n    h1,\n    h2,\n    h3,\n    h4,\n    h5,\n    h6 {\n      margin-bottom: 10pt;\n    }\n\n    h2 {\n      margin-top: 20pt;\n    }\n\n    /* Prevent margin collapse issues or excessive space inside lists */\n    li {\n       margin-bottom: 2pt; /* Optional: small space between list items */\n    }\n\n    /* Remove margin from the last child within common containers if needed */\n    /* This might be overly aggressive, test without it first */\n    /*\n    body > *:last-child,\n    li > *:last-child {\n       margin-bottom: 0;\n    }\n    */\n  </style>\n</head>\n<body>\n  ${htmlContent}\n</body>\n</html>\n`;\n\n// Construct the body with literal \\r\\n ONLY\nlet body = `--${boundary}\\r\\n`;\nbody += `Content-Type: application/json; charset=UTF-8\\r\\n`;\nbody += `\\r\\n`; // Blank line\nbody += `${metadata}\\r\\n`;\nbody += `--${boundary}\\r\\n`;\nbody += `Content-Type: text/html\\r\\n`;\nbody += `\\r\\n`; // Blank line\nbody += `${htmlWithStyles}\\r\\n`; // Add the HTML content\nbody += `--${boundary}--\\r\\n`; // Final boundary\n\nreturn {\n  rawData: body \n};"
      },
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        400,
        -32
      ],
      "id": "2c8c65e6-f657-4462-ac0c-44a12d33cfc0",
      "name": "prepare-request"
    },
    {
      "parameters": {
        "assignments": {
          "assignments": [
            {
              "id": "e7ee03ac-13e3-4fca-a7bc-57c8fc56dc42",
              "name": "document_name",
              "value": "=Test Style Doc 2",
              "type": "string"
            },
            {
              "id": "48a07ef2-ae46-4bfc-aa7e-d92a74ef46d6",
              "name": "html_content",
              "value": "={{ $json.output[0].content[0].text }}",
              "type": "string"
            },
            {
              "id": "22b02fba-ba72-423a-b92f-1191a183a554",
              "name": "drive_folder_id",
              "value": "=1ba-muFiBGMQnNTg-emD1sykvKDhK5nae",
              "type": "string"
            }
          ]
        },
        "options": {}
      },
      "type": "n8n-nodes-base.set",
      "typeVersion": 3.4,
      "position": [
        192,
        -32
      ],
      "id": "36c99eee-9ce3-4526-9c45-00b9c6362eb2",
      "name": "set_fields",
      "notesInFlow": true
    },
    {
      "parameters": {},
      "type": "n8n-nodes-base.manualTrigger",
      "typeVersion": 1,
      "position": [
        -384,
        -32
      ],
      "id": "71c06bbc-2167-450b-b735-7aa6133c8ddd",
      "name": "When clicking ‘Execute workflow’"
    }
  ],
  "pinData": {},
  "connections": {
    "Message a model": {
      "main": [
        [
          {
            "node": "set_fields",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "create-google-doc": {
      "main": [
        []
      ]
    },
    "prepare-request": {
      "main": [
        [
          {
            "node": "create-google-doc",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "set_fields": {
      "main": [
        [
          {
            "node": "prepare-request",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking ‘Execute workflow’": {
      "main": [
        [
          {
            "node": "Message a model",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "47ce3278-8a8f-4cca-b538-f8360cc96a2e",
  "meta": {
    "templateCredsSetupCompleted": true,
    "instanceId": "d4624db7a3a7e9da97f0a286f3b55ac8518307cf24e917b7a02124c1506facab"
  },
  "id": "7NYpxgz4hhyWUP3O",
  "tags": []
}
5/5 - (5 votes)

About

Leave a Comment

Your email address will not be published. Required fields are marked *