user1588989
user1588989

Reputation: 1

onEdit timestamp for specific sheet

I am looking for help with a script that posts that a specific sheet has been edited rather than when any sheet in the document is modified.

Here is what I currently have:

function onEdit() {
  var d = new Date();
  var h = d.getHours();
  var min = d.getMinutes();
  var sec = d.getSeconds();
  var h_str = h;
  var min_str = min;
  var sec_str = sec;

// format time nicely
  if (h < 10) 
    h_str = '0' + h;
  if (min < 10) 
    min_str = '0' + min;
  if (sec < 10) 
    sec_str = '0' + sec;

// create the formatted time string
  var time_str = h_str + ':' + min_str + ':' + sec_str;

// create the message
  var s = 'Roster last modified on:     ' + d.toDateString() + ' @ ' + time_str;

// change the range
  SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Infernal Dawn').getRange("A1").setValue(s);}

This works perfectly fine to only update the sheet "Infernal Dawn" with the modification date, yet it also updates when ANY sheet is modified.

I found that there were 2 different ways of doing this (one using the gid). The only problem is that my skills in coding are extremely limited and I couldn't figure out how to integrate it into the above code. Any help would be appreciated!

Upvotes: 0

Views: 871

Answers (1)

user1525598
user1525598

Reputation: 218

The function onEdit has an optional argument that remembers which cell\range\sheet was edited. The structure of this argument is described here.

function onEdit(e) 
{ var d=new Date();
  var s=d.toLocaleString();
  if(e.source.getActiveSheet().getName()=="TheSheetThatWeWishToTrace")
  { SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Infernal Dawn').getRange("A1").setValue(s);}
}

Upvotes: 1

Related Questions