Mar 23, 2023 12:34 PM
I'm attempting to read the current status of a single select field, what I'd like my script to do is read the status of the button and simply return that status as a string. For example, if I manually set the button to "Doing" and run the script, my script should return "Doing" from the console.
My airable is setup with a single table called "Item Status" that lists all the records in the table. Each record has a "Current Status" field, a single select field with "To Do", "Open", and "In Progress"
// read the current status of a record
let table = base.getTable("Item Status");
let queryResult = await table.selectRecordsAsync({fields: ["Current Status"]});
console.log(queryResult);
let record = queryResult.records[0];
console.log(record.getCellValue("Current Status"));
The problem is when I run this script it returns more data then I want.
CONSOLE.LOG
{id: "selkMEZY58GC8AUfc", name: "Open", color: "blueLight2"}
id: "selkMEZY58GC8AUfc"
name: "Open"
color: "blueLight2"
I don't need to know the id, or color, I'd prefer for now to only return the string "Open" if the button is set to open. I understand that each type of cell has a typeDef but I don't understand how to read from this typedef
TYPEDEF
{
choices: Array<{
id: string,
name: string,
color?: string,
}>,
}
This is part of a simple project where I'm trying to record the status of each record and keep a log of when it was updated in order to track how long each record is in each status.
Solved! Go to Solution.
Mar 23, 2023 01:41 PM - edited Mar 23, 2023 01:43 PM
const table = base.getTable("Item Status");
const records = await table.selectRecordsAsync({
fields: ["Current Status"]
})
.then(query => query.records);
let record = records[0];
console.log(record.getCellValueAsString("Current Status"));
console.log(record.getCellValue("Current Status")?.name);
Single select fields return a choice object containing the data of the current single select value (assuming one is present.)
{
id: string,
name: string,
color?: string,
}
Each single select choice has three properties: the unique ID of that particular option, the string value that is displayed in the actual field, and an optional color property if you have colored options enabled.
To get the string value, you can use the record.getCellValueAsString() method.
You can similarly use the record.getCellValue() method, but you will need to access the name property.
Here's an example:
/*
In a single select field, we have the following option selected: "Choice 1".
(Option coloring is set to off.)
*/
let singleSelectFieldValue = record.getCellValue("yourField");
/*
The value of singleSelectFieldValue is:
{
id: "idString",
name: "Choice 1"
}
*/
let valueAsString = record.getCellValueAsString("yourField");
// The value of valueAsString is: "Choice 1".
Mar 23, 2023 01:41 PM - edited Mar 23, 2023 01:43 PM
const table = base.getTable("Item Status");
const records = await table.selectRecordsAsync({
fields: ["Current Status"]
})
.then(query => query.records);
let record = records[0];
console.log(record.getCellValueAsString("Current Status"));
console.log(record.getCellValue("Current Status")?.name);
Single select fields return a choice object containing the data of the current single select value (assuming one is present.)
{
id: string,
name: string,
color?: string,
}
Each single select choice has three properties: the unique ID of that particular option, the string value that is displayed in the actual field, and an optional color property if you have colored options enabled.
To get the string value, you can use the record.getCellValueAsString() method.
You can similarly use the record.getCellValue() method, but you will need to access the name property.
Here's an example:
/*
In a single select field, we have the following option selected: "Choice 1".
(Option coloring is set to off.)
*/
let singleSelectFieldValue = record.getCellValue("yourField");
/*
The value of singleSelectFieldValue is:
{
id: "idString",
name: "Choice 1"
}
*/
let valueAsString = record.getCellValueAsString("yourField");
// The value of valueAsString is: "Choice 1".
Mar 24, 2023 12:34 AM
Each week, runners in the Run group I manage log in to Airtable using a form entry from a "Check-Ins" table. This form retrieves information (names) from a more comprehensive "Runner List" that records the login histories of all registered runners. The "Runner List" master sheet, which totals the number of runs each individual has attended, is synced back to the "Check-Ins" table, which keeps account of the weekly login for each run. I want to build a new table that displays the number of new runners that appear each week.
Mar 24, 2023 07:06 AM
Thanks Ben, I'm not sure if this is the right answer but I think this might be one step closer to a solution.