Home > database >  Adding OR in OnEdit Issue
Adding OR in OnEdit Issue

Time:08-31

I am trying to tweak a script (which seems like an easy fix), but can't seem to figure out why it is not working.

In English -- I'm looking to create this movement

"If Col A is marked as "In Progress", put a timestamp in Column C" "If Col B is marked as TRUE, put a timestamp in Column D"

This script works like a charm if I was designating both values in one column -- meaning both IN PROGRESS, or TRUE, would be in column A

function tryInsertDate(e, rangeToSet, val) {
    const sheet = e.range.getSheet();
    if (sheet.getName() !== 'test' || sheet.getRange('a'   e.range.rowStart).getValue() !== val ) 
    {return;}
    
    
    sheet.getRange(rangeToSet   e.range.rowStart).setValue(new Date()).setNumberFormat('MM/dd/yyyy');
}
function onEdit(e) {
    tryInsertDate(e, 'c', 'In Progress');
    tryInsertDate(e, 'd', true);
}

I thought adding another IF statement (like below) would do the trick (one of the many things I've tried).. but it's not running at all when I make the change:

function tryInsertDate(e, rangeToSet, val) {
    const sheet = e.range.getSheet();
    if (sheet.getName() !== 'test' || sheet.getRange('a'   e.range.rowStart).getValue() !== val || sheet.getRange('b'   e.range.rowStart).getValue() !== val) 
    {return;}
    
    
    sheet.getRange(rangeToSet   e.range.rowStart).setValue(new Date()).setNumberFormat('MM/dd/yyyy');
}
function onEdit(e) {
    tryInsertDate(e, 'c', 'In Progress');
    tryInsertDate(e, 'd', true);
}

I'm very new to this, but the way I read it is.. "If the sheet name doesn't equal "test", if the column is anything aside from A or B, do nothing, if not -- put a timestamp".

Any thoughts on why this isn't running??

CodePudding user response:

Try this:

function onEdit(e) {
  const sh = e.range.getSheet();
  if(sh.getName() == "Sheet0" && e.range.columnStart == 1 && e.value == "In Progress") {
    e.range.offset(0,2).setValue(new Date());
  }
  if(sh.getName() == "Sheet0" && e.range.columnStart == 2 && e.value == "TRUE") {
    e.range.offset(0,2).setValue(new Date())
  }
}

Demo:

enter image description here

  • Related