Q_C
Q_C

Reputation: 443

Create embedded charts with custom label

I want to create a line chart using the EmbeddedChart from the Google Apps Script Spreadsheet Service. I have a set of (x,y) coordinates in column A and B of a spreadsheet, and I want to draw the corresponding line in a chart with Apps Script.

function newChart() {
   var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Page 2");
   var chartBuilder = sheet.newChart();
   chartBuilder.addRange(sheet.getRange("A1:B30"))
       .setChartType(Charts.ChartType.LINE);
   sheet.insertChart(chartBuilder.build());
 }

The problem with this function is that it creates a chart with two lines instead of one. In order to have the correct chart, I have to manually edit the advanced settings, and check the option "Use column A as labels".

Is there an option to specify this setting using Apps Script?

Upvotes: 1

Views: 880

Answers (1)

Mogsdad
Mogsdad

Reputation: 45710

The data table for a Chart is interpreted automatically to determine what roles each column performs.ref If your first column contains text, its role will be "domain", and "Domain columns specify labels along the major axis of the chart."

You haven't shared your data in your question, but it's a safe bet that column A contains numbers or dates. Here's what your code produces when column A contains text:

screenshot

So the question is, can the role for a column be specified in an Embedded Chart? Unfortunately, no it can't.

As a work-around, you could replicate the numeric or date values in column A into another column as text.

Upvotes: 2

Related Questions