Brief: I want to change the data in an Google sheets EmbeddedChart by using an array generated within the embedded Google script without pouring the data into cells first.
Long: I have an bound script that updates a preexisting spreadsheet. I modify the data in js and want to change an embedded chart to use the generated array of data without putting the data on the table. I technically do not care that it is an EmbeddedChart I just want to make a summary page with a graph without polluting the document with an extra sheet. Would prefer not to make an HTML page out of the sheet.
I have tried various options.
.setDataTable()
is not a method of EmbeddedChartBuilder
, unlike normal (HTML) Google Charts. I am assuming I am missing something simple, like some class that converts an array to a pseudo-Range instances akin to a DataTable or Excel's names, without touching the sheet.
Worst case scenario: I am not aware of a method to add a div with id attrib in Google sheets, is there one?
Ways tested:
sheet.getCharts()[0].modify().addRange([2,3,4,5]);
//Cannot convert Array to Range.sheet.getCharts()[0].modify().addRange("{2,3,4,5}");
//Cannot find method addRange(string).- (Desparate...)
sheet.getCharts()[0].modify().setDataTable();
//Cannot find function setDataTable in object EmbeddedChartBuilder. - (Desparate...)
sheet.updateChart(chartFromCharts);
//Cannot find method updateChart(Chart). Magically convert array to a range object without saving on a sheet
//Demo script. function populate() { var a=[1, 2, 3, 4, 5]; var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet(); var chart=sheet.getCharts()[0].modify(); chart.setOption('title', "Changed in script..."); cchart=chart.asColumnChart(); dt=Charts.newDataTable().addColumn(Charts.ColumnType.STRING, "Categorical abscissa").addColumn(Charts.ColumnType.NUMBER, "Numerical ordinate").addRow(["A",5]).addRow(["B",6]).build();
//Try 1. Desparate just in case it accepts arrays. //chart.addRange(a); //Cannot convert Array to Range. //cchart.addRange(a); //Cannot convert Array to Range. //Try 2. Desparate just in case it accepts DataTable //chart.addRange(dt); //Cannot find method addRange(DataTable). //Try 3. Desparate hack... //new google.visualization.ColumnChart(getCharts()[0].getId()).draw(dt, options); //ReferenceError: "google" is not defined //Could I have accessed it anyway? //$(getCharts()[0].getId()).html("Hacked") //ReferenceError: "$" is not defined. //again... //document.getElementByID(getCharts()[0].getId()) //ReferenceError: "document" is not defined. //Try 5. //sheet.updateChart(Charts.newColumnChart().build()); //Cannot find method updateChart(Chart). //Try 4. Missing method in documentation?! //chart.addDataTable(a); //Cannot find function addDataTable in object EmbeddedChartBuilder.
//cchart.addDataTable(a); //Cannot find function addDataTable in object EmbeddedColumnChartBuilder.
sheet.updateChart(chart.build()); }