Search code examples
google-apps-scriptgoogle-visualizationembedded-resourcegauge

How to use a gauge chart in Google Sheets programmatically


I would like to add a "Gauge chart" in Google sheets programmatically. This is an example that I found on the internet but it is coded to display the chart on a webpage. I will, however, try to work off of that example code. But, any help is appreciated. Here's the example https://developers.google.com/chart/interactive/docs/gallery/gauge


Solution

    • You want to create "Gauge chart" to the Spreadsheet.
    • You need a sample script for achieving this with Google Apps Script.

    If my understanding is correct, how about this sample script?

    Sample script:

    Before you run the script, as a test case, please set sample and 50 to the cells of "A1" and "B1", respectively.

    function myFunction() {
      var sheet = SpreadsheetApp.getActiveSheet();
      var chart = sheet.newChart()
        .setChartType(Charts.ChartType.GAUGE)
        .addRange(sheet.getRange('A1:B1'))
        .setPosition(3, 1, 0, 0)
        .setOption('height', 300)
        .setOption('width', 300)
        .setOption('title', 'Sample chart')
        .build();
      sheet.insertChart(chart);
    }
    

    Result:

    enter image description here

    References:

    If I misunderstood your question and this was not the direction you want, I apologize.