Reputation: 55
I wrote a script to automatically generate charts from data on different tabs (after clicking a button on a new sheet).
I would now like to add a "reset" button which will automatically delete or remove the generated charts (so I can generate different charts with another button).
Can anyone help on how I can do this? I've tried the below, but to no avail....
function Finance_clear() {
function deleteAllChartsFromSheet(sheetName) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var chartsSheet = ss.getSheetByName(sheetName);
var n = chartsSheet.getCharts().length;
for (var i = n-1; i >= 0; i--) {
var chart = chartsSheet.getCharts()[i];
chartsSheet.removeChart(chart);
}
}
};
Upvotes: 2
Views: 3797
Reputation: 3405
The code worked for me, I rewrote it as:
function remove_charts_from_sheet(sheet_name) {
const tab = SpreadsheetApp.getActive().getSheetByName(sheet_name);
const charts = tab.getCharts();
for(let i = 0; i < charts.length; i++){
tab.removeChart(charts[i]);
}
}
Upvotes: 0
Reputation: 64100
function delAllChartsFromSheet(name) {
var ss=SpreadsheetApp.getActive();
var sh=ss.getSheetByName(name);
var chts=sh.getCharts();
for(var i=0;i<chts.length;i++){
sh.removeChart(chts[i]);
}
}
Upvotes: 3