我正在尝试从我的谷歌日历中获取所有事件标题中包含 TERM_A 或 TERM_B 的事件。换句话说,我正在尝试在
getEvents()
函数的搜索查询中使用 OR 逻辑为 CalendarApp
.
MWE:
function mwe(){
var mycal = "[email protected]";
var cal = CalendarApp.getCalendarById(mycal);
var events = cal.getEvents(new Date("June 26, 2018 00:00:00 CST"), new
Date("July 18, 2020 23:59:59 CST"), {search: SEARCH-FOR-TERM_A-OR-TERM_B});
Logger.log(events);
}
)
我已经尝试用以下方法代替 SEARCH-FOR-TERM_A-OR-TERM_B:
和许多这样的零成功变体。
相关提问:
任何帮助将不胜感激。
这可能不是最好的解决方案,因为它没有在一步中利用谷歌搜索算法,但我为实现同样的目标所做的是使用两次搜索,然后连接数组:
var events = ...
var events2 = ...
events = events.concat(events2);
以及您需要的任何其他搜索。希望对您有所帮助。
{搜索:TERM_A+TERM_B}
我在我的脚本中添加了以下注释以供参考:
// Explanation of how the search section works (as it is NOT quite like most things Google) as part of the getEvents function:
// {search: 'word1'} Search for events with word1
// {search: '-word1'} Search for events without word1
// {search: 'word1 word2'} Search for events with word2 ONLY
// {search: 'word1-word2'} Search for events with ????
// {search: 'word1 -word2'} Search for events without word2
// {search: 'word1+word2'} Search for events with word1 AND word2
// {search: 'word1+-word2'} Search for events with word1 AND without word2
// {search: '-word1+-word2'} Search for events without word1 AND without word2 (I find this to work logically like an 'OR' statement)
var dateStart = sheet.getRange('C3').getValue();
var dateEnd = sheet.getRange('C4').getValue();
// C3 and C4 are the col/row location for the date range
var events = cal.getEvents(new Date(dateStart), new Date(dateEnd), {search: 'TERM_A+TERM_B'});
我最后做了:
var events = cal.getEvents(new Date("January 01, 2013 00:00:00 UTC"), new Date("October 01, 2020 23:59:59 UTC"), {search: 'FIRST'});
events = events.concat(cal.getEvents(new Date("January 01, 2013 00:00:00 UTC"), new Date("October 01, 2020 23:59:59 UTC"), {search: 'SECOND'}));
events = events.concat(cal.getEvents(new Date("January 01, 2013 00:00:00 UTC"), new Date("October 01, 2020 23:59:59 UTC"), {search: 'THIRD'}));
而且效果很好...
谢谢,这个帖子非常有帮助,我从这里借鉴了 soem 的想法,得出以下结论:
支持一个搜索词的方法:
var calendarEntries=calendar.getEvents(fromDate,toDate,{search:searchTerm});
接受以 | 分隔的术语的新方法- 变量名称有点偏离,因为我想保留 searchTerm 作为搜索字符串,只需添加 |处理:
var calendarEntries=[];
var searchTerms=searchTerm.split("|");
for (searchKey in searchTerms){
calendarEntries=calendarEntries.concat(calendar.getEvents(fromDate,toDate,{search:searchTerms[searchKey]}));
}
这似乎在初始测试中运行良好。