Creating hash array in Google Apps Script

后端 未结 3 442
再見小時候
再見小時候 2021-02-04 22:26

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 s

3条回答
  •  情书的邮戳
    2021-02-04 23:00

    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.

提交回复
热议问题