Help

Re: AirTable Script

2681 3
cancel
Showing results for 
Search instead for 
Did you mean: 
Ryan_Mizak
4 - Data Explorer
4 - Data Explorer

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

9 Replies 9

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})

image

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 $

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 )

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

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.)

Thanks. Do you know if I’d be able to get this from a personal view?

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]})

Ive tried and nothing is coming up in the emails when I run the test

That’s the first mention of an email, so post screenshots of your automation set up to figure out what’s wrong.