Search code examples
google-apps-scriptgoogle-sheets

How to stop a TimeStamp from changing on every edits using Apps Script?


I require some guidance regarding Google Apps Script, as I am a novice and facing some challenges while constructing a script. Presently, I have produced a script that registers the timestamp whenever there is an alteration in specific columns across two distinct tabs. However, I am encountering an issue where the timestamp changes every time there is an edit in those cells. My objective is to create a script that captures the timestamp only on the initial edit and does not alter it subsequently. Below, you can find my current script. I would greatly appreciate any assistance that can be extended.

function onEdit() {
  var s = SpreadsheetApp.getActiveSheet();
  if( s.getName() == "Production Activity" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 25 ) { //checks the column
      var nextCell = r.offset(0, 1);
      //if( nextCell.getValue() !== '' ) //is empty?
      nextCell.setValue(new Date());
    }

    if( r.getColumn() == 27 ) { //checks the column
      var nextCell = r.offset(0, 1);
      //if( nextCell.getValue() !== '' ) //is empty?
      nextCell.setValue(new Date());
    }
  }
  if( s.getName() == "Non-Production Activity" ) { //checks that we're on the correct sheet
    var r = s.getActiveCell();
    if( r.getColumn() == 9 ) { //checks the column
      var nextCell = r.offset(0, 1);
      //if( nextCell.getValue() !== '' ) //is empty?
      nextCell.setValue(new Date());
    }

    if( r.getColumn() == 11 ) { //checks the column
      var nextCell = r.offset(0, 1);
      //if( nextCell.getValue() !== '' ) //is empty?
      nextCell.setValue(new Date());
    }
  }
}

Solution

  • In your situation and script, how about using isBlank() method of Class Range? When this is reflected in your script, how about the following modification?

    Modified script:

    function onEdit() {
      var s = SpreadsheetApp.getActiveSheet();
      if (s.getName() == "Production Activity") { //checks that we're on the correct sheet
        var r = s.getActiveCell();
        if (r.getColumn() == 25) { //checks the column
          var nextCell = r.offset(0, 1);
          if (nextCell.isBlank()) {
            nextCell.setValue(new Date());
          }
        }
    
        if (r.getColumn() == 27) { //checks the column
          var nextCell = r.offset(0, 1);
          if (nextCell.isBlank()) {
            nextCell.setValue(new Date());
          }
        }
      }
      if (s.getName() == "Non-Production Activity") { //checks that we're on the correct sheet
        var r = s.getActiveCell();
        if (r.getColumn() == 9) { //checks the column
          var nextCell = r.offset(0, 1);
          if (nextCell.isBlank()) {
            nextCell.setValue(new Date());
          }
        }
    
        if (r.getColumn() == 11) { //checks the column
          var nextCell = r.offset(0, 1);
          if (nextCell.isBlank()) {
            nextCell.setValue(new Date());
          }
        }
      }
    }
    
    • When I saw your script and question, when the event object is used, I thought that the script might be simple. When this is reflected in the script, how about the following modified script?

      function onEdit(e) {
        const obj = {
          "Production Activity": [25, 27],
          "Non-Production Activity": [9, 11]
        };
        const { range } = e;
        const o = obj[range.getSheet().getSheetName()];
        if (o && o.includes(range.columnStart)) {
          const dstRange = range.offset(0, 1);
          if (dstRange.isBlank()) {
            dstRange.setValue(new Date());
          }
        }
      }
      

    References: