提问人:R.Medhi 提问时间:11/10/2023 最后编辑:TheMasterR.Medhi 更新时间:11/13/2023 访问量:69
周日暂停/跳过 google apps 脚本的执行
Pause/skip executions of google apps script on Sunday
问:
我制作了一个 Google Apps 脚本项目,用于从 Google 表格中挑选一些数据,并每天在预定时间发送。问题是,我不想在星期天执行脚本。目前,我正在通过自定义函数处理此问题。问题在于,脚本至少运行自定义函数。我本来希望脚本在周日跳过执行。我一直在寻找一个选项,例如周日除外或在复选框中选择周一至周六的工作日。
这些是截至目前在周计时器中可用的选项。
“选择事件源”中还有另一个选项,如下图所示,如“从日历”所示。但我认为它在这种情况下没有用!
当前代码结构如下。
function doGet() {
if (shouldRunTrigger) {
var sheetName = "today only"; // Replace with the name of your sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(sheetName);
if (sheet) {
var dataPresent = sheet.getRange("A:A").getValues().filter(row => row[0] !== "");
var dataAbsent = sheet.getRange("B:B").getValues().filter(row => row[0] !== "");
var employeesPresent = dataPresent.map(function (row) {
return row[0];
});
var employeesAbsent = dataAbsent.map(function (row) {
return row[0];
});
var emailBody = "Employees present:\n" + employeesPresent.join("\n") + "\n\n" +
"Employees absent:\n" + employeesAbsent.join("\n");
MailApp.sendEmail({
to: "[email protected]", // Add multiple email addresses separated by a comma !!!Not working when put multiple email addresses due to a a feature Google deployed for Spam protection https://support.google.com/mail/answer/69585
subject: "Employee Status",
body: emailBody
});
} else {
Logger.log("Sheet not found!");
}
}
}
function shouldRunTrigger() {
var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
var date = new Date();
var day = days[date.getDay()];
if (day === "Sun") {
Logger.log(day + "\nNot triggered!");
return false;
}
Logger.log(day + "\nTriggered!");
return true;
}
有没有我不知道的功能可以实现这一点?或任何建议!
答:
3赞
Codedabbler
11/10/2023
#1
您可以在扩展中设置可安装的触发器。创建一个新文件并使用如下代码:
function createTimeDrivenTriggers() {
// Trigger function everyday around 6:00 am
var days = [ScriptApp.WeekDay.MONDAY, ScriptApp.WeekDay.TUESDAY,
ScriptApp.WeekDay.WEDNESDAY, ScriptApp.WeekDay.THURSDAY,
ScriptApp.WeekDay.FRIDAY,ScriptApp.WeekDay.SATURDAY];
for (var i=0; i<days.length; i++) {
ScriptApp.newTrigger('doGet')
.timeBased()
.onWeekDay(days[i])
.atHour(6)
.nearMinute(00)
.create();
}
}
然后保存并单击“运行”。授权脚本运行后,将为一周中的每一天设置触发器。在上面的代码示例中,触发器设置为在上午 6:00 左右运行脚本。这可以根据需要进行修改。
评论
Class ClockTriggerBuilder
来设置timeBased()
触发器,而不是运行自定义函数。shouldRunTrigger()