0
votes

I would like to know how to modify my script to target a specif tab (Check) and not all tabs. Below is my script:

function onEdit(e) {    
  var sheet, cols, colInd, offset, format;   
  sheet = e.source.getActiveSheet();
  cols = [7,13,14,16];
  colInd = cols.indexOf(e.range.columnStart);
  if (colInd == -1) return;
  if (colInd < 3) {
    offset = [4,-1,1,1];
    format = "MM/dd/yyyy HH:mm:ss";
    e.range.offset(0, offset[colInd])
    .setValue(Utilities.formatDate(new Date(), "GMT+8", format));
  } else {
    setValidation(e);
  }
}

Here's my sample sheet: https://docs.google.com/spreadsheets/d/1hQhh3UlWm38ILADMqlCuv08GipMBDWVOF4l4gzM5tjE/edit#gid=2039637864

2
Does this answer your question? Script to run on specific sheet - Tedinoz

2 Answers

0
votes

After re-reading your question I think that what you mean is only executing the onEdit only when the Sheet Check is modified.

I that's the case you should just put an if statement to make sure that the edit is happening in the correct sheet. So using your original code that would work something like:

function onEdit(e) {    
  var cols, colInd, offset, format;   
  if(e.range.getSheet().getName() = 'Check'){
    cols = [7,13,14,16];
    colInd = cols.indexOf(e.range.columnStart);
    if (colInd == -1) return;
    if (colInd < 3) {
      offset = [4,-1,1,1];
      format = "MM/dd/yyyy HH:mm:ss";
      e.range.offset(0, offset[colInd])
      .setValue(Utilities.formatDate(new Date(), "GMT+8", format));
    } else {
      setValidation(e);
    }
  }
}
4
votes

Answer by Raserhin works if you want to get a reference to the 'Check' sheet when any of the sheets in the document is edited.

If you want to run the code ONLY when the 'Check' sheet is edited, then use the following code.

function onEdit(e) {

    var sheet, cols, colInd, offset, format;   
    sheet = e.source.getActiveSheet();
    if (sheet.getName() === 'Check') {  // Condition to check edited Sheet
        cols = [7,13,14,16];
        colInd = cols.indexOf(e.range.columnStart);
        if (colInd == -1) return;
        if (colInd < 3) {
            offset = [4,-1,1,1]
            format = "MM/dd/yyyy HH:mm:ss";
            e.range.offset(0, offset[colInd])
            .setValue(Utilities.formatDate(new Date(), "GMT+8", format));
        } else {
            setValidation(e);
        }
     }
}