繁体   English   中英

根据电子表格中的值在自定义日期和时间触发基于 Google Apps 脚本时间的触发器

[英]Google Apps Script Time based trigger at custom date and time based on value in spreadsheet

我正在尝试创建一个 Google 应用程序脚本,该脚本能够根据电子表格中的值在特定时间安排邮件。 将有多个日期时间单元格基于这些单元格进行触发。 我曾尝试使用ScriptApp.newTrigger()编程方式添加触发器,但我不知道该函数何时应该运行。 关于如何解决这个问题的任何见解都会很棒。

编辑:当用户像这样提交表单时创建触发器。

function saveForm(form) {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');

  const now = Utilities.formatDate(new Date(), "GMT+5:30", "yyyy-MM-dd");

  const newRow = [now, form.cid, form.custName, form.custNumber, form.goodsType, form.tonnage, form.area, form.completeAddr, `${now} - ${form.time}`, form.comments];
  sheet.appendRow(newRow); 

  const customDate = new Date(`${now}T${form.time}`);

  ScriptApp.newTrigger('dispatchMail').timeBased().at(customDate).create();


  return 'Data saved successfully';
}

现在发送邮件的功能

  function dispatchMail() {
       const now = Utilities.formatDate(new Date(), "GMT+5:30", "yyyy-MM-dd");

      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Test');
      const data = sheet.getDataRange().getValues();

      const normalized = {
               'Mark': [],
               'Jack': [],
               'Chloe': [],
               'Robin': [],
               'Unassigned': []
      };

      for(let i=1; i<data.length; i++) {
          normalized[data[i][10]] = normalized[data[i][10]].concat([data[i]]);
      }

     //start our mail dispatch here once we have aggregated the data
  }

但是现在的问题是不知道将邮件发送给谁。 例如。 用户提交表单,邮件触发器计划在下午 2 点发送给马克,下午 4 点发送给杰克。 现在在发送邮件函数中如何确定该邮件是发送给马克还是杰克。 我没有看到将参数传递给触发器函数的方法。 有什么办法可以解决这个问题吗?

如果您的问题与确定触发器运行时应执行的内容有关,您可能需要更改处理方式。

与其为每条消息创建一个触发器,不如让一个触发器检查是否需要发送消息。

你可以:

  • 创建一个函数:
    • 过滤掉已经发送的消息
    • 查找target send time在过去的消息
    • 发送那些消息
  • 创建一个基于时间的触发器来运行该函数。

请记住,在最好的情况下,您的消息将在目标时间发送,在最坏的情况下,它将在<interval between trigger runs>时间发送。

此外,您需要跟踪已发送的内容(例如,使用额外的列或删除行)

暂无
暂无

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

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