【发布时间】:2020-01-08 18:17:54
【问题描述】:
我在玩 Google Apps 脚本,我快疯了。
点击 Google Apps 脚本站点中的“运行”和“调试”按钮时,以下代码运行良好。 但是,当尝试通过 tyrigger 执行它时,我总是会收到一个奇怪的错误:
TypeError: Fonction indexOf introuvable dans l'objet {year=2020, month=1, day-of-month=8, day-of-week=3, week-of-year=2, hour=17, minute =57,秒=56,时区=UTC,authMode=FULL,triggerUid=2998093}。 在 createIssueReminder(代码:39)
它是法语,但写着function indexOf not found in object {year=2020, month=1, day-of-month=8, day-of-week=3, week-of-year=2, hour=17, minute=57, second=56, timezone=UTC, authMode=FULL, triggerUid=2998093}
这是奇怪的部分:虽然消息与自身一致(好吧,对象不是数组,所以他不能使用 indexOf),它与我的代码完全不一致,因为categories 是一个数组,并且indexOf 被调用。我到底在哪里尝试从日期实例调用 indexOf!?
如前所述,这段代码在 Google Apps 脚本编辑器中完美运行,我不知道在触发器执行时它失败的原因或位置...
对此的任何见解将不胜感激。 谢谢大家。 你是我唯一的希望,欧比旺·克诺比。
/**
* Check each week if we need a reminder for the week's issue, provided the issue matches the user's chosen categories.
* @param {string[]} categories The watched categories.
*/
function createIssueReminder(categories) {
if (!categories)
{
categories = ["S", "DD", "D", "M"];
}
// Get the "Censored SS Title" spreadsheet
// cf https://docs.google.com/spreadsheets/d/ss-id-here/
var spreadsheet = SpreadsheetApp.openById("ss-id-here");
// Get the sheet storing the issues
var issueSheet = spreadsheet.getSheetByName("Sorties");
// Get the range storing the data
var issueRange = issueSheet.getRange("A2:D81");
// Get the data from the range
// use data[row][column] to access values
var issueData = issueRange.getValues();
// Get current date and formatted date, in French format Utilities.formatDate(new Date(), "CET", "dd/MM/yy")
var currentDate = new Date();
var rowIndex = 0;
for (rowIndex; rowIndex < issueData.length; rowIndex++)
{ // repeat loop until end of data
var rowDate = issueData[rowIndex][0];
// if the date in the cell is today's date...
if (rowDate.getDate() == currentDate.getDate() &&
rowDate.getMonth() == currentDate.getMonth() &&
rowDate.getFullYear() == currentDate.getFullYear())
{
// if the issue's category matches any category set for the user...
if (categories.indexOf(issueData[rowIndex][3]) != -1)
{
// add a reminder with the issue number and content
createIssueReminderEvent(issueData[rowIndex][3], issueData[rowIndex][1], issueData[rowIndex][2]);
}
}
}
}
/**
* Add a reminder to the calendar.
* @param {number} issueNumber The number of the issue.
* @param {string} issueMessage The content of the issue.
*/
function createIssueReminderEvent(category, issueNumber, issueMessage) {
// Prepare the message
var title = "Issue #"+issueNumber+" ["+category+"]"
var message = issueMessage;
var timeZone = "CET";
var now = new Date();
var startString = Utilities.formatDate(now, timeZone, 'MMMM dd, yyyy 17:30:00 Z');
var startTime = new Date(startString);
var endString = Utilities.formatDate(now, timeZone, 'MMMM dd, yyyy 17:35:00 Z');
var endTime = new Date(endString);
// Create event for the issue
var event = CalendarApp.getDefaultCalendar().createEvent(title, startTime, endTime, {description: message});
// Add notification for the event, after deleting default notifications
event.removeAllReminders();
event.addPopupReminder(330);
// Set status (to prevent blocking the day ; workaround as we can't modify transparency of the event)
// event.addGuest("an-email@goes.here");
// event.setMyStatus(CalendarApp.GuestStatus.INVITED);
}
【问题讨论】:
-
听起来类别不是一个数组。调试并查看它实际上是什么。
-
考虑添加一个标准来验证类别实际上是一个数组,例如如果
(!categories || !Array.isArray(categories)) . ....
标签: javascript google-apps-script indexof