找到你要的答案

Q:Google Apps Script: How to set “Use column A as labels” in chart embedded in spreadsheet?

Q:谷歌应用程序脚本:如何在图表中嵌入“使用列A作为标签”?

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.

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?

我使用谷歌应用程序的脚本和EmbeddedChartBuilder嵌入线图在我的谷歌电子表格。当你手工创建这些图表时,你有(非默认)选项“使用列A作为标签”(其中“a”是数据范围内的第一列)。我找不到一个脚本做同样的方法。从谷歌可视化线图文档,似乎默认是把第一列为“域”的作用;但EmbeddedChartBuilder似乎覆盖了这个给所有列的“数据”的作用。因为我没有一个明确的数据表,我没有办法设置列的角色我自己。

我错过了这样做的方法吗?或者我要开关的方法从EmbeddedChartBuilder到使用电子表格作为数据源?

answer1: 回答1:

Found it! Set the option useFirstColumnAsDomain to true with EmbeddedChartBuilder.setOption.

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.

发现它!设置为true和embeddedchartbuilder.setoption选项usefirstcolumnasdomain。

此选项似乎是无证。我去“出版图发现它”(点击图,然后从右上方的下拉框中选择)、检查给定的代码的JavaScript数据结构。确切地说,我创建了一个“使用列A作为标签”未选中的图表,抓取了发布的数据结构,然后选中“使用列A作为标签”,抓取了新发布的数据结构,并将两者进行了比较。相比较,我建议规范JSON和运行不同的这项技术可用于在图表编辑器进行反向工程的任何设置。

answer2: 回答2:

I just experienced this same problem. I took a similar approach as Andrew, but found a different solution (presumably because Google has added functionality to their graphs in Spreadsheets).

I used http://jsbeautifier.org/ to format the code after publishing, and I found this part to be responsible for adding the data labels. Note that you can even change the color of the stem that connects the bar to the data label:

"series": {
                    "0": {
                        "errorBars": {
                            "errorType": "none"
                        },
                        "dataLabel": "value",
                        "annotations": {
                            "stemColor": "none"
                        }
                    },
                    "color": "black",
                    "targetAxisIndex": 0,
                    "annotations": {
                        "textStyle": {
                            "color": "red",
                            "fontSize": 12
                        }
                    }
                },

我刚刚经历了同样的问题。我采取了类似的方法,安得烈,但发现了一个不同的解决方案(大概是因为谷歌增加了功能,他们的图表在电子表格)。

我用http://jsbeautifier.org/格式化代码出版后,我发现这部分负责添加数据标签。请注意,您甚至可以改变颜色的阀杆连接的数据标签的酒吧:

"series": {
                    "0": {
                        "errorBars": {
                            "errorType": "none"
                        },
                        "dataLabel": "value",
                        "annotations": {
                            "stemColor": "none"
                        }
                    },
                    "color": "black",
                    "targetAxisIndex": 0,
                    "annotations": {
                        "textStyle": {
                            "color": "red",
                            "fontSize": 12
                        }
                    }
                },
google-apps-script  google-visualization