Airscript Learn to use Airtable scripts

Displaying data from your base

Many Airtable scripts will:

  • read some data in one of the base tables
  • do something with the data (manipulate it, aggregate it etc)
  • write it back to the table (or another table)

Let’s pick on the first of these steps: we’ll read some data from the base and display it on the output window.

Our table is a list of people - names, email addresses and phone numbers:

Open up your scripting block and clear out any script that’s there (of course, save it somewhere if it is a particular favourite 💾).

A script is, of course, always contained in a base, so we don’t need to tell the script the details of the base. We can see the sort of information about the base that Airtable gives us by logging the base object to the output. Run this script:

console.log(base);

You’ll see something like this in the output window.

The output is an object - a JavaScript data type for holding data. The object contains “key/value” pairs and the values can be strings, numbers, arrays and other data types. Expand the object by clicking on the black arrows and take a look at the data stored in the object.

We can display and use different elements of the object by specifying the key we want to access. Change your script to this and execute it again.

console.log(base.name);

You should see your base name in the output window.

Let’s get into the purpose of our script - displaying data from our base. The steps our script will take are:

  • configure the table in the base we are going to query
  • query the table
  • do something with the records returned by the query

Type this code into the code window (always better to type, than copy and paste 😉)


let table = base.getTable('People');
let query = await table.selectRecordsAsync();
for (let record of query.records) {
    console.log(record);
}

Let’s walk through the code line by line.

First we configure the table we want to access. We use the method:

base.getTable('MY TABLE NAME')

and set the result of this to be a new variable we’re calling “table” (but you can call it anything you want).

let table = base.getTable('People');

Next, we query the configured table using the method:

table.selectRecordsAsync();

You’ll notice that this is prefixed, in the script above, with the keyword await. If you are interested, you can read more about await (and its partner async) here, but it is a bit of an advanced topic, so feel free to bypass this for now and just know that you need to put await in front of the command to select records from a table.

Having queried the table, we can now view the results. We can use the method query.records to give us the records of query. query.records is an array, so, whilst we can print this out as it is using:

console.log(query.records)

it is much more useful to iterate through the array and print each iteration. We do this with a for loop:


for (let record of query.records) {
    console.log(record);
}

This code:

let record of query.records

defines a new variable record (again you can call this variable anything you want). We use this variable to represent the record we access on every iteration of the loop. So, our for loop code is now saying “for each record of the array query.records, log the record”.

If you run all of the code you will get something like this (your data might be different of course):

What we see here is the unique record ID in Airtable and the record name (the value or text in the table primary field). What we don’t see, however, is each record’s email and phone number.

We can use the method

record.getCellValue('MY FIELD NAME')

to access any field within the table, so let’s modify our script to display these values too.

For each record, we’ll assign the cell value of each field to a new variable, then we’ll log the new variables.


let table = base.getTable('People');
let query = await table.selectRecordsAsync();
for (let record of query.records) {
    let name = record.getCellValue('Name');
    let email = record.getCellValue('Email');
    let phone = record.getCellValue('Phone');
    console.log(name, email, phone);
}

Run the script again and your output should look like this:

Recap

  • Configuring the table we want to query
  • Querying the table
  • Getting the records array from the query
  • Looping through the records array and doing something with each record
  • Getting the cell values for each record and displaying them