I'm working with Google sheets and script to write functions that look up data from a table and return the records as a hash. There may be a better way of doing this, but right now, I'm trying to return an array of record hashes, in which the keys are the field names.
What I'm having trouble with is calling a key from an individual record hash from the array. It's just returning 'undefined'. Here's the portion of my test code I've written to look at my application code where I'm having the issue:
function hashArrayTest() {
var workbookKey = "I blanked this out"
var tableName = "AttributeChildParentJoin";
var headerRow = 1;
var i;
var attrsJoinObj = new dbEntitySource(workbookKey, tableName, headerRow);
var OutputHashArray = attrsJoinObj.returnHashItems("Parent Attribute", "Child Attribute", "AESTHETICS");
Logger.log("This is the resulting output array of hashes: ");
for(i=0; i<OutputHashArray.length; i++){
Logger.log("-------------" + i + "-------------");
Logger.log(OutputHashArray[i]);
}
Logger.log("Here are the Parent Attributes from each hash: ");
for(i=0; i<OutputHashArray.length; i++){
Logger.log("-------------" + i + "-------------");
Logger.log(OutputHashArray[i]['Parent Attribute']);
}
}
Here's the source data table:
Here is the log of the code output:
[-] This is the resulting output array of hashes:
[-] -------------0-------------
[-] {'Date Added'='', 'Child Attribute'='Color', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='21'}
[-] -------------1-------------
[-] {'Date Added'='', 'Child Attribute'='Finish', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='22'}
[-] -------------2-------------
[-] {'Date Added'='', 'Child Attribute'='Material', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='23'}
[-] -------------3-------------
[-] {'Date Added'='', 'Child Attribute'='VBL Styling Adherence', 'Parent Attribute'='AESTHETICS', 'Added by'='', 'ID'='24'}
[-] Here are the Parent Attributes from each hash:
[-] -------------0-------------
[-] undefined
[-] -------------1-------------
[-] undefined
[-] -------------2-------------
[-] undefined
[-] -------------3-------------
[-] undefined
In this output, it looks as though I have successfully created an array of hashes, although I am unable to call the ['Parent Attribute'] key from each hash.
I wrote a really simple function to test out my issue with arrays of hashes, but this time I am successful. =>
Here's the code:
function rawHashTest(){
var recordHash1 = {};
recordHash1['car'] = 45;
recordHash1['chicken'] = "ten";
var recordHash2 = {};
recordHash2['red'] = 30;
recordHash2['sticks'] = "blue";
Logger.log(recordHash1);
Logger.log(recordHash1['car']);
var hashArray = new Array();
hashArray = [recordHash1, recordHash2];
Logger.log(hashArray);
Logger.log(hashArray[0]);
Logger.log(hashArray[0]["car"]);
}
Here's the log:
[-] {chicken=ten, car=45.0}
[-] 45.0
[-] [{chicken=ten, car=45.0}, {red=30.0, sticks=blue}]
[-] {chicken=ten, car=45.0}
[-] 45.0
I can't figure out why I am able to call a hash key value from one of the hashes in the array in my simple test code, but not when I'm testing my application code. I've tried this all different ways and I've checked for leading and trailing spaces. What am I missing here?
Thank you, Nicholas Kincaid
Logger.log(typeof OutputHashArray[0])log? Also can youfor-inthrough OutputHashArray[i]?