Creating hash array in Google Apps Script

后端 未结 3 444
再見小時候
再見小時候 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 22:58

    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.

提交回复
热议问题