Google Script - Use Row X As Headers
I have a system that makes line graphs and scatterplots in google sheets. As of March 7th, 2018 the graphs started to include the 1 row of my data as part of the graph rather than
Solution 1:
You can set legend values using a labelInLegend
in the series
configuration options as mentioned here.(Navigate to the option series)
So in your above code lets assume you have extracted your first row in a array like so:
var values = currRange.getValues()
var headerRow = []
for (var i =0; i<values[0].length ; i++)
headerRow[i] = values[0][i]
You can set the values of your series using setOptions like so:
.setOption('series',
{0:{color: '#54B4C6', pointSize: 3, pointShape: 'circle',labelInLegend:headerRow[1]},
1:{color: '#2F53D9', pointSize: 10, pointShape: 'diamond',labelInLegend:headerRow[2]},
2:{color: '#F1BE00', pointSize: 4, pointShape: 'circle',labelInLegend:headerRow[3]},
3:{color: '#D90D0C', pointSize: 11, pointShape: 'diamond',labelInLegend:headerRow[4]}})
In short, just include the following key:value
pair lableInLegend:'Header Value'
in your series object.
Note: Skip the first value of the header, as that pertains to the vertical axis.
Your final code will like so:
var values = currRange.getValues()
var headerRow = []
for (var i =0; i<values[0].length ; i++)
headerRow[i] = values[0][i]
var currChart = currSheet.newChart();
currChart.setPosition (row, col, 5,5)
var builtChart = currChart.asScatterChart()
.addRange(currRange)
.setTitle(title)
.setYAxisTitle(axisLabel)
.setOption('useFirstColumnAsDomain','true')
.setOption('hAxis.viewWindow.min', min)
.setOption('hAxis.viewWindow.max', max)
.setOption('chartArea.left', 80)
.setOption('chartArea.top', 60)
.setOption('chartArea.width', 360)
.setOption('chartArea.height', 240)
.setOption('chartArea.backgroundColor',
{ stroke: "#828282", strokeWidth: 2, fill: "#fcfcfc" })
.setOption('series',
{0:{color: '#54B4C6', pointSize: 3, pointShape: 'circle',labelInLegend:headerRow[1]},
1:{color: '#2F53D9', pointSize: 10, pointShape: 'diamond',labelInLegend:headerRow[2]},
2:{color: '#F1BE00', pointSize: 4, pointShape: 'circle',labelInLegend:headerRow[3]},
3:{color: '#D90D0C', pointSize: 11, pointShape: 'diamond',labelInLegend:headerRow[4]}})
.build();
currSheet.insertChart(builtChart);
Post a Comment for "Google Script - Use Row X As Headers"