【问题标题】:Json to google chart table with varying cols and rowJson to google chart table with different cols and row
【发布时间】:2020-05-05 05:06:50
【问题描述】:

我有下面的json

[{
    "dept": "department1",
    "values": [20, 4, 355, 223, 5, 5, 19, 23, 45,11,12]
}, {
    "dept": "department2",
    "values": [5, 2]
}, {
    "dept": "department3",
    "values": [35, 3, 8, 18, 10,3,4, 9]
}, {
    "dept": "department4",
    "values": [36]
}
]

部门的数量不同(不超过 24 个),因此在值中计数(但不超过 100 个)

我想倾斜并使其成为如下的谷歌表格。我该怎么做?


department1 , department2, department3, department4
20            5            35            36
4             2            3
355                        8
223                        18
5                          10
5                          3
19                         4
23                         9
45                        
11
12                        

【问题讨论】:

    标签: javascript json charts html-table google-visualization


    【解决方案1】:

    您可以使用嵌套的 for 循环来完成此操作:

    var file = [{
        "dept": "department1",
        "values": [20, 4, 355, 223, 5, 5, 19, 23, 45,11,12]
    }, {
        "dept": "department2",
        "values": [5, 2]
    }, {
        "dept": "department3",
        "values": [35, 3, 8, 18, 10,3,4, 9]
    }, {
        "dept": "department4",
        "values": [36]
    }
    ]
    
    
    var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet()
    
    for (i = 0; i < file.length; i++){
        //get the department name and add it to the first cell of current column (i)
        sheet.getRange(1, i+1).setValue(file[i].dept)
        for (j = 0; j < file[i].values.length; j++){
         //get the each value of values and add it to the cell in row(j) and column (i)
          sheet.getRange(j+2, i+1).setValue(file[i].values[j])
        }
      }
    

    【讨论】:

    • 对不起,我忘了添加,它必须显示在 html 的
      中。 SpreadsheepApp 可以嵌入这个吗?
    • 文件>发布到网络中有一个选项,它创建一个 iframe 来嵌入工作表。不过,从来没有使用过这个。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签