Question about comparing column of a google sheet with an array of strings

What I have already:
I’ve a workflow that gets from emails, the invoices that come attached, from where it contains a table of rows that is related with document numbers that were paid.

Objective:
And after doing that, I want to compare these with an actual google sheet that has a list of document numbers that need to be verified and marked as paid on a dedicated column of the same sheet per document number, once the automation compares what came and matches the same document numbers.

Issue:
I really dont know how can I compare both batches of data and even how to update the corresponded cell on google sheet, under that column that is dedicated to show if was paid or not.

How I was trying to compare but for some reason loop is not running for those 4 items, it does for first two and simply stops:


Google sheet table:

What I got from the invoice:

So do you know what I am missing with that loop that only runs 2 times from a 4 item array? And what do you advise in general knowing my objective and looking to what I have now?

Hello @marafado88! The reason this is happening is because it looks like it only sends it to 1 input out of the 2 inputs available. You can acheive and fix this by using a merge node, which i often use to ensure i get everything i need to the node.

You can also use a code node instead of the compare datasets node like so and a merge node instead of seperating the inputs for a compare dataset node -

// Get the document numbers extracted from the invoice
const invoiceItems = $(“Invoice Extract”).all();
const invoiceNumbers = invoiceItems.map(item => item.json[“Nº Documento”].trim());
// Get all rows from the Google Sheet
const sheetRows = $(“Google Sheets”).all();
// Compare and mark matches
const updates = ;
for (const row of sheetRows) {
const docNum = String(row.json[“Nº Documento”]).trim();
if (invoiceNumbers.includes(docNum)) {
updates.push({
json: {
“Nº Documento”: docNum,
“Pago”: “Sim”,
// include row_number if needed for updating
“row_number”: row.json[“row_number”]
}
});
}
}
return updates;

Just ensure you change the code to fit your workflow.

2 Likes

Hi @marafado88, welcome to the n8n community :tada:! From what I can see, the loop is not stopping, it is running in batches, which is why you only see two runs even though you have four items. In n8n, in cases where nodes already run once per item, it is usually not recommended to use Loop Over Items. The simplest solution is to remove the extra loops, keep the paid document numbers as an array, check each sheet row against that array with an IF or a Merge by key, and then update the row using the column to match on in the Google Sheets Update Row node.

1 Like

Hi @marafado88,

Are you sure you need the loops?!

From what I understand from the screenshots, you’re already extracting an array of strings,
So you could split that out and then use it as a key to match against the Google Sheets table,

I can show you how to do this if you provide some sample data and a minimal version of the workflow to reproduce the idea..

1 Like

Thank you a lot for the help!

Thanks to AI I got your script a bit more refined to my case, and it worked like a charm, even getting the row number!

Your welcome @marafado88 Feel free to mark one of any of the replies in this thread as the solution! Have a great day! If you have any more questions, I’m free to help!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.