3

Something I'm writing in Google Apps Script for Google Sheets generates a chart. In trying to generate a graph I'd like to set axis titles so I can make the charts more understandable for people other than myself. The problem I'm having is that setting vAxis.title in any form doesn't do anything. Once the chart is generated no vertical axis title is made, it's just blank.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
var chartBuilder = sheet.newChart();

chartBuilder.setChartType(Charts.ChartType.SCATTER)
    .addRange(range)
    .setOption("pointSize", 1)
    .setOption("hAxis.title", "X value")
    .setOption("title", "Output")
    .setOption("vAxis.title", "Y output") //Set vertical axis title (not working)
    .setOption("trendlines", {0: {type: "linear", visibleInLegend: true, showR2: true}})
    .setPosition(1, 1, 150, 0);

sheet.insertChart(chartBuilder.build());

Help would be greatly appreciated, Thank you!

tehhowch
  • 9,645
  • 4
  • 24
  • 42
Napostrophe
  • 145
  • 1
  • 6
  • 1
    Possible duplicate of [Google Apps Script: How to set "Use column A as labels" in chart embedded in spreadsheet?](https://stackoverflow.com/questions/13594839/google-apps-script-how-to-set-use-column-a-as-labels-in-chart-embedded-in-spr) – TheMaster Oct 19 '18 at 14:46

1 Answers1

3

This gist solved a very similar issue for me: https://gist.github.com/tanaikech/4125cc280e15c0fc726cb2fe4f35a3f7

Setting "vAxis" options through setOption("vAxis", {title: "y axis"}) seems to be broken. I tried all types of setting but nothing seemed to work.

But as the gist I link to above suggests, all those settings can be set using setOption("vAxes", {0: {title: "y axis"}})

So in your code change .setOption("vAxis.title", "Y output") to setOption("vAxes", {0: {title: "Y output"}}).

You can set multiple options that way, here is a working example from my code: .setOption("vAxes", {0: {title: "Kilograms", viewWindowMode:'explicit',viewWindow: {max:150,min:50}}})

TheIceBear
  • 2,912
  • 9
  • 23