Search code examples
google-sheetsgoogle-apps-script

Open specific Google Sheet based on current annual quarter (range of 3 months)


I created a Google Spreadsheet as a cyclical meal plan that changes with the seasons. The spreadsheet has 4 sheets that represent a season of the year: Winter (December, January, February), Spring (March, April, May), Summer (June, July, August), and Fall (September, October, November), but there are additional sheets as well for Entrees, Snacks, Sides and Dessert, etc.

When opening the Google Spreadsheet, I need a Google Script to use the On_Open function to automatically determine which sheet should be active based on the current month.

For example, if I opened the Google spreadsheet on February 16th then it would open with the Winter sheet active. However, if I opened it on August 9th then the Summer sheet would be active.

I found this post from 2013 that poses a similar question but I can't find a way to make it apply to my situation.

Any help is deeply appreciated!


Solution

  • Sets the active sheet during the onOpen simple trigger handler:

    function onOpen(e) {
      const sObj = {11:"Winter",0:"Winter",1:"Winter",2:"Spring",3:"Spring",4:"Spring",5:"Summer",6:"Summer",7:"Summer",8:"Winter",9:"Winter",10:"Winter"};
      const ss = SpreadsheetApp.getActive();
      ss.setActiveSheet(ss.getSheetByName(sObj[new Date().getMonth()]))
    }