Last active
March 16, 2020 19:20
-
-
Save kuovonne/1c38ca3bf87c5656af59381b14f46398 to your computer and use it in GitHub Desktop.
getRecordsAsList - helper function for Airtable scripting block
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
Title: getRecordsAsList - helper function for Airtable scripting block | |
Author: Kuovonne Vorderbruggen | |
Date Created: March 16, 2020 | |
Copyright (c) 2020 by Kuovonne Vorderbruggen | |
License: MIT License | |
** Descripton ** | |
This function gets the records and cell values from atable and | |
makes the cell values accessible with dot notation. | |
** Parameters ** | |
table: an Airtable API table object | |
fieldNames: an array of field names in the table whose values are returned | |
sortFieldName: optional, a field for sorthing the records | |
sortDirection: optional, the direction for the sort | |
** Example usage of resulting array ** | |
let records = await getRecordsAsList(table, fieldNames); | |
recordId = records[index].id; | |
recordName = records[index].name; | |
fieldValue = records[index].fields[fieldName]; | |
******************************************************************************* | |
*/ | |
async function getRecordsAsList(table, fieldNames, sortFieldName = null, sortDirection = "asc") { | |
// set the options for the query | |
let queryOptions = { fields: fieldNames }; | |
if (sortFieldName) { | |
queryOptions["sorts"] = [{field: sortFieldName, direction: sortDirection}] | |
} | |
// run the query | |
let queryResult = await table.selectRecordsAsync(queryOptions); | |
// get the cell values | |
let recordsWithFieldValues = queryResult.records.map((record)=> { | |
let newRecord = { id: record.id, | |
name: record.name, | |
fields: {}, | |
}; | |
for (let fieldName of fieldNames) { | |
newRecord.fields[fieldName] = record.getCellValue(fieldName); | |
} | |
return newRecord; | |
}); | |
return recordsWithFieldValues; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment