javascript - Javascript - 在google app脚本中,组合两个onEdit()函数

列B中的工作表值取决于A,列i中的值取决于H,如果我运行一个脚本,第二个不能工作,


function onEdit(){


 var tabLists ="source1";


 var tabValidation ="main";


 var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();


 var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);



 var activeCell = ss.getActiveCell();



 if(activeCell.getColumn() == 1 && 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);



 } 



 }



}


function onEdit(){


 var tabLists ="source2";


 var tabValidation ="MAINTENANCE";


 var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();


 var datass = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(tabLists);



 var activeCell = ss.getActiveCell();



 if(activeCell.getColumn() == 8 && 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);



 } 



 }



}


 

时间:

试试这个:


function onEdit(e){


 var sh=e.range.getSheet();


 if(e.range.columnStart==1 && e.range.rowStart>1 && sh.getName()=='main'){


 var srcsh=e.source.getSheetByName('source1');


 e.range.offset(0,1).clearContent().clearDataValidations();


 var makes=srcsh.getRange(1, 1, 1, srcsh.getLastColumn()).getValues();


 var makeIndex=makes[0].indexOf(e.value)+1;


 if(makeIndex!=0){


 var validationRange=srcsh.getRange(3, makeIndex,srcsh.getLastRow());


 var validationRule=SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();


 e.range.offset(0,1).setDataValidation(validationRule);


 } 


 }


 if(e.range.columnStart==8 && e.range.rowstart>1 && sh.getName()=='MAINTENANCE'){


 var srcsh=e.source.getSheetByName('source2');


 e.range.offset(0,1).clearContent().clearDataValidations();


 var makes=srcsh.getRange(1,1,1,srcsh.getLastColumn()).getValues();


 var makeIndex=makes[0].indexOf(e.value()) + 1;


 if(makeIndex!=0){


 var validationRange=srcsh.getRange(3,makeIndex,srcsh.getLastRow());


 var validationRule=SpreadsheetApp.newDataValidation().requireValueInRange(validationRange).build();


 e.range.offset(0, 1).setDataValidation(validationRule);


 } 


 }


}



...