Creating hash array in Google Apps Script

Psyllex picture Psyllex · Sep 29, 2012 · Viewed 30.5k times · Source

I've been trying to work with Trello and the Google Apps Script this week. I am trying to create an array of hashes that I can then use to load the spreadsheet. Google apps script doesn't like the typical javascript code of creating hashes. I've looked up the docs but they don't have anything like hashes...they say to:

 var object = [];
 var object1 = {};
 object.push(object1);

This wont work because I'm essentially trying to do something like:

var hash={name: , label: };
var n= someNumber;
var l= someLabel
var hash.push(name: n, label: l);

Essentially that is the code I have right now. But here is my entire function:

  function getData(){
  var list={};
  //get the list of delivered cards from Trello
  var listRequest = authorizeToTrello(); // get authorization
  var result = UrlFetchApp.fetch("https://trello.com/1/lists/4fea3a2c3a7038911ebff2d8/cards",
  listRequest);//fetch list
  var listOfCards = Utilities.jsonParse(result.getContentText());//Google app utility format json

  //outer loop to iterate through list of Cards
  for(var i=0; i < listOfCards.length; i++){
     var cardId = listOfCards[i].id; //get the id of a single card
     var l = listOfCards[i]["label"]; //get the label for the our structure

  //get a json object for a single card within the list of cards iteration 
  var cardRequest = authorizeToTrello();
  var getCard = UrlFetchApp.fetch("https://trello.com/1/cards/" + cardId + "/actions", cardRequest); 
  var singleCard = Utilities.jsonParse(getCard.getContentText());

 //inner loop to iterate the single cards JSON objects

  for(var j=0; j < singleCard.length; j++) {
    if(singleCard[j].data != undefined && singleCard[j].data.listAfter != undefined)
    {
      var str = singleCard[j]["data"]["listAfter"]['name'];
      if(str === "Delivered Q3 2012"){
          var n = singleCard[j]['memberCreator']['fullName'];
        }
     }
  }
    //push the data to list
    list.push(n,l);
  }

 return name, label; //return list for output
 }

Answer

megabyte1024 picture megabyte1024 · Sep 30, 2012

Reading the question, I understood that the author needs to know how to create an associative array in a GAS. If it is correct then here is a couple of links (here and here) and a sample code is bellow.

function testMap() {
  var map = {};
  map["name1"] = "value1";
  map["name2"] = "value2";
  return map;
}

If the author needs really

an array of hashes

then there are a couple of ways depending on which hash algorithm is required.

  1. to use the Utilities.computeDigest method to calculate a hash of a string using one of available algorithms.
  2. if the required hash calculation algorithm is not supported by the Utilities.computeDigest, then is possible to write own implementation as it is done for the BLAKE function.

Here is a sample of how to create an array of hashes using the MD5 hash.

function testHash() {
  var array = [];
  array.push(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, "value1"));
  array.push(Utilities.computeDigest(Utilities.DigestAlgorithm.MD5, "value2"));
  return array;
}

P.S. The return line of the author code return name, label; //return list for output is not correct - only the label variable value is returned. To return a couple of variables as an array is necessary to write return [name, label];. Or may be the author needs to return the list variable and not name and label.