问题描述
我正在使用 Google Apps Script 和 EmbeddedChartBuilder 在我的 Google 电子表格中嵌入折线图.当您手动创建这些图表时,您有(非默认)选项使用 A 列作为标签"(其中A"是数据范围中的第一列).我找不到从脚本中执行相同操作的方法.从 Google Visualization Line Chart 文档中,似乎默认是将第一列视为具有域"角色;但 EmbeddedChartBuilder 似乎覆盖了这一点,并赋予所有列数据"角色.由于我没有明确的 DataTable,我无法设置 列角色 我自己.
I am using Google Apps Script and EmbeddedChartBuilder to embed line charts within my Google Spreadsheet. When you create these charts by hand, you have the (non-default) option to "Use column A as labels" (where "A" is the first column in the data range). I cannot find a way to do the same from a script. From the Google Visualization Line Chart documentation, it appears that the default is to treat the first column as having the "domain" role; but EmbeddedChartBuilder seems to override this and give all columns the "data" role. Since I don't have an explicit DataTable, I have no way to set the column roles myself.
我错过了这样做的方法吗?或者我是否必须将方法从 EmbeddedChartBuilder 切换到使用电子表格作为数据源?
Have I missed a way to do this? Or do I have to switch approaches from EmbeddedChartBuilder to using the spreadsheet as a data source?
推荐答案
找到了!使用 将选项 useFirstColumnAsDomain
设置为 true
EmbeddedChartBuilder.setOption.
Found it! Set the option useFirstColumnAsDomain
to true
with EmbeddedChartBuilder.setOption.
这个选项似乎没有记录.我通过转到发布图表"(单击图表,然后从右上角的下拉列表中进行选择)并检查给定代码中的 JavaScript 数据结构来找到它.确切地说,我创建了一个没有选中使用 A 列作为标签"的图表,抓取了发布数据结构,然后选中了使用 A 列作为标签",抓取了新的发布数据结构,并将两者进行了比较.为了进行比较,我建议规范化 JSON 并运行 diff
.此技术可用于对图表编辑器中的任何设置进行逆向工程.
This option appears to be undocumented. I found it by going to "Publish chart" (click on the chart, then select from the drop-down in the top right) and inspecting the JavaScript data structure in the given code. To be exact, I created a chart with "Use column A as labels" unchecked, grabbed the publish data structure, then checked "use column A as labels", grabbed the new publish data structure, and compared the two. To compare, I suggest normalizing the JSON and running diff
. This technique can be used to reverse-engineer any settings in the chart editor.
这篇关于Google Apps 脚本:如何设置“将 A 列用作标签"在嵌入电子表格的图表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!