Looping through Google Sheets Inputs

I can’t seem to get Google Sheets to loop through outputs from a Notion getAll function:

Here Notion returns 29 items, but only the first item is processed by Google Sheets? Trying to get all 29 items processed by Google Sheets and then all 29 to again by sent through to Notion.

Would be very grateful for what can be done to fix this! Please see below for the workflow,

Thanks!

Hey @pb84, I can see why you’d be surprised by n8n’s behaviour here. Most nodes run their action for each item they receive, but there are a few exceptions which mostly concern database/spreadsheet nodes (where a single action typically fetches all required data). These are listed here.

That said, you can still build the loop you have in mind, this is described on the doc page linked above and would look like so:

Hope this helps! Give me a shout if you run into any trouble with this.

1 Like

Brilliant - This is exactly what I was trying to do, but I was incorrectly using the SplitInBatches module. Thank you again, the support on this forum is brilliant and I will be bringing as many people as I know to n8n!

Very quickly, two follow-up questions:

  1. Can one set the batch size dynamically to match the number of outputs from Notion; and
  2. How do you correctly signal to n8n once all batches are done, move on and complete more modules (after Notion1)
1 Like

Awesome, glad to hear this helped already!

As for your questions:

  1. The batch size would need to be 1 in this example for the Google Sheet node to run for every single item. Otherwise the Google Sheet node would run only once per batch.
  2. To check if you’re done looping you can use the {{$node["SplitInBatches"].context["noItemsLeft"]}} expression in an IF node:

Fantastic - this is perfect. Thank you again @MutedJam !

1 Like

Another question, lets say that we stop the loop at Google Sheets, but we want to aggregate together into a json string the output from each Google Sheet look-up, how might we do that?

I’ve had great success with the the List module outside of the SplitInBatches environment, but not sure how to position it when working with SplitInBatches

So this part is a bit more tricky because n8n doesn’t have built/in logic for such a transformation.

So in a first step you’d need to merge all separate executions back together into a single one with a custom Function. Afterwards you can use the Item Lists node again.

Here’s an example workflow doing that:

You should be able to copy the Merge Execution Data Function node from the example and use it in your own workflow, you’d just need to replace the name of the node in there (with Google Sheets if you want the Google Sheets items):

Afterwards you’d have one execution with many items again instead of many executions with one item:

Ah okay - so although we have 3 items after the function module (Merge Execution Data), using the list module again will aggregate them back into one?

Yes exactly, after the Merge Execution Data Function node in the example you can use the Item Lists node like you could without the loop, for example like so:

Is that what you had in mind?

Exactly - thanks again, so helpful!