0
votes

I am trying to run two onEdit on the same sheet but only one works at a time:

First function:

function onEdit(e) {
if ([8,9].indexOf(e.range.columnStart) != -1) {
e.range.offset(0, 2).setValue(new Date());
}
}

Second function:

function onEdit(e) {
var value = (typeof e.value == "object" ? e.range.getValue() : e.value);
if (e.range.columnStart == 14 && value == "ISSUED") {
e.range.offset(0,1).setValue(new Date());
}
}

I am an entry level when it comes to performing script, any assistance? Thank you in advance for the help!

2

2 Answers

1
votes

you have two options. here is example of hybryd code:

nested:

function onEdit(e) {
  moveRowsFromSheetToSheet_(e);
}

function moveRowsFromSheetToSheet_(e) {
  // start of code
  if(condition == met) {
    //operation to follow
  } //closes if statement operations; no close to moveRowsFromSheetToSheet_()

  function moveRowsFromSheetToSheet1_() {
    //start of code
    if(condition == met){   
      //operation to follow
    } //closes if statement operation
  } //closes moveRowsFromSheetToSheet1_()
  moveRowsFromSheetToSheet1_; //calls moveRowsFromSheetToSheet1_() so that it will run after moveRowsFromSheetToSheet_() code
} // closes moveRowsFromSheetToSheet_()

unnested:

function onEdit(e) {
  moveRowsFromSheetToSheet_(e);
  moveRowsFromSheetToSheet1_();
}

function moveRowsFromSheetToSheet_(e) {
  // start of code
  if(condition == met) {
    //operation to follow
  } //closes if statement operations


function moveRowsFromSheetToSheet1_() {
  //start of code
  if(condition == met){   
    //operation to follow
  }
}
1
votes

The most straightforward approach in your case is to use your current functions and wrap them around a single onEdit trigger as you can only have one per project:

function onEdit(e){
  myFunction1(e);
  myFunction2(e);
}

function myFunction1(e) {
if ([8,9].indexOf(e.range.columnStart) != -1) {
e.range.offset(0, 2).setValue(new Date());
}
}

function myFunction2(e) {
var value = (typeof e.value == "object" ? e.range.getValue() : e.value);
if (e.range.columnStart == 14 && value == "ISSUED") {
e.range.offset(0,1).setValue(new Date());
}
}

Just save the code and it will work upon edits.