我尝试使用以下代码以编程方式在Google Script中创建时间驱动的触发器:
function RecurringTaskBuilder(spreadsheet, sheet, range, string) {
var text = string.toLowerCase();
Logger.log(text);
if (/other/.test(text)) {
var other = 1
} else {
var other = ""
}
if (/monday|tuesday|wednesday|thursday|friday|saturday|sunday/.test(text)) {
var days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"];
for (var index in days) {
if (text.indexOf(days[index]) > -1) {
var trigger = ScriptApp.newTrigger('RecurTask')
.timeBased()
.onWeekDay(eval("ScriptApp.WeekDay." + days[index].toUpperCase()))
.create();
var id = trigger.getUniqueId();
}
}
} else if (/\d{1,2}\w{2}\s/.test(text)) {
var trigger = ScriptApp.newTrigger('RecurTask')
.timeBased()
.onMonthDay(parseInt(text.match(/\d{1,2}/)))
.create();
var id = trigger.getUniqueId();
} else if (text.indexOf(" day") > -1 && text.indexOf("days") == -1) {
Logger.log("starting day run");
var trigger = ScriptApp.newTrigger('RecurTask')
.timeBased()
.everyDays(1)
.create();
var id = trigger.getUniqueId();
Logger.log("ending day run");
} else if (text.indexOf(" days") > -1 && /\d+/.test(text)) {
var trigger = ScriptApp.newTrigger('RecurTask')
.timeBased()
.everyDays(parseInt(text.match(/\d+/)))
.create();
var id = trigger.getUniqueId();
} else {
var id = "Failure";
}
Logger.log("finished if statement" + id);
var target_sheet = spreadsheet.getSheetByName("Recurring Tasks");
var row = range.getRow();
var source_range = sheet.getRange(row, 1, 1, 5);
var lastrow = target_sheet.getLastRow();
var target_range = target_sheet.getRange(lastrow + 1, 3, 1, 5);
source_range.copyTo(target_range);
var values = [[id, other]];
target_sheet.getRange(target_range.getRow(), 1, 1, 2).setValues(values);
Logger.log("ending run");
}
此代码正常运行,如果我使用以下内容从Google Script调试器调用它,则会创建触发器:
function Test() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet = spreadsheet.getSheetByName("Incomplete Tasks");
var range = sheet.getRange("H14");
var string = range.getValue();
RecurringTaskBuilder(spreadsheet, sheet, range, string)
}
但是,当我从onEdit(在#34;每隔一天输入#34;在我的电子表格中的一个单元格中输入)中使用完全相同的参数调用它时,它不会触发。
在调查时,它似乎只是挂起实际的触发器创建并停在那里,没有给出任何错误消息。
请提前帮助,谢谢!