Sum values of a Sheets Column
Hi community! As a lot of people I’m not a coder myself and whenever I need help I come to the community to find the codes I need.
However with this new update the codes I’m using are not working anymore.
I have a Google Sheets node Look up a specific sheet to get all order values and the goal is to have the sum of these values for the date I used when looking it up.
I’m already getting the order values for the specific date, but I have no idea how to sum them up in this new code node (I wouldn’t know in the previous version as well )
Thank you guys for the support.
Information on n8n setup
- n8n version:0.200.1
- Running n8n via [Docker]:
Hi @Lucas_Lopes, welcome to the community
So, you would like to simply sum up all the values in the order_total
column returned by the lookup operation on your Google Sheets node? This is doable, though n8n does not have a “Calculation” node, so requires a little bit of code.
Here’s a quick example:
I’ve tried to keep the code as readable as possible, here’s what it does:
- In the first line, we’re simply setting a FIELD_NAME for later re-use. This keeps the code re-usable, if you want to sum up another field simply replace
order_total
with your new field name.
-
let result = 0;
defines a result
variable with a value of 0. We can then add each of the field values to this.
-
for (item of $input.all()) { }
simply loops through each input item
-
result = result + parseFloat(item.json.order_total);
adds the value of the respective field from our current loop item to our result
. parseFloat() is there just in case the incoming data doesn’t have a numeric data type yet.
- Finally,
return [{ total: result }];
returns our result in a new field called total
.
Hope this helps! Let me know if you have any questions on this.
1 Like