Is it possible to fire an event on active sheet change?

后端 未结 2 948
南方客
南方客 2021-01-15 06:25

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

2条回答
  •  执念已碎
    2021-01-15 06:46

    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.

提交回复
热议问题