Sgtmullet
Sgtmullet

Reputation: 335

Extend timestamp script to multiple columns and only update if cell was previous empty

I am using this script to automatically add a timestamp into Col "J" (E-Date) when anything is entered into Col "I" (Emailed) and it works pretty well:

function onEdit(event)
{ 
  var timezone = "GMT+2";
  var timestamp_format = "MM-dd-yy";
  var updateColName = "Emailed";
  var timeStampColName = "E-Date";
  var sheet = event.source.getActiveSheet();

  var actRng = event.source.getActiveRange();
  var editColumn = actRng.getColumn();
  var index = actRng.getRowIndex();
  var headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues();
  var dateCol = headers[0].indexOf(timeStampColName);
  var updateCol = headers[0].indexOf(updateColName); updateCol = updateCol+1;
  if (dateCol > -1 && index > 1 && editColumn == updateCol) { 
    var cell = sheet.getRange(index, dateCol + 1);
    var date = Utilities.formatDate(new Date(), timezone, timestamp_format);
    cell.setValue(date);
  }
}

Here is a link to the Google sheet, simply add anything to Col "I" (Emailed) and you will see the date being added - https://docs.google.com/spreadsheets/d/15_rDiujP-N1nNeFLIjbpVdU3bVZOftC5TdMvqD6EZJ8/edit?usp=sharing

However, I would like to extend this script so that it also works with the "SMSed" column (adds timestamp to "S-Date) and the "Phoned" column (adds timestamp to "P-Date"). In addition to that, for ONLY the "Emailed" and "SMSed" columns, it must only add/update the date if the cell was previously empty.

Thanks!

Upvotes: 0

Views: 794

Answers (1)

JPV
JPV

Reputation: 27262

Remove all onEdit() scripts you have and give this a try

function onEdit(e) {
var cols, ind;
cols = [9, 11, 13];
ind = cols.indexOf(e.range.columnStart);
if (ind === -1 || typeof e.value === 'object') return;
var v = (ind === 0) ? new Date() : (e.range.offset(0, 1).getValue())? null : new Date();
if (v) e.range.offset(0, 1).setValue(v);
}

Format the column with the timestamp as you wish (via the 123-button in the spreadsheet).

Upvotes: 0

Related Questions