title: Timestamp is generated when cell is not empty. If blank, timestamp is cleared
body: <p>What I am trying to create a script which is when you enter a value in column 2, it generates a timestamp and timestamp clears itself if cell is blank.</p>
  
  <p>I trying to combine "my code" and "other code".
  The Problem is, when I clear the cell in Column 2 the timestamp still remains in Column 1.</p>
  
  <p>How do I fix my code to make it work?</p>
  
  <p>If anybody can help, power to you.</p>
  
  <p>EDIT: already answered by @Raserhin</p>
  
  <p>My code: <a href="https://support.google.com/docs/thread/18439710?msgid=18439710&amp;authuser=0" rel="nofollow noreferrer">original source</a></p>
  
  <pre><code>function onEdit(e) {
      var s = e.source.getActiveSheet(),
          watchCols = [2],
          offsetCol = [-1],
          ind = watchCols.indexOf(e.range.columnStart)
          if (s.getName() !== "work" || ind === -1) return;
      e.range.offset(0, offsetCol[ind])
          .setValue(new Date());
  }
  </code></pre>
  
  <p>Other code I trying to combine with.
  <a href="https://stackoverflow.com/questions/54259009/how-to-modify-current-insert-timestamp-script-to-remove-timestamp-when-cell-is-c">How to modify current insert timestamp script to remove timestamp when cell is cleared</a></p>
  
  <pre><code>function onEdit(e) {
    var rg=e.range;
    var sh=e.range.getSheet();
    var name=sh.getName();
    if(name=="CHECK OUT" || name== "CHECK IN" || name== "MISC OUT" || name=="MISC IN"){  
      if(rg.columnStart==1) {
        var vA=rg.getValues();
        for(var i=0;i&lt;vA.length;i++){
          if(vA[i][0]) {
            sh.getRange(rg.rowStart + i,8).setValue(new Date());
          }else{
            sh.getRange(rg.rowStart + i,8).setValue('');    
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>My combined code:
  <a href="https://docs.google.com/spreadsheets/d/13zGg9TUFQcqapG6-ZxB3CPJNwFsisxT98gbkrqU0Wns/edit?usp=sharing" rel="nofollow noreferrer">https://docs.google.com/spreadsheets/d/13zGg9TUFQcqapG6-ZxB3CPJNwFsisxT98gbkrqU0Wns/edit?usp=sharing</a></p>
  
  <pre><code>function onEdit(e) {
      var rg=e.range;
      var sh=e.range.getSheet();
      var name=sh.getName();
      var s = e.source.getActiveSheet(),
          watchCols = [2],
          offsetCol = [-1],
          ind = watchCols.indexOf(e.range.columnStart)
          if (s.getName() !== "work" || ind === -1) return;
      e.range.offset(0, offsetCol[ind])
          .setValue(new Date());
      if(rg.columnStart==1) {
      var vA=rg.getValues();
      for(var i=0;i&lt;vA.length;i++){
        if(vA[i][0]) {
          sh.getRange(rg.rowStart + i,8).setValue(new Date());
        }else{
          sh.getRange(rg.rowStart + i,8).setValue('');    
        }
      }
    }
  }
  </code></pre>
  
  <p>Edit:
  Thanks to @Raserhin for noticing my mistake.</p>
  
  <pre><code>//Working code
  function onEdit(e) {
    var rg=e.range;
    var sh=e.range.getSheet();
    var name=sh.getName();
    var s = e.source.getActiveSheet(),
        watchCols = [2],
        offsetCol = [-1],
        ind = watchCols.indexOf(e.range.columnStart)
    if (s.getName() !== "work" || ind === -1) return;
    e.range.offset(0, offsetCol[ind])
    .setValue(new Date());
    if(rg.columnStart==2) { // Change 1 to 2
      var vA=rg.getValues();
      for(var i=0;i&lt;vA.length;i++){
        if(vA[i][0]) {
          sh.getRange(rg.rowStart + i,1).setValue(new Date()); // Change 8 to 1 
        }else{
          sh.getRange(rg.rowStart + i,1).setValue('');    // Change 8 to 1
        }
      }
    }
  }
  </code></pre>