使用Google App脚本通过正则表达式过滤Google日历

时间:2018-11-26 10:42:55

标签: javascript regex google-apps-script

我只想在Google日历中显示某些条目。 我想为此使用正则表达式( ^([0-9] + day)

在我的原型中,我充满了错误:

TypeError:“ 0”

function myFunction() {
  var cal = CalendarApp.getCalendarById("sl5something@gmail.com")
  var now = new Date();
  var events = cal.getEventsForDay(now)
  for(var i = 0; i<events.length;i++){
    title = events[i].getTitle()
    var ar = title.match(/^([0-9]+day)/)
      var preTitle = ar[0]; // <===== TypeError: "1"
      var postTitle = ar[1];
      if(preTitle){
        Browser.msgBox(preTitle + ":" + postTitle)
      }
  }
}

1 个答案:

答案 0 :(得分:0)

这按预期工作:

function myFunction() {
  var cal = CalendarApp.getCalendarById("someEmail@gmail.com")
  var now = new Date();
  var events = cal.getEventsForDay(now)
  for(var i = 0; i<events.length;i++){
    title = events[i].getTitle()
    var ar = title.match(/^([0-9]+day)(.*)/)
    if(!ar) // <==== this helps
      continue
    var preTitle = ar[1]; // <===== no TypeError: "1"
    var postTitle = ar[2];
    Browser.msgBox(preTitle + " ### " + postTitle)
  }
}