Google-sheets – Make function only work on first sheet, not in whole Google Sheets file

google sheetsgoogle-apps-scriptgoogle-sheets-timestamp

I have the following code which is for tracking the date of any edit made in a particular row. However, this code applies to the whole spreadsheet file, I would like to change this to be for the 1st sheet only which is titled "Deals List". Can anyone help please?

function onEdit() {
var dateColNum = 7 //column G
var ss1 = SpreadsheetApp.getActiveSpreadsheet();

//set date in same row as edit happens, at fixed column  
ss1.getActiveSheet().getRange(ss1.getActiveRange().getLastRow(), dateColNum, 1, 1).setValue(new Date())
}

Best Answer

Add something like the following

If by "first sheet" you mean one specific sheet no matter the sheets order

if(ss1.getSheetName() == 'Sheet1'){
  //do something like set date
}

or

If by "first sheet" you mean that the sheets' order matters

if(SpreadsheetApp.getSheets()[0].getSheetName() === ss1.getSheetName()){
  //do something like set date
}