0

I'm sure there is a very simple answer to this question;

I have a function that works perfectly, however it only works if the function name is 'onEdit' which is not what i want as i want to create similar functions for different tabs, hence the first line checking the sheet name, can someone please point out the error of my ways :-) thanks.

function onEdit(e) {
if (e.range.getSheet().getName() != "PrintNoteLines") {  
  return}
    var sh = e.source.getActiveSheet()
    if (sh.getName().indexOf('PrintNoteLines') > -1 && e.range.getA1Notation() == 'B1' && e.value) {
        sh.getRange('A25:A183')
                .setDataValidation(SpreadsheetApp.newDataValidation()
                .requireValueInRange(e.source.getRangeByName(e.value), true)
                .setAllowInvalid(false)
                .build());                                                                         }
                        }
Marios
  • 26,333
  • 8
  • 32
  • 52
Prospidnick
  • 133
  • 1
  • 10

2 Answers2

3
  • onEdit is a key word defining that the function is bound to a simple onEdit trigger

  • If you want to rename the function, but maintain the functionality, you can use an installable onEdit trigger instead

  • See here how to bind it to your renamed function

  • Mind that having more than one function runing on onEdit can lead to conflicts and it mostly not necessary

  • You can call other functions from inside the onEdit triggerred funciton if necessary

Sample:

function onEdit(e) {
if (e.range.getSheet().getName() == "A") {  
  function1();
  }
else if (e.range.getSheet().getName() == "B") {  
  function2();
  }
else {  
  function3();
  }
}
function function1(){
 Logger.log("The sheet name is A");
}
function function2(){
 Logger.log("The sheet name is B");
}
function function3(){
 Logger.log("The sheet name is different");
}
ziganotschka
  • 25,866
  • 2
  • 16
  • 33
1

You have two options:

  • You can either create additional if conditions to your existing onEdit(e) function to execute different code when different sheets are edited.
  • You can create a main onEdit(e) trigger function that feeds all the other functionName(e).

For example:

Rename the function of your current code to myFunction1(e):

function myFunction1(e) {
if (e.range.getSheet().getName() != "PrintNoteLines") {  
  return}
    var sh = e.source.getActiveSheet()
    if (sh.getName().indexOf('PrintNoteLines') > -1 && e.range.getA1Notation() == 'B1' && e.value) {
        sh.getRange('A25:A183')
                .setDataValidation(SpreadsheetApp.newDataValidation()
                .requireValueInRange(e.source.getRangeByName(e.value), true)
                .setAllowInvalid(false)
                .build());                                                                         }
                        }

and then feed the event object from the onEdit(e) function like that:

function onEdit(e){
myFunction1(e) 
}

In this way you can have multiple functions being executed after an edit is made:

function onEdit(e){
myFunction1(e);
myFunction2(e);
myFunction3(e)
...

}
Marios
  • 26,333
  • 8
  • 32
  • 52