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

后端 未结 2 947
南方客
南方客 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:39

    You can use the onSelectionChange() simple trigger. It will fire whenever a new cell is selected, including when the active sheet changes. (This was introduced April 22, 2020, well after the original question was asked.)

    The onSelectionChange(e) trigger runs automatically when a user changes the selection in a spreadsheet. Most onSelectionChange(e) triggers use the information in the event object to respond appropriately.

    A stringified event object looks like this:

    {
      "authMode": {},
      "range": {
        "rowEnd": 1,
        "columnStart": 1,
        "rowStart": 1,
        "columnEnd": 1
      },
      "source": {}, // Represents the spreadsheet itself
      "user": {
        "nickname": "NICKNAME",
        "email": "EMAIL"
      }
    }
    

    This will display a message box whenever the active sheet changes, except on the first run. It also attempts to minimize calls to the PropertiesService as a quota exists on it. Note that it uses the sheet ID as they are immutable, unlike sheet names.

    function onSelectionChange(e) {
      if (activeSheetChanged(e.range.getSheet().getSheetId())) {
          Browser.msgBox(e.range.getSheet().getName());
      }
    }
    
    /**
     * Check if the active sheet changed.
     * Will give a false positive if no value exists in the properties.
     * @returns {boolean}
     */
    function activeSheetChanged(newSheetId) {
      const key = 'activeSheetId';
      const cache = CacheService.getUserCache();
      let properties;
      const savedSheetId = getSavedSheetId();
      if (savedSheetId != newSheetId) {
        saveSheetId(newSheetId);
        return true;
      }
      return false;
      
      /**
       * Get the saved sheet ID from the Cache/Properties.
       * @returns {Number}
       */
      function getSavedSheetId() {
        let savedSheetId = cache.get(key);
        if (savedSheetId == null) {
          properties = getProperties();
          savedSheetId = properties.getProperty(key);
          cache.put(key, savedSheetId);
        }
        return cache.get(key);
      }
      
      /**
       * Save the sheet ID to the Cache & Properties
       */
      function saveSheetId(sheetId) {
        properties = properties ? properties : getProperties();
        properties.setProperty(key, sheetId);
        cache.put(key, sheetId);
      }
      
      /**
       * @returns {PropertiesService.Properties}
       */
      function getProperties() {
        return PropertiesService.getUserProperties();
      }
    }
    
    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题