Giving and Retrieving Parameters for Chart at GAS

This sample script is for retrieving parameters from a chart. The chart created by both Google Apps Script and manually operation can be used.

Creates Chart

When a chart is created, it supposes following parameters.

var parameters = {
  "title": "x axis",
  "fontName": "Arial",
  "minValue": 0,
  "maxValue": 100,
  "titleTextStyle": {
    "color": "#c0c0c0",
    "fontSize": 10,
    "fontName": "Roboto",
    "italic": true,
    "bold": false
  }
};

.setOption('hAxis', parameters)

Retrieve Parameters From Chart

For the chart created by above parameters, in order to retrieve the parameters, it uses following script.

var hAxis_title = chart.getOptions().get('hAxis.title')
var hAxis_fontName = chart.getOptions().get('hAxis.fontName')
var hAxis_minValue = chart.getOptions().get('hAxis.minValue')
var hAxis_maxValue = chart.getOptions().get('hAxis.maxValue')
var hAxis_titleTextStyle_color = chart.getOptions().get('hAxis.titleTextStyle.color')
var hAxis_titleTextStyle_fontSize = chart.getOptions().get('hAxis.titleTextStyle.fontSize')
var hAxis_titleTextStyle_fontName = chart.getOptions().get('hAxis.titleTextStyle.fontName')
var hAxis_titleTextStyle_italic = chart.getOptions().get('hAxis.titleTextStyle.italic')
var hAxis_titleTextStyle_bold = chart.getOptions().get('hAxis.titleTextStyle.bold')

Result :

x axis
Arial
0.0
100.0
#c0c0c0
10.0
Roboto
true
false

At Google chart, the parameters is constructed by JSON data. So when the chart is created, JSON data is used. In the same way, when the parameters is retrieved from the chart, it retrieves from the JSON data.

By using this method, it is possible to create a Google Apps Script from the charts which was manually created.

 Share!