简体   繁体   English

如何在每个月末设置应用程序脚本触发 email Google 表单?

[英]How do I set up an apps script trigger to email a Google form at the end of every month?

I created the following Apps Script to automatically email a Google form at the end of every month.我创建了以下 Apps 脚本以在每个月底自动 email Google 表单。 I'm getting the error "Script function not found: sendEmail" and I don't know enough about any of this to figure out how to fix it and my googling has been fruitless.我收到错误“找不到脚本 function:sendEmail”,我对此了解不足,无法弄清楚如何修复它,我的谷歌搜索毫无结果。 Any help is appreciated!!任何帮助表示赞赏!

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).');
  }
}
}

The ClockTriggerBuilder Class already has onMonthDay() to run every x day of the month, so you have a few other approaches. ClockTriggerBuilder Class 已经有onMonthDay()在每个月的x天运行,所以你有一些其他的方法。

If you don't mind sending the email on the first day of every month you can just set onMonthDay(1)如果您不介意在每个月的第一天发送 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).');
}

If you really need to run it on the last day, onMonthDay takes values up to 31, but I didn't find any documentation that would confirm whether or not this would work on months with fewer days, so just to be safe you can also create a trigger that runs on the first day of the month, to calculate the last day and set a trigger for that day.如果你真的需要在最后一天运行它, 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).');
}

Credit to this answer for the daysInMonth() function. daysInMonth() function 的这个答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM