Is there an event that fires when the active sheet changes in Google Sheets? Every time the result of getActiveSheet()
changes, I would like to call a function
To execute a code when switching between different sheets you can use onSelectionChange(e). For example, the following is a general code template to execute a code when switching to Sheet1:
function onSelectionChange(e) {
const as = e.source.getActiveSheet();
if (as.getName() == 'Sheet1'){
// put your code here
}
}
In this way, every time you select a different sheet, the onSelectionChange(e)
fires and the active sheet changes. Then you can use the active sheet within an if condition to execute a code block of your choice.