|
97 | 97 | "position": [1540, -100], |
98 | 98 | "onError": "continueRegularOutput" |
99 | 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 | + }, |
100 | 110 | { |
101 | 111 | "parameters": { |
102 | 112 | "operation": "executeQuery", |
103 | | - "query": "=SELECT * FROM {{ $('Loop Over Tables').item.json.table_name }}", |
| 113 | + "query": "=SELECT * FROM {{ $json.table_name }}", |
104 | 114 | "options": {} |
105 | 115 | }, |
106 | 116 | "id": "pg-dump-table", |
107 | 117 | "name": "Dump Table", |
108 | 118 | "type": "n8n-nodes-base.postgres", |
109 | 119 | "typeVersion": 2.6, |
110 | | - "position": [1760, -100] |
| 120 | + "position": [1980, -100] |
111 | 121 | }, |
112 | 122 | { |
113 | 123 | "parameters": { |
114 | | - "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, 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 } } }];" |
| 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 } } }];" |
115 | 125 | }, |
116 | 126 | "id": "to-csv", |
117 | 127 | "name": "Build CSV + Payload", |
118 | 128 | "type": "n8n-nodes-base.code", |
119 | 129 | "typeVersion": 2, |
120 | | - "position": [1980, -100] |
| 130 | + "position": [2200, -100] |
121 | 131 | }, |
122 | 132 | { |
123 | 133 | "parameters": { |
|
132 | 142 | "name": "Has Rows?", |
133 | 143 | "type": "n8n-nodes-base.if", |
134 | 144 | "typeVersion": 2.3, |
135 | | - "position": [2200, -100] |
| 145 | + "position": [2420, -100] |
136 | 146 | }, |
137 | 147 | { |
138 | 148 | "parameters": { |
|
149 | 159 | "name": "Paste to Sheet", |
150 | 160 | "type": "n8n-nodes-base.httpRequest", |
151 | 161 | "typeVersion": 4.2, |
152 | | - "position": [2420, -200] |
| 162 | + "position": [2640, -200] |
153 | 163 | } |
154 | 164 | ], |
155 | 165 | "connections": { |
|
160 | 170 | "Get Sheet IDs": {"main": [[{"node": "Map Sheet IDs", "type": "main", "index": 0}]]}, |
161 | 171 | "Map Sheet IDs": {"main": [[{"node": "Loop Over Tables", "type": "main", "index": 0}]]}, |
162 | 172 | "Loop Over Tables": {"main": [[], [{"node": "Ensure Sheet", "type": "main", "index": 0}]]}, |
163 | | - "Ensure Sheet": {"main": [[{"node": "Dump Table", "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}]]}, |
164 | 175 | "Dump Table": {"main": [[{"node": "Build CSV + Payload", "type": "main", "index": 0}]]}, |
165 | 176 | "Build CSV + Payload": {"main": [[{"node": "Has Rows?", "type": "main", "index": 0}]]}, |
166 | 177 | "Has Rows?": {"main": [[{"node": "Paste to Sheet", "type": "main", "index": 0}], [{"node": "Loop Over Tables", "type": "main", "index": 0}]]}, |
|
0 commit comments