Google-sheets – How to use 3 separate sheet specific onEdit formulas

google sheetsgoogle-apps-script

So I have 3 separate sheets in on Google Sheet Doc, I have a script running on the first sheet that hides a row when a drop down is ammended:

var SHEET = "Tweaks Needed";
var VALUE = "All Tweaks Made";
var COLUMN_NUMBER = 7

function onEdit(e) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var activeSheet = ss.getActiveSheet();

if(SHEET == activeSheet.getName()){
  var cell = ss.getActiveCell()
  var cellValue = cell.getValue();

  if(cell.getColumn() == COLUMN_NUMBER){
    if(cellValue == VALUE){
      activeSheet.hideRow(cell);
   };
  };
 }; 
}

I have then generated a second script as follows:

var SHEET = "Standalone";
var VALUE = "Correct";
var COLUMN_NUMBER = 5

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();

  if(SHEET == activeSheet.getName()){
    var cell = ss.getActiveCell()
    var cellValue = cell.getValue();

    if(cell.getColumn() == COLUMN_NUMBER){
      if(cellValue == VALUE){
        activeSheet.hideRow(cell);
      };
    };
  };
}

When I add this second script, it completely overrides the first one, causing it to not function. I do have a third script however this is the same concept.

Is there a way to combine these two scripts so that both work accordingly?

Best Answer

onEdit scripts are per google sheet documents, not per individual tab/spreadsheet. You should change your flow so that it follows something like this:

function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();

  if("Standalone" == activeSheet.getName()){
    // logic for sheet Standalone       
  } else if ("Tweaks Needed" == activeSheet.getName()) {
    // logic for sheet Teaks Needed
  }
};