- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎Sep 12, 2022 09:38 AM
I am trying to get a sum total of a column in an automation that will send out an email showing the new total every time an amount is added. I was tasked to create an automation running a script, but I am very new to script writing. Any help would be most appreciated
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎Dec 12, 2022 03:11 PM
This mornings coffee break, I created this example using a Scripting Extension - once you have an understanding of this, we can then talk about how to gear it within an Automation Script (which will be a very similar exercise, with minor differences).
The key to this is using the Javascript reduce() method. I've also demonstrated how you can filter the data using a filter() method, before the data is summed via reduce().
const emailTable = base.getTable("Numbers Table");
let records = await emailTable.selectRecordsAsync({
fields: ["Fruit", "Total Mass"]
});
console.info(records);
let initialValue = 0;
let sumMass = records.records
.filter(
(record) =>
record.name !== "Green Apple"
)
.reduce((accumulator, record) => record.getCellValue("Total Mass") + accumulator, initialValue);
console.log(sumMass);
Here, we can see the data that's been filtered, then reduced - and the final total;