Home > Mobile >  how to get two on edit functions to work in the same google sheet script
how to get two on edit functions to work in the same google sheet script

Time:12-30

im trying to get google sheets to put a timestamp next to the edited sku and it works great.

only problem is it only works for the "Sales" tab and not the "In Log" tab. tried looking around at other solutions but its just not clicking for me so if someone could help attempt to make my monkey brain understand the problem that would be greatly appreciated.

here is the script

function onEdit(e){
  function onEdit1(e){
    var s = SpreadsheetApp.getActiveSheet();
    if( s.getName() == "In Log" ) { //checks that we're on Sheet1 or not
    var r = s.getActiveCell();
    if( r.getColumn() == 1 ) { //checks that the cell being edited is in column A
    var nextCell = r.offset(0, 4);
    if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?
    nextCell.setValue(new Date());
  }
  }
    else 
  }

  function onEdit2(e) {
    var s = SpreadsheetApp.getActiveSheet();
    if( s.getName() == "Sales" ) { //checks that we're on Sheet1 or not
    var r = s.getActiveCell();
    if( r.getColumn() == 1 ) { //checks that the cell being edited is in column A
    var nextCell = r.offset(0, 5);
    if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?
    nextCell.setValue(new Date());
  }
  }
  }
}

its a bit gross from copy pasting some code sorry about that.

here is a link to a copy of the google sheet: https://docs.google.com/spreadsheets/d/1nGPIKCjE6c_xJe7bDwhGIoFz5fJ-cpbTtavsUelxSW0/edit?usp=sharing

CodePudding user response:

Try joining them in one onEdit function with the conditionals

function onEdit(e){
  
    var s = SpreadsheetApp.getActiveSheet();
    if( s.getName() == "In Log" ) { //checks that we're on Sheet1 or not
    var r = s.getActiveCell();
    if( r.getColumn() == 1 ) { //checks that the cell being edited is in column A
    var nextCell = r.offset(0, 4);
    if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?
    nextCell.setValue(new Date());
  }
  }
  }
    else if( s.getName() == "Sales" ) { //checks that we're on Sheet1 or not
    var r = s.getActiveCell();
    if( r.getColumn() == 1 ) { //checks that the cell being edited is in column A
    var nextCell = r.offset(0, 5);
    if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?
    nextCell.setValue(new Date());
  }
  }
  }
  }
  • Related