Search code examples
javascriptgoogle-apps-scriptgoogle-sheetstimestampcomma-operator

Google Sheet - Add Timestamp When Any Cell on Specific Sheet is Modified


I have a Google Sheet file that has many tabs (sheets) in it. What I am looking to do is place a timestamp when any cell on a specific page is updated or modified.

Sheet name = "Data-Dashboard"
Cell to place timestamp on "Data-Dashboard" is B1.

I wonder if having the timestamp on the sheet that is updating the timestamp on edit if it will create a loop so then I can exclude row 1 and just monitor the remaining rows and columns.

Here is my starting point:

function onEdit(e) {
  var col = e.range.getColumn();
   if(col == 1, 2, 3, 4, 5, 6) {
    e.source.getActiveSheet().getRange(1,2).setValue(new Date())

That puts the timestamp in the correct place and works when I modify anything in rows A-F. But it applies to all sheets, I need to limit it to a single sheet. Any help would be appreciated!


Solution

  • Try this:

    function onEdit(e) {
      const sh = e.range.getSheet();
      var col = e.range.getColumn();
      if (sh.getName() == "Your Sheet Name" && e.range.columnStart < 7 && e.range.rowStart > 1) {
        sh.getRange(1, 2).setValue(new Date());
      }
    }