脚本因indexOf而失败,仅在被触发时(而不是在测试时)

问题描述 投票:3回答:1

[我正在使用Google Apps脚本,但我很生气。

下面的代码在单击Google Apps脚本网站中的运行和调试按钮时均运行正常。但是,当尝试通过触发程序执行它时,我总是会收到一个奇怪的错误:

TypeError:不可思议的dans l'objet的功能索​​引{年= 2020,月= 1,月日= 8,星期几= 3,年周= 2,小时= 17,分钟= 57,秒= 56,时区= UTC,authMode = FULL,triggerUid = 2998093}。在createIssueReminder(Code: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("[email protected]");
  // event.setMyStatus(CalendarApp.GuestStatus.INVITED);

}
javascript google-apps-script indexof
1个回答
2
投票

好像您在createIssueReminder功能上创建了一个触发器。触发器调用函数时,它将传递事件对象。参见https://developers.google.com/apps-script/guides/triggers/events

因此,当触发器调用createIssueReminder时,它将作为categories传递事件对象。您的代码仅在未将其作为参数传递时设置categories

如果是触发器,则将其传递。

将功能更改为此:

function createIssueReminder() {
  categories = ["S", "DD", "D", "M"];
...
© www.soinside.com 2019 - 2024. All rights reserved.