Auto Set DateTime for New Rows in Google Sheets

google sheets

I have a sheet. What I am trying to do is when I click on a new row to automatically set the first column in the row to =now().

I tried setting =Now() in the cell but it appears to be updating it all time time.

So I tried doing some kind of if statement. IF the next cell is empty then I set the first cell to now. If its not then I leave it to the original value.

=IF(ISBLANK(CONCAT("b",ROW())), NOW(), CONCAT("a",ROW()))

This didn't work as it becomes a circular reference.

Is there a way to configure my sheet so that the first column is always filled out with the current date? the time stamp of when the row was created.

Best Answer

function onEdit(e) {
  e.range
   .getSheet()
   .getRange(e.range.getRow(), 1, e.range.getNumRows())
   .setValue(new Date())
   .setNumberFormat('dd.MM.yy, hh:mm:ss');
}

demo sheet

or try this:

function onEdit(e) {

  var range = e.range;
  var sheet = range.getSheet();
  var row = range.getRow();
  var rows = range.getNumRows();
  var date = new Date();

  var dates = sheet
      .getRange(row, 1, rows)
      .getValues()
      .map(function(row) {return [row[0] || date];});

  sheet
   .getRange(row, 1, rows)
   .setValues(dates)
   .setNumberFormat('dd.MM.yy, hh:mm:ss');
}