【问题标题】:Google Apps Script - Google Calendar - getEvents() multiple search queries - OR logicGoogle Apps 脚本 - Google 日历 - getEvents() 多个搜索查询 - 或逻辑
【发布时间】:2025-12-17 00:45:01
【问题描述】:

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

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'"

还有许多这样的变种,成功率为零。

提出的相关问题:

任何帮助将不胜感激。

【问题讨论】:

  • 你试过用or代替|吗?
  • 是的,ORor 都试过了。 TERM_A 返回一个条目。 TERM_B 返回一个条目。 TERM_A 或 TERM_B 返回空输出。
  • 试过+
  • 是的,尝试过andAND++ 基本上是单词的组合。它返回标题包含两个单词的事件。

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    {搜索:TERM_A+TERM_B}

    我将以下 cmets 放入我的脚本中以供参考:

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

    【讨论】:

      【解决方案3】:

      我最终做了:

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

      而且效果很好……

      【讨论】: