脚本由于indexOf仅在触发时失败(在测试时未失败)

时间:2020-01-08 18:17:54

标签: javascript google-apps-script indexof

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

在Google Apps脚本网站中单击“运行”和“调试”按钮时,以下代码运行良好。 但是,当尝试通过触发程序执行它时,总是出现一个奇怪的错误:

TypeError:不可思议的Dans l'objet的功能索​​引{year = 2020,month = 1,month-of-month = 8,week-of-week = 3,year-of-year = 2,hour = 17,minutes = 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("an-email@goes.here");
  // event.setMyStatus(CalendarApp.GuestStatus.INVITED);

}

1 个答案:

答案 0 :(得分:1)

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

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

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

将功能更改为此:

function createIssueReminder() {
  categories = ["S", "DD", "D", "M"];
...