【问题标题】:Is it possible set the "Use row x as headers" option for a chart using Google Sheets API?是否可以使用 Google Sheets API 为图表设置“使用 x 行作为标题”选项?
【发布时间】:2022-08-04 14:17:38
【问题描述】:

我目前正在尝试构建图表并正确设置图例标签,但我似乎无法找到使用 API 执行此操作的方法。

似乎可以使用 Apps 脚本,但除非没有其他方法,否则我不想这样做。

这是我想要的图片

  • 你能提供你当前的脚本吗?
  • @Tanaike 不确定这有什么帮助,但在这里:pastebin.com/FJhV3te2。我只能补充的是,系列范围从“Col 1”行开始
  • 谢谢你的回复。我不得不为我糟糕的英语水平道歉。我没注意到I\'m asking if it\'s possible。在这种情况下,答案是肯定的。但不幸的是,我不知道你的实际图表。因此,在现阶段,我无法提出样品请求正文。如果您需要示例请求正文,您能否提供包含图表的示例电子表格?通过这个,我想检查一下。我再次为我糟糕的英语水平深表歉意。
  • @Tanaike 链接到我的请求正文 pastebin.com/4vQzaX49 和电子表格 docs.google.com/spreadsheets/d/…
  • 谢谢你的回复。当我看到您的示例电子表格时,您的示例电子表格的图表似乎与您的显示图像不同。所以,我无法理解你的目标。我为此道歉。我可以问你目标的细节吗?

标签: google-sheets google-sheets-api


【解决方案1】:

在这种情况下,请将headerCount的值从0修改为1。您可以在basicChart 的属性中找到此属性。使用 UpdateChartSpecRequest 时,示例请求正文如下。

{
  "requests": [
    {
      "updateChartSpec": {
        "chartId": ###, <--- Please set your chart ID.
        "spec": {
          "title": "Chart 0",
          "basicChart": {
            "chartType": "COLUMN",
            "axis": [
              {
                "position": "BOTTOM_AXIS",
                "title": "condition",
                "format": {
                  "fontFamily": "Roboto"
                },
                "viewWindowOptions": {}
              },
              {
                "position": "LEFT_AXIS",
                "viewWindowOptions": {}
              }
            ],
            "domains": [
              {
                "domain": {
                  "sourceRange": {
                    "sources": [
                      {
                        "startRowIndex": 3,
                        "endRowIndex": 7,
                        "startColumnIndex": 0,
                        "endColumnIndex": 1
                      }
                    ]
                  }
                }
              }
            ],
            "series": [
              {
                "series": {
                  "sourceRange": {
                    "sources": [
                      {
                        "startRowIndex": 3,
                        "endRowIndex": 7,
                        "startColumnIndex": 1,
                        "endColumnIndex": 2
                      }
                    ]
                  }
                },
                "targetAxis": "LEFT_AXIS",
                "dataLabel": {
                  "type": "NONE",
                  "textFormat": {
                    "fontFamily": "Roboto"
                  }
                }
              },
              {
                "series": {
                  "sourceRange": {
                    "sources": [
                      {
                        "startRowIndex": 3,
                        "endRowIndex": 7,
                        "startColumnIndex": 2,
                        "endColumnIndex": 3
                      }
                    ]
                  }
                },
                "targetAxis": "LEFT_AXIS",
                "dataLabel": {
                  "type": "NONE",
                  "textFormat": {
                    "fontFamily": "Roboto"
                  }
                }
              }
            ],
            "headerCount": 1  <--- This is important modification point.
          },
          "hiddenDimensionStrategy": "SKIP_HIDDEN_ROWS_AND_COLUMNS",
          "titleTextFormat": {
            "fontFamily": "Roboto"
          },
          "fontName": "Roboto"
        }
      }
    }
  ]
}

参考:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-05
    • 1970-01-01
    相关资源
    最近更新 更多