Google Apps脚本-Google日历-getEvents()多个搜索查询-或逻辑

时间:2018-11-26 18:01:18

标签: google-apps-script google-calendar-api

我正在尝试从我的Google日历中获取事件标题中包含TERM_A或TERM_B的所有事件。换句话说,我正在尝试在getEvents()函数的CalendarApp函数的搜索查询中使用OR逻辑。

MWE:

function mwe(){
  var mycal = "my@emailaddress.com";
  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:

  • “ TERM_A” | “ TERM_B”
  • “ TERM_A | TERM_B”
  • “ TERM_A TERM_B”
  • “'TERM_A'|'TERM_B'”

以及许多这样的变体,但成功率为零。

相关问题:

任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

这可能不是最好的解决方案,因为它没有一步一步利用Google搜索算法,但是我为实现同一目的所做的工作是使用两次搜索,然后连接数组:

var events = ...        
var events2 = ...
events = events.concat(events2);

以及您需要的任何其他搜索。希望对您有所帮助。

答案 1 :(得分:0)

{搜索: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'});

答案 2 :(得分:0)

我最终做了:

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'}));

效果很好...