|
68 | 68 | }, |
69 | 69 | { |
70 | 70 | "parameters": { |
71 | | - "jsCode": "const sheetsResponse = $('Get Sheet IDs').first().json;\nconst sheetMap = {};\nfor (const s of sheetsResponse.sheets) {\n sheetMap[s.properties.title] = s.properties.sheetId;\n}\n\nconst tables = $('Any Changes?').all().map(i => i.json.table_name);\nconst result = tables.map(t => ({\n json: { table_name: t, sheet_id: sheetMap[t] ?? null, exists: sheetMap[t] != null }\n}));\nreturn result;" |
| 71 | + "jsCode": "const sheetsResponse = $('Get Sheet IDs').first().json;\nconst sheetMap = {};\nfor (const s of sheetsResponse.sheets) {\n sheetMap[s.properties.title] = s.properties.sheetId;\n}\n\nconst tables = $('Any Changes?').all().map(i => i.json.table_name);\nconst result = tables.map(t => ({\n json: { table_name: t, sheet_id: sheetMap[t] ?? null }\n}));\nreturn result;" |
72 | 72 | }, |
73 | 73 | "id": "map-sheet-ids", |
74 | 74 | "name": "Map Sheet IDs", |
|
84 | 84 | "typeVersion": 3, |
85 | 85 | "position": [1320, -100] |
86 | 86 | }, |
87 | | - { |
88 | | - "parameters": { |
89 | | - "operation": "create", |
90 | | - "documentId": {"__rl": true, "mode": "id", "value": "={{ $env.GOOGLE_SHEETS_ID }}"}, |
91 | | - "title": "={{ $json.table_name }}" |
92 | | - }, |
93 | | - "id": "sheets-create", |
94 | | - "name": "Ensure Sheet", |
95 | | - "type": "n8n-nodes-base.googleSheets", |
96 | | - "typeVersion": 4.7, |
97 | | - "position": [1540, -100], |
98 | | - "onError": "continueRegularOutput" |
99 | | - }, |
100 | | - { |
101 | | - "parameters": { |
102 | | - "jsCode": "return [{ json: { table_name: $('Loop Over Tables').item.json.table_name, sheet_id: $('Loop Over Tables').item.json.sheet_id } }];" |
103 | | - }, |
104 | | - "id": "passthrough", |
105 | | - "name": "Restore Context", |
106 | | - "type": "n8n-nodes-base.code", |
107 | | - "typeVersion": 2, |
108 | | - "position": [1760, -100] |
109 | | - }, |
110 | 87 | { |
111 | 88 | "parameters": { |
112 | 89 | "operation": "executeQuery", |
|
117 | 94 | "name": "Dump Table", |
118 | 95 | "type": "n8n-nodes-base.postgres", |
119 | 96 | "typeVersion": 2.6, |
120 | | - "position": [1980, -100] |
| 97 | + "position": [1540, -100] |
121 | 98 | }, |
122 | 99 | { |
123 | 100 | "parameters": { |
124 | | - "jsCode": "const rows = $input.all().map(i => i.json);\nconst tableName = $('Restore Context').first().json.table_name;\nlet sheetId = $('Restore Context').first().json.sheet_id;\n\nif (rows.length === 0) {\n return [{ json: { _empty: true, table_name: tableName } }];\n}\n\nconst headers = Object.keys(rows[0]);\nconst csvLines = [headers.join(',')];\nfor (const row of rows) {\n const line = headers.map(h => {\n const v = String(row[h] ?? '');\n if (v.includes(',') || v.includes('\"') || v.includes('\\n')) {\n return '\"' + v.replace(/\"/g, '\"\"') + '\"';\n }\n return v;\n }).join(',');\n csvLines.push(line);\n}\n\nconst requests = [];\nif (sheetId != null) {\n requests.push({ updateCells: { range: { sheetId: sheetId }, fields: 'userEnteredValue' } });\n}\nrequests.push({\n pasteData: {\n coordinate: { sheetId: sheetId ?? 0, rowIndex: 0, columnIndex: 0 },\n data: csvLines.join('\\n'),\n type: 'PASTE_NORMAL',\n delimiter: ','\n }\n});\n\nreturn [{ json: { _empty: false, table_name: tableName, body: { requests } } }];" |
| 101 | + "jsCode": "const rows = $input.all().map(i => i.json);\nconst tableName = $('Loop Over Tables').item.json.table_name;\nlet sheetId = $('Loop Over Tables').item.json.sheet_id;\n\nif (rows.length === 0) {\n return [{ json: { _empty: true } }];\n}\n\nfunction hashCode(s) {\n let h = 0;\n for (let i = 0; i < s.length; i++) h = ((h << 5) - h) + s.charCodeAt(i) | 0;\n return Math.abs(h);\n}\n\nconst headers = Object.keys(rows[0]);\nconst csvLines = [headers.join(',')];\nfor (const row of rows) {\n csvLines.push(headers.map(h => {\n const v = String(row[h] ?? '');\n return (v.includes(',') || v.includes('\"') || v.includes('\\n')) ? '\"' + v.replace(/\"/g, '\"\"') + '\"' : v;\n }).join(','));\n}\n\nconst requests = [];\n\nif (sheetId == null) {\n sheetId = hashCode(tableName);\n requests.push({ addSheet: { properties: { sheetId, title: tableName } } });\n}\nrequests.push({ updateCells: { range: { sheetId }, fields: 'userEnteredValue' } });\nrequests.push({ pasteData: { coordinate: { sheetId, rowIndex: 0, columnIndex: 0 }, data: csvLines.join('\\n'), type: 'PASTE_NORMAL', delimiter: ',' } });\n\nreturn [{ json: { _empty: false, body: { requests } } }];" |
125 | 102 | }, |
126 | 103 | "id": "to-csv", |
127 | 104 | "name": "Build CSV + Payload", |
128 | 105 | "type": "n8n-nodes-base.code", |
129 | 106 | "typeVersion": 2, |
130 | | - "position": [2200, -100] |
| 107 | + "position": [1760, -100] |
131 | 108 | }, |
132 | 109 | { |
133 | 110 | "parameters": { |
|
142 | 119 | "name": "Has Rows?", |
143 | 120 | "type": "n8n-nodes-base.if", |
144 | 121 | "typeVersion": 2.3, |
145 | | - "position": [2420, -100] |
| 122 | + "position": [1980, -100] |
146 | 123 | }, |
147 | 124 | { |
148 | 125 | "parameters": { |
|
159 | 136 | "name": "Paste to Sheet", |
160 | 137 | "type": "n8n-nodes-base.httpRequest", |
161 | 138 | "typeVersion": 4.2, |
162 | | - "position": [2640, -200] |
| 139 | + "position": [2200, -200] |
163 | 140 | } |
164 | 141 | ], |
165 | 142 | "connections": { |
|
169 | 146 | "Any Changes?": {"main": [[{"node": "Get Sheet IDs", "type": "main", "index": 0}], []]}, |
170 | 147 | "Get Sheet IDs": {"main": [[{"node": "Map Sheet IDs", "type": "main", "index": 0}]]}, |
171 | 148 | "Map Sheet IDs": {"main": [[{"node": "Loop Over Tables", "type": "main", "index": 0}]]}, |
172 | | - "Loop Over Tables": {"main": [[], [{"node": "Ensure Sheet", "type": "main", "index": 0}]]}, |
173 | | - "Ensure Sheet": {"main": [[{"node": "Restore Context", "type": "main", "index": 0}]]}, |
174 | | - "Restore Context": {"main": [[{"node": "Dump Table", "type": "main", "index": 0}]]}, |
| 149 | + "Loop Over Tables": {"main": [[], [{"node": "Dump Table", "type": "main", "index": 0}]]}, |
175 | 150 | "Dump Table": {"main": [[{"node": "Build CSV + Payload", "type": "main", "index": 0}]]}, |
176 | 151 | "Build CSV + Payload": {"main": [[{"node": "Has Rows?", "type": "main", "index": 0}]]}, |
177 | 152 | "Has Rows?": {"main": [[{"node": "Paste to Sheet", "type": "main", "index": 0}], [{"node": "Loop Over Tables", "type": "main", "index": 0}]]}, |
|
0 commit comments