Home > Software design >  Running clear content on each sheet
Running clear content on each sheet

Time:08-08

I want to run a google script to clear selected ranges on every tab in my google sheet. How do I write the code to run on every tab, not just the active sheet?

function clearweek() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var morning = sheet.getRange("C4:I8").clearContent();
  var afternoon = sheet.getRange("C11:I22").clearContent();
  var evening = sheet.getRange("C25:I35").clearContent();
  var fines = sheet.getRange("C38:I38").clearContent();
}

CodePudding user response:

Something like this:

function clear_all_sheets() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  ss.getSheets().forEach(sh => clearweek(sh));
}

function clearweek(sheet) {  
  var morning = sheet.getRange("C4:I8").clearContent();
  var afternoon = sheet.getRange("C11:I22").clearContent();
  var evening = sheet.getRange("C25:I35").clearContent();
  var fines = sheet.getRange("C38:I38").clearContent();
}

CodePudding user response:

Try it this way:

function clearweek() {
  const ss = SpreadsheetApp.getActive();
  const rs = ["C4:I8","C11:I22","C25:I35","C38:I38"];
  ss.getSheets().forEach(sh => { 
    rs.forEach(r => {
      sh.getRange(r).clearContent();
    });
  });
}
  • Related