我正在尝试创建一个 Google Apps 脚本,能够根据电子表格中的值在特定时间安排邮件。将有多个日期时间单元格作为触发器的基础。我尝试使用 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 点为 Mark,下午 4 点为 Jack。现在在发送邮件功能中如何确定该邮件是发送给 Mark 还是 Jack。我没有看到将参数传递给触发函数的方法。有办法解决这个问题吗?
最佳答案
如果您的问题与确定触发器运行时应执行的内容有关,您可能需要更改处理此问题的方式。
与其为每条消息创建一个触发器,不如使用一个触发器来检查是否需要发送消息。
你可以:
- 创建一个函数:
- 过滤掉已发送的消息
- 查找具有
target send time
的邮件关于过去 - 发送这些消息
- 创建一个基于时间的触发器来运行该函数。
请记住,在最好的情况下,您的消息将在目标时间发送,在最坏的情况下,它将在 <interval between trigger runs>
上发送。时间。
此外,您还需要跟踪已发送的内容(例如,使用额外的列,或删除行)
关于javascript - Google Apps 脚本基于时间的触发器,根据电子表格中的值在自定义日期和时间触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60433444/