Google Sheets node evaluates Document and Sheet for the first input item only
Bug Description
In the Google Sheets node (v2, node versions 4.x), the Document and Sheet resource locators are evaluated once, against the first input item only. Every other parameter of the operation (options, filters, range, columns) is evaluated per item.
So when the Sheet field is an expression like {{ $json.sheet }} and the node receives several items, the node silently uses item 0's sheet for all of them. For Get Row(s) on node versions > 4.1, the node loops over the input items (one read per item) but every iteration reads the same tab. There is no error and no execution hint, so the wrong data goes downstream unnoticed. The expression editor preview shows the per-item values correctly, which makes this harder to spot.
Root cause (same code on master and on the 2.39.10 tag):
packages/nodes-base/nodes/Google/Sheet/v2/actions/router.tsreadsdocumentIdandsheetNamewith item index0and resolves the sheet a single time before dispatching the operation: https://github.com/n8n-io/n8n/blob/4a01756ea453bc7cbdbc869968315934f76e800b/packages/nodes-base/nodes/Google/Sheet/v2/actions/router.ts#L23-L49sheet/read.operation.tsthen loops over all input items (for node version > 4.1) with that onesheetName: https://github.com/n8n-io/n8n/blob/4a01756ea453bc7cbdbc869968315934f76e800b/packages/nodes-base/nodes/Google/Sheet/v2/actions/sheet/read.operation.ts#L179-L195- while
utils/readOperation.tsreadsoptions,filtersUI.valuesandcombineFiltersperitemIndex: https://github.com/n8n-io/n8n/blob/4a01756ea453bc7cbdbc869968315934f76e800b/packages/nodes-base/nodes/Google/Sheet/v2/actions/utils/readOperation.ts#L23-L89
This keeps coming up on the forum, where it is answered with workarounds (Split Out, Loop Over Items with batch size 1, Switch per sheet) rather than reported:
- https://community.n8n.io/t/google-sheets-get-rows-only-uses-first-dynamic-sheet-name/295060
- https://community.n8n.io/t/dynamic-expressions-wont-work-in-google-sheet-node/201479
To Reproduce
- Take a spreadsheet with two tabs that have different columns, e.g.
Channels Data(16 rows) andPosts Log(188 rows). - Build: Manual Trigger → Code node returning two items
{ sheet: 'Channels Data' }and{ sheet: 'Posts Log' }→ Google Sheets Get Row(s), Document by ID, Sheet By Name ={{ $json.sheet }}. - Execute.
Result on n8n 2.39.10, tested with node versions 4.5 and 4.6:
| Input items (in order) | Output items | Rows came from |
|---|---|---|
Channels Data, Posts Log |
32 | Channels Data only (16 rows, read twice) |
Posts Log, Channels Data |
376 | Posts Log only (188 rows, read twice) |
Swapping the order of the two input items changes which tab is read, and the second item's tab is never read in either case.
Workflow (replace the document ID and credential):
{
"nodes": [
{ "parameters": {}, "name": "When clicking ‘Execute workflow’", "type": "n8n-nodes-base.manualTrigger", "typeVersion": 1, "position": [0, 0] },
{ "parameters": { "jsCode": "return [{ json: { sheet: 'Channels Data' } }, { json: { sheet: 'Posts Log' } }];" }, "name": "Emit sheet names", "type": "n8n-nodes-base.code", "typeVersion": 2, "position": [220, 0] },
{ "parameters": { "documentId": { "__rl": true, "mode": "id", "value": "YOUR_SPREADSHEET_ID" }, "sheetName": { "__rl": true, "mode": "name", "value": "={{ $json.sheet }}" }, "options": {} }, "name": "Get rows", "type": "n8n-nodes-base.googleSheets", "typeVersion": 4.6, "position": [440, 0], "credentials": { "googleSheetsOAuth2Api": { "id": "YOUR_CREDENTIAL_ID", "name": "Google Sheets account" } } }
],
"connections": {
"When clicking ‘Execute workflow’": { "main": [[{ "node": "Emit sheet names", "type": "main", "index": 0 }]] },
"Emit sheet names": { "main": [[{ "node": "Get rows", "type": "main", "index": 0 }]] }
}
}Expected behavior
Document and Sheet should be resolved per input item like the other parameters, so the run above returns 16 rows from Channels Data followed by 188 rows from Posts Log (204 items), with paired items pointing at the right source. Same for Append, Update, Append or Update, Delete and Clear, which all go through the same router.
If per-item resolution is considered a breaking change that needs a new node version, a fallback would be to at least fail or add an execution hint when the Document or Sheet expression evaluates to different values across the input items, instead of silently using the first one.
Happy to open a PR for this if the approach is agreed.
Debug Info
Copied from a self-hosted instance (Help > About n8n is not reachable from the API, so the relevant fields are listed here):
- n8n version: 2.39.10, self hosted
- Google Sheets node versions tested: 4.5 and 4.6; the code path is unchanged on
master(4.7) - Credential type: Google Sheets OAuth2
- Execution triggered via webhook (production mode); the manual-trigger version behaves the same
Operating System
Linux (AWS EC2)
n8n Version
2.39.10
Node.js Version
Not checked (release build of 2.39.10); the behaviour is in the node code and does not depend on it
Database
Not checked; does not affect this node's behaviour
Execution mode
Not checked; does not affect this node's behaviour
Hosting
self hosted
Source: n8n-io/n8n