google sheets script onedit sort for multiple sheets code example

Example: google sheets script onedit sort for multiple sheets

function onEdit(event){
  	var ss = SpreadsheetApp.getActiveSpreadsheet();
  	var sheet = event.source.getActiveSheet().getName()
  	var editedCell = event.range.getSheet().getActiveCell();
	if(sheet=="Loan Inquiries"){
  		var columnToSortBy = 2;
  		var tableRange = "A3:T99"; //range to be sorted
  		if(editedCell.getColumn() == columnToSortBy){   
    		var range = ss.getActiveSheet().getRange(tableRange); 
    		range.sort( { column : columnToSortBy, ascending: true } );
  		}
    }
  	else if(sheet=="Deals in Escrow"){
    	var columnToSortBy = 7;
    	var tableRange = "A3:T99"; //range to be sorted
  		if(editedCell.getColumn() == columnToSortBy){   
     		var range = ss.getActiveSheet().getRange(tableRange); 
    		range.sort( { column : columnToSortBy, ascending: true } );
  		}
    }
  	else{return}
}

Tags:

Misc Example