Home > Net >  Use script only on specific sheet not on all sheets in Google sheets
Use script only on specific sheet not on all sheets in Google sheets

Time:12-16

I am using the following script to make a stamp with editors email in column 6 when column 7 is changed

function onedit(e) {
  var r = e.range;
  if( r.getColumn() != 6 ) { //checks the column
    var time = new Date();
    time = Utilities.formatDate(time, "GMT-07:00", "yyyy-MM-dd, hh:mm:ss");
    SpreadsheetApp.getActiveSheet().getRange(r.getRow(),6).setValue(Session.getActiveUser().getEmail());
  }
 }

But it functions on all sheets. I would like it to only function on a sheet named VD Gaps. Can someone help out with that.

CodePudding user response:

You can make the onEdit(e) function only do its thing on the 'VD Gaps' sheet like this:

function onEdit(e) {
  if (e.range.columnStart !== 7
    || !e.range.getSheet().getName().match(/^(VD Gaps)$/i)) {
    return;
  }
  e.range.offset(0, -1).setValue(Session.getActiveUser().getEmail());
}

Please note that the onEdit(e) function is a simple trigger. Simple triggers run in a restricted context where the identity of the user at the keyboard is usually not available. "They may or may not be able to determine the identity of the current user, depending on a complex set of security restrictions." See Session.getActiveUser() and Session.getEffectiveUser().

  • Related