Google Sheet: 如何在JSON 导出中定义"cols.label" 值?

Google Sheet: How to define the "cols.label" value in JSON export?

我使用 google sheet 作为端点将数据导入 html table。根据 sheet 中的数据类型,我得到不同的 JSON 结构。

为了说明问题,我用两个非常简单的table构建了一个Google sheet。 两个 table 之间的唯一区别是 Age 字段,它在 Sheet1 中是数字的,在 Sheet2 中是文本的。

Link document

如果我将 sheets 用作 JSON 端点,我会出现以下行为:Sheet1 正确导出 header,但 Sheet2 显示空 header(第 headers 成为正常的行值)。

工作表 1 JSON

{
    "version": "0.6",
    "reqId": "0",
    "status": "ok",
    "sig": "325167901",
    "table": {
        "cols": [
            {
                "id": "A",
                "label": "Name",
                "type": "string"
            },
            {
                "id": "B",
                "label": "Age",
                "type": "number",
                "pattern": "General"
            }
        ],
        "rows": [
            {
                "c": [
                    {
                        "v": "Vittorio"
                    },
                    {
                        "v": 52.0,
                        "f": "52"
                    }
                ]
            }
        ],
        "parsedNumHeaders": 1
    }
}

工作表 2 JSON

{
    "version": "0.6",
    "reqId": "0",
    "status": "ok",
    "sig": "1566616543",
    "table": {
        "cols": [
            {
                "id": "A",
                "label": "",
                "type": "string"
            },
            {
                "id": "B",
                "label": "",
                "type": "string"
            }
        ],
        "rows": [
            {
                "c": [
                    {
                        "v": "Name"
                    },
                    {
                        "v": "Age"
                    }
                ]
            },
            {
                "c": [
                    {
                        "v": "Vittorio"
                    },
                    {
                        "v": "52"
                    }
                ]
            }
        ],
        "parsedNumHeaders": 0
    }
}

有什么方法可以修复 Sheet2 的行为吗?

好像有问题,同时我的建议是测试JSON端点标签如下

const jsonString = `/*O_o*/
google.visualization.Query.setResponse({"version":"0.6","reqId":"0","status":"ok","sig":"1566616543","table":{"cols":[{"id":"A","label":"","type":"string"},{"id":"B","label":"","type":"string"}],"rows":[{"c":[{"v":"Name"},{"v":"Age"}]},{"c":[{"v":"Vittorio"},{"v":"52"}]}],"parsedNumHeaders":0}});`

function jsonWithLabels(jsonString) {
  var json = JSON.parse(jsonString.slice(47,-2))
  var labels = json.table.cols.map(c => c.label)
  return (labels.join('') != '')
}

function test(){
  console.log (jsonWithLabels(jsonString))
}

如果为 false,则需要从第 1 行获取标签

const jsonString = `/*O_o*/
google.visualization.Query.setResponse({"version":"0.6","reqId":"0","status":"ok","sig":"1566616543","table":{"cols":[{"id":"A","label":"","type":"string"},{"id":"B","label":"","type":"string"}],"rows":[{"c":[{"v":"Name"},{"v":"Age"}]},{"c":[{"v":"Vittorio"},{"v":"52"}]}],"parsedNumHeaders":0}});`

  var json = JSON.parse(jsonString.slice(47,-2))
  var labels = json.table.cols.map(c => c.label)
  console.log (labels.join('') != '')

这里是重建 table 带标签和不带标签的完整脚本

const jsonString = `/*O_o*/
google.visualization.Query.setResponse({"version":"0.6","reqId":"0","status":"ok","sig":"1566616543","table":{"cols":[{"id":"A","label":"","type":"string"},{"id":"B","label":"","type":"string"}],"rows":[{"c":[{"v":"Name"},{"v":"Age"}]},{"c":[{"v":"Vittorio"},{"v":"52"}]}],"parsedNumHeaders":0}});`
document.getElementById("json").innerHTML = myItems(jsonString.slice(47, -2))

  function myItems(jsonString) {
    var json = JSON.parse(jsonString);
    var table = '<table>';
    var flag = jsonWithLabels(json);
    if (flag) {
      table += '<tr>';
      json.table.cols.forEach(colonne => table += '<th>' + colonne.label + '</th>')
      table += '</tr>';
    }
    json.table.rows.forEach((row, index) => {
      table += '<tr>';
      row.c.forEach(cel => {
        try { var valeur = cel.f ? cel.f : cel.v }
        catch (e) { var valeur = '' }
        if (!flag && index == 0) { table += '<th>' + valeur + '</th>' }
        else { table += '<td>' + valeur + '</td>' }
      })
      table += '</tr>';
    })
    table += '</table>';
    return table
  }
  function jsonWithLabels(json) {
    var labels = json.table.cols.map(c => c.label);
    return (labels.join('') != '')
  }
table {border-collapse: collapse;}
th,td {border: 1px solid black;}
<div id="json">json here</div>