1

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:

An image of the data table source


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

1
  • 1
    What does Logger.log(typeof OutputHashArray[0]) log? Also can you for-in through OutputHashArray[i]? Commented Apr 16, 2015 at 9:53

1 Answer 1

0

Thank you everyone for the question probes. I checked my returnHashItems method and found the issue.

My dbEntitySource.returnHashItems method utilizes another method to build each individual hash. Here's the code that includes the original line and the updated line:

 this.returnHash = function returnHash(recordIndex){
    var recordHash = {};

    if (recordIndex !== -1){
      for (i=0; i<this.headerArray.length;i++){

        // This is the line that was giving me the issue:
        //recordHash["'"+ this.headerArray[i] +"'"] = "'" + this.dataArray[recordIndex][i] + "'";          

        // This is the line that fixed the problem:
        recordHash[this.headerArray[i]] = this.dataArray[recordIndex][i];
     }
    }
    return recordHash;
 }

Looking back at it, it looks like I had originally thought that I had to enclose it in quotes since that is how it's done to directly specify strings such as in:

 var recordHash1 = {};
 recordHash1['car'] = 45;
 recordHash1['chicken'] = "ten";

But, since the values in the headerArray and dataArray were already strings, this was redundant and also caused an issue. What type of issue, I'm not really sure yet though.

Thank you, cssimsek, for the debugging tip. I checked the type of OutputHashArray[0] before and after the fix:

 function hashArrayTest() {

  var workbookKey = "1uOHlrW8pSACDg9dNUzMFKgLi8fDBT4Wsqc5KadkoCXk";
  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("Here is the data type for the OutputHashArray:");
  Logger.log(typeof OutputHashArray);

  Logger.log("Here is the data type for the OutputHashArray[0]:");
  Logger.log(typeof OutputHashArray[0]);

  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']);
  }
 }



Output before the Fix:

[-] Here is the data type for the OutputHashArray:
[-] object
[-] Here is the data type for the OutputHashArray[0]:
[-] object
[-] 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



Output after the Fix:

[-] Here is the data type for the OutputHashArray:
[-] object
[-] Here is the data type for the OutputHashArray[0]:
[-] object
[-] This is the resulting output array of hashes:
[-] -------------0-------------
[-] {Date Added=, Child Attribute=Color, ID=21.0, Parent Attribute=AESTHETICS, Added by=}
[-] -------------1-------------
[-] {Date Added=, Child Attribute=Finish, ID=22.0, Parent Attribute=AESTHETICS, Added by=}
[-] -------------2-------------
[-] {Date Added=, Child Attribute=Material, ID=23.0, Parent Attribute=AESTHETICS, Added by=}
[-] -------------3-------------
[-] {Date Added=, Child Attribute=VBL Styling Adherence, ID=24.0, Parent Attribute=AESTHETICS, Added by=}
[-] Here are the Parent Attributes from each hash:
[-] -------------0-------------
[-] AESTHETICS
[-] -------------1-------------
[-] AESTHETICS
[-] -------------2-------------
[-] AESTHETICS
[-] -------------3-------------
[-] AESTHETICS

It looks like the issue wasn't in the data type of my hashArray, but rather in how I was specifying the hash key. Thank you everyone for the debugging tips, it really helped!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.