Search code examples
google-sheets

Clear row of cells within a range after another cell is edited - Google Sheets


The below code will clear cells F4 to P4 once E4 is changed. How can I make this code apply to more than just E4? i.e. If E5 is changed then clear F5 to P5, if E6 is changed then clear F6 to P6, etc. Thank you!

function onEdit(e){
  if(e.range.getA1Notation() != "E4") return;
  e.source.getActiveSheet().getRange("F4:p4").clearContent()
 }

Solution

  • You can try working with numbers of rows and columns. In the first IF I've checked if the column is not number 5 (meaning, column E) or row is less than 4 (please, change it if you need to modify E3, E2 or E1); and then make it clear from the 6th columns the next 11 columns:

    function onEdit(e){
      if(e.range.getColumn() != 5 || e.range.getRow() < 4) return;
      e.source.getActiveSheet().getRange(6,e.range.getRow(),11,1).clearContent()
    }