0

I am clearing the content of specific ranges of cell through this code:

function ClearContents() {
  var sheet = SpreadsheetApp.getActive().getSheetByName('Data_Entry');
      
  sheet.getRange('B5:I21').clearContent();
  sheet.getRange('B24:I36').clearContent();
  sheet.getRange('B39:I53').clearContent();
  sheet.getRange('B56:I68').clearContent();
  sheet.getRange('B71:I90').clearContent();
  sheet.getRange('B93:I109').clearContent();
  sheet.getRange('B112:I126').clearContent();
  sheet.getRange('B129:I140').clearContent();
  sheet.getRange('B143:I157').clearContent();
  sheet.getRange('B160:I174').clearContent();
  sheet.getRange('B177:I188').clearContent();
  sheet.getRange('B191:I204').clearContent();
  sheet.getRange('B207:I220').clearContent();
  sheet.getRange('B223:I234').clearContent();
  sheet.getRange('B237:I248').clearContent();
sheet.getRange('B251:I262').clearContent();
sheet.getRange('B265:I275').clearContent();
sheet.getRange('B278:I287').clearContent();
sheet.getRange('B5:I21').clearContent();
 
};

And I would like to make it cleaner code by using a variable that contains all of these ranges with this code:

var ranges = sheet.getRange[sheet.getRange('B5:I21'),  sheet.getRange('B24:I36'),  sheet.getRange('B39:I53'),  sheet.getRange('B56:I68'),  sheet.getRange('B71:I90'),  
sheet.getRange('B93:I109'),  sheet.getRange('B112:I126'),  sheet.getRange('B129:I140'),  
sheet.getRange('B143:I157'),  sheet.getRange('B160:I174'),  sheet.getRange('B177:I188'),  
sheet.getRange('B191:I204'),  sheet.getRange('B207:I220'),  sheet.getRange('B223:I234'),  
sheet.getRange('B237:I248'),sheet.getRange('B251:I262'),sheet.getRange('B265:I275'),
sheet.getRange('B278:I287'),sheet.getRange('B5:I21')];
  
  ranges.clearContent();

But it doesn't seem to work

1

solved

var ranges=sheet.getRangeList(['B5:I21' ,'B24:I36' , 'B39:I53' , 'B56:I68', 'B71:I90' , 'B93:I109' , 'B112:I126' , 'B129:I140' , 'B143:I157' , 'B160:I174' ,'B177:I188' , 'B191:I204' , 'B207:I220' , 'B223:I234' , 'B237:I248','B251:I262','B265:I275','B278:I287','B5:I21']);

ranges.clearcontent();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.