问题
I have the following chart:
var ss = ... // get sheet
var chart = ss.newChart().asLineChart();
chart.setTitle("My Title");
chart.addRange(dataSheet.getRange(1, 1, ss.getLastRow(), 4));
chart.setColors(["#3c78d8", "#a61c00", "#38761d"]);
chart.setXAxisTitle("X Title");
chart.setYAxisTitle("Y Title");
chart.setPosition(1, 1, 0, 0);
chart.setCurveStyle(Charts.CurveStyle.SMOOTH);
ss.insertChart(chart.build());
This code will use the show the first row as part of the data, instead of using it to label the legend. As a side note, if I use asColumnChart instead of line chart, it does the right thing.
How can I tell a chart to use the first row as headers using Google Apps Script?
回答1:
First off - your code is OK. I took a copy and made minor changes to it just to use a single sheet for illustration:

The problem is likely with your data, specifically the first row. Here's that same example, with some of the "Headers" changed from text into numbers. I believe it is behaving in the bad way you're describing.

One more time, but with the numbers in the header forced to be text. (In the spreadsheet, I input them as '4
and '5
- the leading single quote tells Spreadsheet to treat as text.)

Just take a look at the data types in your first row, and make sure that they are Text.
来源:https://stackoverflow.com/questions/14611197/google-apps-script-chart-use-row-1-as-headers