Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
334 views
in Technique[技术] by (71.8m points)

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

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...