在每个月的最后一小时设置一个触发器来运行函数 [英] Set a trigger to run function the last hour of each month

查看:90
本文介绍了在每个月的最后一小时设置一个触发器来运行函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在谷歌脚本中,我知道有按日期运行的触发器,但我认为这不会起作用,因为月份有不同的天数。所以我想知道是否有办法在每个月的最后一晚设置触发器在11PM运行,无论这是30还是31。



谢谢首先从项目编辑>当前项目的触发器创建一个触发器,或者以编程方式注册它,以便运行它。

每天晚上11点。

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

然后在您的触发器处理程序中,检查今天是否是本月的最后一天,然后执行您的工作

  function myTriggrFunction()
{
var today = new Date();
var lastDayOfMonth = new Date(today.getFullYear(),today.getMonth()+ 1,0);

if(today.getDate()== lastDayOfMonth.getDate())
{
//您要完成的工作
}
}


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 way to set a trigger to run at 11PM on the last night of each month, no matter if that's a 30 or a 31.

Thanks

解决方案

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
  }
}

这篇关于在每个月的最后一小时设置一个触发器来运行函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆