sanjuro8998
sanjuro8998

Reputation: 1261

Google Spreadsheets - Set tab name in a variable

I have this code here:

function onEdit(e) {
  var sheet      = e.source.getActiveSheet();
  var activeCell = sheet.getActiveCell();
  var col        = activeCell.getColumn();
  var row        = activeCell.getRow();
  if (col == 4 )
    sheet.getRange(row, col+1).setValue(new Date()).setNumberFormat('MMM,dd');
  }
}

How do I replace this var sheet = e.source.getActiveSheet(); to a fixed tab name? Because I want this function to execute only in a specific Tab of my sheet, and not in all of them.

Example: I have a spreadsheet with 2 tabs: Test Cases and another one called Summary ... I want this script to update ONLY the Test Cases tab, not the summary one .

I tried: if (col == 3 && sheet == 'Test Cases') but it did not work ... thanks!

Upvotes: 1

Views: 490

Answers (2)

sanjuro8998
sanjuro8998

Reputation: 1261

Found it :-)

function onEdit(e) {
  var sheet      = e.source.getActiveSheet();
  var tab        = sheet.getSheetName();
  var activeCell = sheet.getActiveCell();
  var col        = activeCell.getColumn();
  var row        = activeCell.getRow();
  if (col == 3 && tab == 'Test Cases') {
    sheet.getRange(row, col+1).setValue(new Date()).setNumberFormat('MMM,dd');
  }
}

Upvotes: 0

Paul L
Paul L

Reputation: 938

var sheet = e.source.getSheetByName("Test Cases");

https://developers.google.com/apps-script/reference/spreadsheet/spreadsheet#getsheetbynamename

Upvotes: 1

Related Questions