Google Sheets – How to Print First Modified Date of a Cell

google sheetsgoogle-apps-scriptgoogle-sheets-timestamp

I would like to make a script that works like this script to print a timestamp when a cell was last updated but in this case I want to return when it was first updated, not last.

Here is one attempt:

function onEdit(cellOrigin,cellDestination) { 
var s = SpreadsheetApp.getActiveSheet(); // Get spreadsheet name 
var r = s.getActiveCell(); // store active cell name in current spreadsheet 
var cell1 = cellOrigin // This is the row I want to put values
if(r.getRow() != cell1) { // Ignores this row (where I put the dates)
var column = r.getColumn();  // Get column # from active cell
var time = new Date(); // Get date and time of last mod
time = Utilities.formatDate(time, "GMT-08:00", "MM/DD/yy, hh:mm:ss"); // Format date and time
SpreadsheetApp.getActiveSheet().getRange(cellDestination).setValue(time); // put in time in cell
};
};

Best Answer

Try something like this

// Global variables
var origin = 'A1';
var destination = 'B1';

function onEdit(e){

  if(e.range.getA1Notation() == origin && 
     e.range.getSheet().getRange(destination).isBlank()){
    e.range.getSheet().getRange(destination).setValue(new Date());
  }
}