Aug 10, 2022 11:21 AM
I am trying to create a script in Airtable to give me the sum of a row in a table, but I am new to JavaScript. Any help would be most appreciated, thank you
Aug 10, 2022 11:29 AM
Hi @Ryan_Mizak,
I think you are asking for a formula to put in a formula type field.
In that case, the formulas are fairly simlpe.
If you have two number type fields, Number 1 and Number 2. Then add a formula type field and place this in it:
SUM({Number 1},{Number 2})
Aug 11, 2022 10:43 AM
Thanks. i am actually trying to create a script that sends out an email showing the contents of a column and that also create a sum of all the items in one column. I have a column named Commitment$ and I would need a sum of all the $
Aug 11, 2022 11:01 AM
A very basic script that gets the total of an entire column from a Table would be something like this:
let table = base.getTable("Name of Table")
let field = table.getField("Commitment$")
let query = await table.selectRecordsAsync({fields: [field]})
let records = query.records
let totalSum = records.reduce(getTotalSum, 0)
function getTotalSum(total, x) {
return total + Number(x.getCellValue(field))
}
If this is an automation script, you probably want to output the sum variable into another step in your automation. You’d do that by adding this to the end:
output.set('totalSum', totalSum )
Aug 11, 2022 11:02 AM
Hi @Ryan_Mizak,
I think I commented this on another thread you made.
To Sum an array
var array = [1, 2, 3, 4, 5];
// Getting sum of numbers
var sum = array.reduce(function(a, b){
return a + b;
}, 0);
console.log(sum); // Prints: 15
Aug 11, 2022 11:42 AM
If you want a script that sums the numbers in a column for only the results of a “find records” action (versus the entire table), my Automation Helpers bundle includes such a script.
Note that this script is a variation on the script posted by Kamille so if Kamille’s script works for you, or if you feel comfortable modifying Kamille’s script, you have no need to buy the automation helpers.
However, if you want a quick solution without having to learn any coding, they can be a good option. All the inputs are set via input variables, and no coding skills are necessary. (Or if you want to learn coding, these scripts in this bundle can be helpful starting points.)
Aug 11, 2022 01:00 PM
Thanks. Do you know if I’d be able to get this from a personal view?
Aug 11, 2022 02:17 PM
Yes, a personal view should work as well. To do that, add :
let view = table.getView("Name of View")
just below the first line, and change the query line to
let query = await view.selectRecordsAsync({fields: [field]})
Aug 12, 2022 07:15 PM
Ive tried and nothing is coming up in the emails when I run the test
Aug 13, 2022 04:45 PM
That’s the first mention of an email, so post screenshots of your automation set up to figure out what’s wrong.