[英]How do I set up an apps script trigger to email a Google form at the end of every month?
我创建了以下 Apps 脚本以在每个月底自动 email Google 表单。 我收到错误“找不到脚本 function:sendEmail”,我对此了解不足,无法弄清楚如何修复它,我的谷歌搜索毫无结果。 任何帮助表示赞赏!
function monthlyEmailTrigger() {
ScriptApp.newTrigger('sendEmail')
.timeBased()
.atHour(8)
.everyDays(1)
.create();
}
function myTriggerFunction()
{
var today = new Date();
var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth()+1, 0);
if(today.getDate() == lastDayOfMonth.getDate() )
{
function sendEmail() {
GmailApp.sendEmail('myemail@gmail.com', 'Monthly Form', 'Please complete this form (form URL here).');
}
}
}
ClockTriggerBuilder Class 已经有onMonthDay()
在每个月的x
天运行,所以你有一些其他的方法。
如果您不介意在每个月的第一天发送 email,您可以设置onMonthDay(1)
function monthlyEmailTrigger() {
ScriptApp.newTrigger('sendEmail')
.timeBased()
.onMonthDay(1)
.create();
}
function sendEmail() {
GmailApp.sendEmail('myemail@gmail.com', 'Monthly Form', 'Please complete this form (form URL here).');
}
如果你真的需要在最后一天运行它, onMonthDay
的值最多为 31,但我没有找到任何文档可以确认这是否适用于天数较少的月份,所以为了安全起见,你也可以创建一个在每月第一天运行的触发器,以计算最后一天并为该日设置触发器。
function triggerCreator(){ // run once to create the trigger
ScriptApp.newTrigger('monthlyTrigger')
.timeBased()
.onMonthDay(1) //the trigger will runs every 1st day of the month
.create()
}
function monthlyTrigger(){
let today = new Date()
let month = today.getMonth()+1
let year = today.getFullYear()
let lastday = daysInMonth(today.getMonth()+1, year) // calculates last day of the current month
ScriptApp.newTrigger('sendEmail')
.timeBased()
.atDate(year, month, lastday) //sets the trigger to run the last day of the current month
.create()
}
function daysInMonth (month, year) {
return new Date(year, month, 0).getDate();
}
function sendEmail() {
GmailApp.sendEmail('myemail@gmail.com', 'Monthly Form', 'Please complete this form (form URL here).');
}
daysInMonth()
function 的这个答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.