Home > Back-end >  How do I exclude certain sheets/tabs from google app script?
How do I exclude certain sheets/tabs from google app script?

Time:03-29

I am using this code: function onEdit(e) { e.source.getActiveSheet().getRange('K2').setValue(new Date()).setNumberFormat("dd/mm/yyyy hh:mm:ss") }

to add the date and time when each sheet/tab was 'last updated', however, there are two tabs I want to exclude from this: "Product Features" and "Client List Overview" - how can I adjust the script so that these two tabs/sheets are not included? (I'm complete novice so will need to know exactly what to change and where to put it, thank you!)

CodePudding user response:

Consider get first the name of the active sheet, then, add an conditional to check if the name of the active sheet is different from the ones you want to exclude the code (i.e. setting a date in a specific cell).

Example (N.B: untested):

function onEdit(e) {
    /** Name of the active sheet. */
    var sheetName = "";

    /** Active sheet. */
    var activeSheet = null;

    // Get active sheet: 
    activeSheet = e.source.getActiveSheet();

    // Get active sheet's name:
    sheetName = activeSheet.getSheetName();

    // Apply this code to those active sheets where their name is different from these sheets:
    if (sheetName != "Product Features" && sheetName != "Client List Overview") {
        e.source.getActiveSheet().getRange('K2').setValue(new Date()).setNumberFormat("dd/mm/yyyy hh:mm:ss")
    }
}

CodePudding user response:

I believe this is what you need:

function onEdit(e) { 
  var activeSheet = SpreadsheetApp.getActive();
  activeSheet.getRange('K2').setValue(new Date()).setNumberFormat("dd/mm/yyyy hh:mm:ss") 
  }
  • Related