Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 要匹配字符串,但getting:TypeError:无法在非对象上使用的实例_Javascript - Fatal编程技术网

Javascript 要匹配字符串,但getting:TypeError:无法在非对象上使用的实例

Javascript 要匹配字符串,但getting:TypeError:无法在非对象上使用的实例,javascript,Javascript,要匹配字符串,但获取: TypeError:无法在非对象上使用的实例 我正在尝试改编以下脚本: 函数ColorEvents(){ var today=新日期(); var nextweek=新日期(); nextweek.setDate(nextweek.getDate()+7); Logger.log(今天+“”+nextweek); var calendars=CalendarApp.getAllowedCalendars(); Logger.log(“找到的日历数:“+日历.长度”);

要匹配字符串,但获取:

TypeError:无法在非对象上使用的实例

我正在尝试改编以下脚本:

函数ColorEvents(){
var today=新日期();
var nextweek=新日期();
nextweek.setDate(nextweek.getDate()+7);
Logger.log(今天+“”+nextweek);
var calendars=CalendarApp.getAllowedCalendars();
Logger.log(“找到的日历数:“+日历.长度”);

对于(var i=0;i不确定您想要实现什么,但您完全滥用了本应仅用于对象的内容(并且您的标题[0]似乎是一个字符串)。如果您试图检查字符串是否包含其他子字符串,请使用

您可以使用
标题[0]。indexOf(“[TEST]”)1
,这意味着字符串“[TEST]”如果条件为,则在测试[0]中存在true@shrys这不正确匹配,但没有错误。给定一个字符串或字符串数组。我不确定。我怎么知道这一点?使用includes()时,行需要是什么?if(title[0]。includes(“[TEST]”){这给出了错误:TypeError:找不到包含在对象P中的函数(第17行,文件“ColorCode”),我怀疑这是我遇到的Google脚本错误
function ColorEvents() {

  var today = new Date();
  var nextweek = new Date();
  nextweek.setDate(nextweek.getDate() + 7);
  Logger.log(today + " " + nextweek);

  var calendars = CalendarApp.getAllOwnedCalendars();
  Logger.log("found number of calendars: " + calendars.length);

  for (var i=0; i<calendars.length; i++) {
    var calendar = calendars[i];
    var events = calendar.getEvents(today, nextweek);
    for (var j=0; j<events.length; j++) {
      var e = events[j];
      var title = e.getTitle();
      if ("[TEST]" in title[0]) {
        e.setColor(CalendarApp.EventColor.GREY);
      }
      if (title[0] == "!") {
        e.setColor(CalendarApp.EventColor.RED);
      }
      if (title[0] == '#') {
        e.setColor(CalendarApp.EventColor.GREEN);
      }
    }
  }
}