Skip to content

Commit d1dc092

Browse files
ImTotemclaude
andcommitted
fix(workflow): add Restore Context node after Ensure Sheet
Ensure Sheet returns empty data on existing sheets (onError: continue). Restore Context code node re-injects table_name and sheet_id from Loop Over Tables so downstream nodes receive proper input. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent d535676 commit d1dc092

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

workflows/pg_sheets_sync.json

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,27 +97,37 @@
9797
"position": [1540, -100],
9898
"onError": "continueRegularOutput"
9999
},
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+
},
100110
{
101111
"parameters": {
102112
"operation": "executeQuery",
103-
"query": "=SELECT * FROM {{ $('Loop Over Tables').item.json.table_name }}",
113+
"query": "=SELECT * FROM {{ $json.table_name }}",
104114
"options": {}
105115
},
106116
"id": "pg-dump-table",
107117
"name": "Dump Table",
108118
"type": "n8n-nodes-base.postgres",
109119
"typeVersion": 2.6,
110-
"position": [1760, -100]
120+
"position": [1980, -100]
111121
},
112122
{
113123
"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 } } }];"
115125
},
116126
"id": "to-csv",
117127
"name": "Build CSV + Payload",
118128
"type": "n8n-nodes-base.code",
119129
"typeVersion": 2,
120-
"position": [1980, -100]
130+
"position": [2200, -100]
121131
},
122132
{
123133
"parameters": {
@@ -132,7 +142,7 @@
132142
"name": "Has Rows?",
133143
"type": "n8n-nodes-base.if",
134144
"typeVersion": 2.3,
135-
"position": [2200, -100]
145+
"position": [2420, -100]
136146
},
137147
{
138148
"parameters": {
@@ -149,7 +159,7 @@
149159
"name": "Paste to Sheet",
150160
"type": "n8n-nodes-base.httpRequest",
151161
"typeVersion": 4.2,
152-
"position": [2420, -200]
162+
"position": [2640, -200]
153163
}
154164
],
155165
"connections": {
@@ -160,7 +170,8 @@
160170
"Get Sheet IDs": {"main": [[{"node": "Map Sheet IDs", "type": "main", "index": 0}]]},
161171
"Map Sheet IDs": {"main": [[{"node": "Loop Over Tables", "type": "main", "index": 0}]]},
162172
"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}]]},
164175
"Dump Table": {"main": [[{"node": "Build CSV + Payload", "type": "main", "index": 0}]]},
165176
"Build CSV + Payload": {"main": [[{"node": "Has Rows?", "type": "main", "index": 0}]]},
166177
"Has Rows?": {"main": [[{"node": "Paste to Sheet", "type": "main", "index": 0}], [{"node": "Loop Over Tables", "type": "main", "index": 0}]]},

0 commit comments

Comments
 (0)