在Google Apps脚本中创建哈希数组 [英] Creating hash array in Google Apps Script

查看:140
本文介绍了在Google Apps脚本中创建哈希数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本周我一直在尝试使用Trello和Google Apps脚本。我正在尝试创建一个哈希数组,然后我可以使用它来加载电子表格。 Google应用脚本不喜欢创建哈希的典型JavaScript代码。我查了一下文档,但是他们没有像哈希那样的东西......他们说:

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
 }


推荐答案

阅读问题,我理解作者需要知道如何在GAS中创建关联数组 。如果它是正确的,那么这里有几个链接(这里这里)以及示例代码如下。

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;
}

如果作者确实需要


哈希数组

an array of hashes

然后根据哪种哈希算法有几种方法是必需的。

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


  1. 使用 Utilities.computeDigest 方法使用可用算法

  2. 如果 Utilities.computeDigest ,然后可以编写自己的实现,因为它是已完成 n.29%rel =noreferrer> BLAKE功能。

  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.

以下是如何创建使用MD5哈希的哈希数组。

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

P.S。作者代码的返回行返回名称,标签; //返回输出
的列表不正确 - 仅返回标签变量值。要将一些变量作为数组返回,需要编写 return [name,label]; 。或者可能是作者需要返回列表变量而不是 name label

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.

这篇关于在Google Apps脚本中创建哈希数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆