Google Script睡眠定时器



我写了一个google apps脚本。它从谷歌表格中读取数据,并根据一天剩余的配额数量向他们发送电子邮件。但是我需要设置一个睡眠计时器,让我的脚本每5分钟睡眠一次。但是我不确定我应该在哪一行添加超时或睡眠功能,以及我应该怎么做。

// It will only works on sheets with below schema:
// A      B          C          D         E         F           G
// ID   Email   First Name  Last Name   Status  Email Body  Email Subject
// DO NOT change any line of this code
// DO NOT change schema of your sheet or swap columns with each other (otherwise code may not work properly)
// Write your Email Body in "F2" cell and your Subject in "G2" cell
// To use rich text editor, use https://onlinehtmleditor.dev/ , then copy & paste ""HTML source code"" in "F2" cell
var EMAIL_SENT = 'SENT';
var EMAIL_ERROR = 'ERROR';

function sendEmails() {
var emailQuotaRemaining = MailApp.getRemainingDailyQuota() - 1; // leave 1 for being sure
Logger.log("Number of emails you can send in this run:" + emailQuotaRemaining);
var sheet = SpreadsheetApp.getActiveSheet();
var message = sheet.getRange(2, 6).getValue();
var subject = sheet.getRange(2, 7).getValue();
var startRow = 2;
var numRows = sheet.getLastRow() - 1; // Number of rows to process
var dataRange = sheet.getRange(startRow, 1, numRows, 5);
var data = dataRange.getValues();
var sentCount = 0;
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var emailAddress = row[1];
var firstName = row[2].toString().trim();
firstName = firstName.charAt(0).toUpperCase() + firstName.slice(1); // Capitalize first name
var status = row[4];
var messageWithName = "<p>Dear " + firstName + "</p>" + message;
if (status !== EMAIL_SENT && MailApp.getRemainingDailyQuota() > 1) { // Prevents sending duplicates AND check for remaining daily quotes
var isFailed = false;
try {
MailApp.sendEmail({
to: emailAddress,
subject: subject,
htmlBody: messageWithName
});
} catch (error) {
Logger.log(error);
isFailed = true;
}
if (isFailed) {
sheet.getRange(startRow + i, 5).setValue(EMAIL_ERROR);
Logger.log(`something went wrong for sending to: ${emailAddress}`);
}
else {
sheet.getRange(startRow + i, 5).setValue(EMAIL_SENT);
Logger.log(`successfully sent to: ${emailAddress}`);
sentCount += 1;
}
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
Logger.log(`${sentCount} emails has been sent successfully!`);
}

添加

function myNewTrigger() {
ScriptApp.newTrigger("sendEmails")
.timeBased()
.everyMinutes(5)
.create();
}

和启动一次。

相关内容

  • 没有找到相关文章

最新更新