Home > other >  How to sort a google sheet Table Chart with Google Scripts
How to sort a google sheet Table Chart with Google Scripts

Time:07-18

I am trying to build a table chart to automatically generate a dashboard, but when presented, the data is not sorted in the chart. I am trying to sort descending based on the second column in the chart. The documentation available on the apps scripts developers page is not particularly helpful about implementation. See below sample code & Error. Thanks!

TypeError: workingSheet.newChart(...).setChartType(...).addRange(...).addRange(...).setTransposeRowsAndColumns(...).setMergeStrategy(...).setInitialSortingDescending is not a function

var chart = workingSheet.newChart()
    .setChartType(Charts.ChartType.TABLE)
    .addRange(tablePrice)
    .addRange(tableSubteam)
    .setTransposeRowsAndColumns(true)
    .setMergeStrategy(Charts.ChartMergeStrategy.MERGE_ROWS)
    .setInitialSortingDescending(2)
    .setPosition(row2, col1, 0, 0)
    .build();
workingSheet.insertChart(chart);

CodePudding user response:

From your showing script and error message, how about modifying as follows?

Modified script:

var chart = workingSheet.newChart()
    .asTableChart() // <--- Added
    .setChartType(Charts.ChartType.TABLE)
    .addRange(tablePrice)
    .addRange(tableSubteam)
    .setTransposeRowsAndColumns(true)
    .setMergeStrategy(Charts.ChartMergeStrategy.MERGE_ROWS)
    .setInitialSortingDescending(2)
    .setPosition(row2, col1, 0, 0)
    .build();
workingSheet.insertChart(chart);

Reference:

  • Related