title: Google sheets script for erasing a timestamp after the cell is cleared
body: <p>I have a sheet counting drinks. I need to timestamp every drink entered by the user in separate column. I have done this using the script below. My problem is when the user deletes a drink (cell empty again) the timestamp is still there and messes with the calculations. I need the timestamp to be erased when the cell is erased.</p>
  
  <pre class="lang-js prettyprint-override"><code>var timezone = "GMT";
  var timestamp_format = "k:mm"; // Timestamp Format. 
  var datestamp_format = "d/M";
  var sheet = event.source.getSheetByName('My sheet'); //Name of the sheet where you want to run this script.
  
  var questionArray = ['drink 1', 'drink 2', 'drink 3','drink 4', 'drink 5', 'drink 6','drink 7', 'drink 8', 'drink 9','drink 10', 'drink 11', 'drink 12','drink 13','drink 14'];
  var responseArray = ['Timestamp 1', 'Timestamp 2', 'Timestamp 3', 'Timestamp 4', 'Timestamp 5', 'Timestamp 6', 'Timestamp 7', 'Timestamp 8','Timestamp 9','Timestamp 10','Timestamp 11', 'Timestamp 12', 'Timestamp 13', 'Timestamp 14'];
  
  var actRng = event.source.getActiveRange();
  var editColumn = actRng.getColumn();
  var index = actRng.getRow();
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();//get values and place them inside array 
  var dateCol = headers[0].indexOf(timeStampColName); //get index position inside the array
  
  for(var i = 0; i &lt; questionArray.length ; i ++){   
     if (headers[0].indexOf(responseArray[1]) &gt; -1 &amp;&amp; index &gt; 1 &amp;&amp; editColumn == (headers[0].indexOf(questionArray[i])+1) ) { // only timestamp if 'Last Updated' header exists, but not in the header row itself!
         var cell = sheet.getRange(index, (headers[0].indexOf(responseArray[i])+1) ); 
         var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
         cell.setValue(date); 
     }
  }
  </code></pre>