'How do I exclude certain sheets/tabs from google app script?
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!)
Solution 1:[1]
Try
function onEdit(e) {
var excl = ['Sheet1', 'Sheet2'];//excluded sheets
var activeSheet = e.source.getActiveSheet();
if (!~excl.indexOf(activeSheet.getSheetName())) {
activeSheet.getRange('K2').setValue(new Date()).setNumberFormat("dd/mm/yyyy hh:mm:ss")
}
}
Solution 2:[2]
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")
}
}
Solution 3:[3]
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")
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Mike Steelson |
| Solution 2 | Marco Aurelio Fernandez Reyes |
| Solution 3 | hamid_gholami |
