javascript - 仅在触发时(而不是在测试时)因 indexOf 导致脚本失败

标签 javascript google-apps-script indexof

我在玩 Google Apps Script,我快疯了。

在 Google Apps Script 站点中单击 Run 和 Debug 按钮时,以下代码运行完美。
但是,当尝试通过 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, second=56, timezone=UTC, authMode=FULL, triggerUid=2998093}. at 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);

}

最佳答案

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

所以当触发器调用 createIssueReminder它传递一个事件对象作为 categories .您的代码只设置 categories如果它没有作为参数传递。

在触发器的情况下,它被传递。

将您的功能更改为:

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

关于javascript - 仅在触发时(而不是在测试时)因 indexOf 导致脚本失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59651671/

相关文章:

javascript - 确定单击了哪个元素

google-apps-script - 使用 Apps 脚本自定义 Google Sheets 图表颜色?

google-apps-script - 在没有随机 Utilities.sleep 的情况下处理 UrlFetch 速率限制的正确方法?

java - 在java中使用indexOf分割字符串时遇到问题

javascript - 没有提交按钮就提交

javascript - Angularjs 应用程序不打开配置的模式

c# - 文本框 Indexof 和 LastIndexOf

jQuery indexof 从第二个相同的字符开始

javascript - 欢迎来到 socket.io。唯一的消息

javascript - 添加和删​​除文档上两点之间的内容