0

I made a dependent drop-down with a script. I'd like to apply it to two sheets. but only OnEdit1 works and onEdit2 doesn't work. I don't know what to do with both sheets. Do I have to add or do something to make two sheets work?

function onEdit(e){
  
  oneEdit1(e);
  var tabLists = "lists";
  var tabValidation = "Question_B";
  var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);
  
  var activeCell = ss.getActiveCell();
  
  if(activeCell.getColumn() == 4 && activeCell.getRow() > 1 && ss.getSheetName() == tabValidation){
    
    activeCell.offset(0, 1).clearContent().clearDataValidations();
    
    var makes = datass.getRange(1, 1, 1, datass.getLastColumn()).getValues();
    
    var makeIndex = makes[0].indexOf(activeCell.getValue()) + 1;
    
    if(makeIndex != 0){
    
        var validationRange = datass.getRange(3, makeIndex, datass.getLastRow());
        var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
        activeCell.offset(0, 1).setDataValidation(validationRule);
       }  
      
  }
  
  oneEdit2(e);
  var tabListss = "lists2";
  var tabValidations = "Qeustion_R";
  var sss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
  var datasss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabListss);
  
  var activeCell = sss.getActiveCell();
  
  if(activeCell.getColumn() == 4 && activeCell.getRow() > 1 && ss.getSheetName() == tabValidations){
    
    activeCell.offset(0, 1).clearContent().clearDataValidations();
    
    var makess = datasss.getRange(1, 1, 1, datasss.getLastColumn()).getValues();
    
    var makeIndexs = makess[0].indexOf(activeCell.getValue()) + 1;
    
    if(makeIndex != 0){
    
        var validationRange = datasss.getRange(3, makeIndexs, datasss.getLastRow());
        var validationRule = SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();
        activeCell.offset(0, 1).setDataValidation(validationRule);  
    
    }

  }

}
chan
  • 11
  • 1

1 Answers1

0

Two combine two onEdit functions in one:

  • define both of them outside of the main function
  • call them from the main function

Sample:

function onEdit(e){
  oneEdit1(e);
  oneEdit2(e)
}
  
function oneEdit1(e){
 ...
      
}
  
function oneEdit2(e){
 ...

}
NightEye
  • 10,634
  • 2
  • 5
  • 24
ziganotschka
  • 25,866
  • 2
  • 16
  • 33