Creating hash array in Google Apps Script

2020-05-24 13:01发布

问题:

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
 }

回答1:

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.



回答2:

I know this is an old post / question, but i would like to update my answer since the original anwer (1st answer) is misleading. I was myself looking for how to return associative arrays back to a cell in the spreadsheet, but alas.. "YOU CANNOT". Google spreadsheet MUST want an numerically indexed array or an object. Otherwise it returns "#ERROR".

Here are the steps to replicate the issue.

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

Formula in your cell: =testMap()
Value in your cell: Thinking... #ERROR

Solution (rather a workaround)

1: Transfer your objects from your associative array into a numerically indexed array using for-each type loop.

var temp = new Array();
for (var i in map) {
    temp.push([i,map[i]])
    // optionally use activeSheet.getRange(X:X).setValue([i,map[i]])) function here.
   // set values will not work in cell functions. To use it via cell functions, rerun / trigger the functions using an on_edit event. 
}

If you used a temp like numerically indexed array, you can return "temp" back to the calling cell.



回答3:

Summary: For onEdit() purposes, use Cache Service to define associative array data.

Here's a shared Gsheet demonstrating this curious behavior. I tried the following solution in programmatically defining an associative array based on data in a Google sheet.

var assocArr = {
  labels: {},
  init: function () {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('sheetName');
    var values = sheet.getDataRange().getValues();
    for(var row in values) {
      assocArr.labels[values[row][0]] = values[row][1];
    };
    for(var key in assocArr.labels) {
      Logger.log("key: %s, value: %s",key, assocArr.labels[key]);
    };
    return(void(0));
  },
};

To execute this, you run the init() method in the onOpen() event handler.

function onOpen() {
  assocArr.init();
  var key = 'test';
  SpreadsheetApp.getUi().alert( assocArr.labels[key] );
  Logger.log("onOpen: key: %s, value: %s",key, assocArr.labels[key]);
};

The logger message confirms that init() loads the data from the worksheet.

Now if I try to reference this assocArr object in onEdit() it returns undefined for all key values.

function onEdit(event) {
  var key = 'test';
  SpreadsheetApp.getUi().alert( assocArr.labels[key] );
  Logger.log("onEdit: key: %s, value: %s",key, assocArr.labels[key]);
};

I infer that for security reasons, Google limited the simple-trigger onEdit() to not have global variable scope, same as they voided the utility of the event.user property.

Now instead if I simply put the key-value pair in the cache, it works! Here is the complete code that works using the Cache Service.

var cache = CacheService.getPrivateCache();

var assocArr = {
  init: function () {
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Account Labels');
    var values = sheet.getDataRange().getValues();
    for(var row in values) {
      cache.put(values[row][0], values[row][1], 3600);
    };
    return(void(0));
  },
};

function onOpen() {
  assocArr.init();
  var key = 'test';
  SpreadsheetApp.getUi().alert( cache.get(key) );
  Logger.log("onOpen: key: %s, value: %s",key, cache.get(key));
};


function onEdit(event) {
  var key = 'test';
  SpreadsheetApp.getUi().alert( cache.get(key) );
  Logger.log("onEdit: key: %s, value: %s",key, cache.get(key));
};

Curiously, the onEdit() has the cache variable in its scope.

Here again is the shared Gsheet demonstrating this curious behavior.