Created
March 16, 2020 19:01
-
-
Save kuovonne/098e319d513d1a57dc357f4bbaa70606 to your computer and use it in GitHub Desktop.
getRecordsAsHash - 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: getRecordsAsHash - 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 specified field values for all the records in the table | |
and then stores them in a hash table, with the record id as the key. | |
This provides very quick lookup of field values based on record id. | |
** Parameters ** | |
table: a scripting API table object | |
fieldNames: an array of field names for the given table | |
** Example usage of resulting hash table ** | |
let recordsHash = await getRecordsAsHash(table, fieldNames); | |
let fieldValue = recordsHash[recordId][fieldName]; | |
******************************************************************************* | |
*/ | |
async function getRecordsAsHash(table, fieldNames) { | |
// get the records with just the desired fields | |
let queryResult = await table.selectRecordsAsync({ fields: fieldNames }); | |
// create the hash and fill it with the values (including any nulls); | |
let recordHash = {}; | |
queryResult.records.forEach((record)=> { | |
let fieldValues = {}; | |
for (let fieldName of fieldNames) { | |
fieldValues[fieldName] = record.getCellValue(fieldName); | |
} | |
recordHash[record.id] = fieldValues; | |
}); | |
return recordHash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment