Set a trigger to run function the last hour of each month

前端 未结 1 1658
走了就别回头了
走了就别回头了 2021-01-23 12:55

In google scripts I know there are triggers to run by date, but I don\'t think that will work because month\'s have different amounts of days. So I was wondering if there\'s a w

相关标签:
1条回答
  • 2021-01-23 13:43

    First create a trigger from project Edit > current project's trigger or register it programmatically that will run every day at 11 pm.

    ScriptApp.newTrigger("myTriggerFunction")
       .timeBased()
       .atHour(23)
       .everyDays(1) 
       .create();
    

    Then in your trigger handler, check if today is the last day of the month, then do your work.

    function myTriggrFunction()
    {
      var today = new Date();
      var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);
    
      if(today.getDate() == lastDayOfMonth.getDate() )
      {
        // your work to be done
      }
    }
    
    0 讨论(0)
提交回复
热议问题