javascript - 想要匹配字符串但得到:TypeError:不能在非对象上使用实例
问题描述
想要匹配字符串但得到:
TypeError:不能在非对象上使用实例
我正在尝试调整以下脚本:
https://rickpastoor.com/2019/05/30/google-calendar-color-coder.html
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);
}
}
}
}
当我尝试运行它时,出现错误:
TypeError:不能在非对象上使用实例。(第 17 行,文件“颜色代码”)
这本质上是“if ("[TEST]" in title[0]) {" 行
如果标题[0] 包含字符串“[TEST]”,我想要一个匹配项
解决方案
不确定您要达到什么目的,但是您完全滥用了 in 运算符,该运算符应该仅用于对象(并且您的title[0]似乎是一个字符串)。如果您尝试检查您的字符串是否包含其他子字符串,请使用includes()
推荐阅读
- node.js - 如何在 API Gateway 授权方后面调用 lambda
- .htaccess - 在 Joomla htaccess 中强制重定向到 /index.php/url
- javascript - Javascript:替换字符串中的标记模板文字
- angularjs - AngulaJS中的“isNull”具有非定义值
- php - Apache 2.4 将 Symfony 1.4 index.php 显示为纯文本
- azure - 在 Log Analytics 工作区下的 Azure 资源中设置 Azure 函数时出错
- python-3.x - 终端中无法识别 Python
- c - 如何用鼠标在 Turbo-C 中触发动画?
- ios - 增加 UINavigationBar 的高度
- c# - 为什么 View 没有重新加载?