0

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!

Marios
  • 26,333
  • 8
  • 32
  • 52

2 Answers2

1

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
  }
}
player0
  • 124,011
  • 12
  • 67
  • 124
  • Thanks; based in your comment, I would just combine my two functions into one single script? Sorry, I’m not familiar with unnested or nested but I will read up on it. Would you be able to guide me and use my formulas as example and demonstrate what it would look like when combined? Thanks again! – Simon Tse Jan 10 '21 at 20:09
1

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.

Marios
  • 26,333
  • 8
  • 32
  • 52