Hyder W
Hyder W

Reputation: 55

How to delete a chart using google script?

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

Answers (2)

kztd
kztd

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

Cooper
Cooper

Reputation: 64100

Delete All Charts from a Sheet

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]);
  }
}

Sheet Reference

Upvotes: 3

Related Questions