2

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?

oneself
  • 38,641
  • 34
  • 96
  • 120

1 Answers1

4

First off - your code is OK. I took a copy and made minor changes to it just to use a single sheet for illustration:

Chart Screenshot - all good

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.

Chart Screenshot - problems

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.)

Chart Screenshot - all good again

Just take a look at the data types in your first row, and make sure that they are Text.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275