Home > front end >  Insert or delete a timestamp based on cell value
Insert or delete a timestamp based on cell value

Time:12-20

With this script I can create automatically a timestamp in col G based on "SI" in col E.
Now I would that:
1 - When I delete an existing value "SI" in col E, the timestamp must be deleted
2 - And so, if I put a different value from "SI" in col E, the timestamp must be deleted
How could I proceed?

function onEdit(e){
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "STAMPA2" )
  if(e.range.columnStart == 5 && e.value === 'SI'){
     e.range.offset(0,2).setValue(new Date());
  }
}

CodePudding user response:

I believe your goal is as follows.

  • When the value of SI is put to the column "E", you want to put the current date to the column "G".
  • When the value of SI is not put to the column "E", you want to delete the value of the column "G".

In this case, how about the following modification?

Modified script:

function onEdit(e) {
  var { range, value } = e;
  var s = range.getSheet();
  if (s.getName() == "STAMPA2" && range.columnStart == 5) {
    range.offset(0, 2).setValue(value === 'SI' ? new Date() : "");
  }
}

or

function onEdit(e) {
  var { range } = e;
  var s = range.getSheet();
  if (s.getName() == "STAMPA2" && range.columnStart == 5) {
    range.offset(0, 2).setValue(range.getValue() === 'SI' ? new Date() : "");
  }
}

CodePudding user response:

function onEdit(e) {
  var s = e.range.getSheet();
  if (s.getName() == "STAMPA2" && e.range.columnStart == 5) {
    if (e.value == 'SI') {
      e.range.offset(0, 2).setValue(new Date());
    } else {
      e.range.offset(0, 2).setValue('');
    }
  }
}
  • Related