getActiveSheet Function Work for Every Tab but One – Google Apps Script

google sheetsgoogle-apps-script

I have the following function that adds the latest edit time to the L1 cell in our spreadsheet –

function onEdit(e) {
    // Prevent errors if no object is passed.
    if (!e) return;
    // Get the active sheet.
    e.source.getActiveSheet()
        // Set the cell you want to update with the date.
        .getRange('L1')
        // Update the date.
    .setValue(Utilities.formatDate(new Date(), "GMT-6:00", "''MM-dd-yyyy hh:mm:ss aa"));
}

This is working as intended and seems to be OK – but I would like to exclude a specific sheet when running the function.

Say I have 6 tabs/sheets – Line1, Line2, Line3, Line4, Line5, Data Sheet

I would like the function to continue working for each of the "Line" tabs, but not apply to the Data tab as this has information in the L1 cell that needs to stay. Is there anything simple I can add to this function that excludes the "Data Sheet" from applying the latest edit time?

Best Answer

You could introduce a JavaScript if statement like in the following script

function onEdit(e){
  // if active sheet name is Data Sheet then stop
  if(e.range.getSheet().getName() === 'Data Sheet') return;
  // Add here the things to do when active sheet name isn't Data Sheet

}