Google Apps 脚本日历服务:仅获取所有重复(全天)事件的第一个事件 [英] Google Apps Script Calendar Service: Get only the first events of all recurring (all day) events

查看:33
本文介绍了Google Apps 脚本日历服务:仅获取所有重复(全天)事件的第一个事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了 这个问题 我想问一下如何有效地只检索所有重复(全天)事件的第一个事件.为每个事件调用函数 findFirstEvent() 似乎不合理.所以我的方法是过滤所有事件的数组.

In addition to this question I'd like to ask how to efficiently retrieve only the first events of all recurring (all day) events. To call the function findFirstEvent() for each single event seems not to be reasonable. So my approach would be to filter the array of all events.

var cal=CalendarApp.getCalendarById("Calendar Id");
var startTime=new Date(1850,0,1);
var endTime=new Date();
var events=cal.getEvents(startTime, endTime);
var firstEvents=events.filter(onlyFirstEvents);

function onlyFirstEvents() {
    ...
}

我最终真正需要的是一个数组,其中事件标题为键,Date 对象为值.

What I actually need in the end is an array with the event titles as keys and Date objects as values.

推荐答案

  • 您想从 Google 日历中检索所有定期活动和全天活动.
  • 特别是,您要检索重复事件的开始事件的日期对象.
  • 您希望使用 Google Apps 脚本实现此目的.
  • 如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一.

    If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

    • 本例使用isRecurringEvent()isAllDayEvent()方法.
    • getEvents() 按降序返回事件.使用它,可以检索到您期望的结果.
    • In this case, the methods of isRecurringEvent() and isAllDayEvent() are used.
    • getEvents() returns the events with the descending order. Using this, the result you expect is retrieved.

    当以上几点反映到你的脚本中时,它变成如下.

    When above points are reflected to your script, it becomes as follows.

    var firstEvents=events.filter(onlyFirstEvents);
    

    到:

    var firstEvents = events.reduce(function(ar, e) {
      var id = e.getId();
      if (e.isRecurringEvent() && e.isAllDayEvent() && !ar.some(function(f) {return f.eventId == id})) {
        ar.push({eventTitle: e.getTitle(), eventId: id, startDate: e.getAllDayStartDate(), endDate: e.getAllDayEndDate()});
      }
      return ar;
    }, []);
    

    结果:

    运行上述脚本时,返回以下值.

    Result:

    When above script is run, the following value is returned.

    [
      {
        "eventTitle": "###",
        "eventId": "###",
        "startDate": ### date object ###,
        "endDate": ### date object ###
      },
    ,
    ,
    
    ]
    

    参考资料:

    • isRecurringEvent()
    • isAllDayEvent()李>
    • getId()
    • 如果我误解了您的问题并且这不是您想要的方向,我深表歉意.

      If I misunderstood your question and this was not the direction you want, I apologize.

      • 所以你会 for 循环遍历结果数组 firstEvents 以获取所需的数组,其中事件标题作为键,日期对象作为值?

      由此,我无法理解您想要一个数组还是一个对象.所以我想提出2种模式.在这种情况下,我认为可以使用当前脚本的firstEvents.

      From this, I cannot understand whether you want an array or an object. So I would like to propose 2 patterns. In this case, I thought that firstEvents of the current script can be used.

      在此模式中,返回一个数组,其中包括事件标题和开始日期对象分别是键和值.请进行如下修改.

      In this pattern, an array, which includes that the event titles and the start date object are the key and value, respectively, is returned. Please modify as follows.

      var firstEvents = events.reduce(function(ar, e) {
        var id = e.getId();
        if (e.isRecurringEvent() && e.isAllDayEvent() && !ar.some(function(f) {return f.eventId == id})) {
          ar.push({eventTitle: e.getTitle(), eventId: id, startDate: e.getAllDayStartDate(), endDate: e.getAllDayEndDate()});
        }
        return ar;
      }, []);
      firstEvents = firstEvents.map(function(e) {
        var obj = {};
        obj[e.eventTitle] = e.startDate;
        return obj;
      });
      

      模式2:

      在此模式中,返回一个对象,其中包括事件标题和开始日期对象分别是键和值.

      Pattern 2:

      In this pattern, an object, which includes that the event titles and the start date object are the key and value, respectively, is returned.

      var firstEvents = events.reduce(function(ar, e) {
        var id = e.getId();
        if (e.isRecurringEvent() && e.isAllDayEvent() && !ar.some(function(f) {return f.eventId == id})) {
          ar.push({eventTitle: e.getTitle(), eventId: id, startDate: e.getAllDayStartDate(), endDate: e.getAllDayEndDate()});
        }
        return ar;
      }, []);
      firstEvents = firstEvents.reduce(function(obj, e) {
        obj[e.eventTitle] = e.eventTitle in obj ? obj[e.eventTitle].concat(e.startDate) : [e.startDate];
        return obj;
      }, {});
      

      这篇关于Google Apps 脚本日历服务:仅获取所有重复(全天)事件的第一个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆