2
votes

I'm trying to create a button that will delete selected cells across 4 sheets. I've searched all over for a script that does this and I cant find one. Im trying to delete cell A2:BC51 across sheet1, Sheet2, Sheet3, Sheet4 with one button. Thanks for your help!

This is the closest I could find to what im looking for:

function clearRange() {
  //replace 'Sheet1' with your actual sheet name
  var sheet = SpreadsheetApp.getActive().getSheetByName('Sheet1');
  sheet.getRange('A2:BC51').clearContent();
}
2
What you have looks correct and like in Excel, I don't think it's possible to create a range across sheets so you'll have to do each Sheet 1 at a time.Gordon

2 Answers

1
votes
function clearRange() {
  var range = "A2:BC51";
  SpreadsheetApp.getActive().getSheetByName('Sheet1').getRange(range).clearContent();
  SpreadsheetApp.getActive().getSheetByName('Sheet2').getRange(range).clearContent();
  SpreadsheetApp.getActive().getSheetByName('Sheet3').getRange(range).clearContent();
  SpreadsheetApp.getActive().getSheetByName('Sheet4').getRange(range).clearContent();
}
-1
votes

You could perhaps try.

function clearSheets() {
  clearRange('Sheet1');
  clearRange('Sheet2');
  clearRange('Sheet3');
  clearRange('Sheet3');
}

function clearRange(strSheet) {
 //replace 'Sheet1' with your actual sheet name
 var sheet = SpreadsheetApp.getActive().getSheetByName(strSheet);
 sheet.getRange('A2:BC51').clearContent();
}

I've just replaced 'Sheet1' with a variable called strSheet and put clearRange(strSheet) so that the sheet name can be passed to the function.

You could also do clearRange(strSheet, strRange) so that you can call the function clearRange('Sheet1', 'A2:BC51');

it will make it easier to update the code should the range change.

I've not tested this but it might work.