除了 这个问题 我想问一下如何有效地只检索所有重复(全天)事件的第一个事件.为每个事件调用函数 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.
如果我的理解是正确的,那么这个答案呢?请认为这只是几个可能的答案之一.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
isRecurringEvent()和isAllDayEvent()方法.getEvents() 按降序返回事件.使用它,可以检索到您期望的结果.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;
}, []);
运行上述脚本时,返回以下值.
When above script is run, the following value is returned.
[
{
"eventTitle": "###",
"eventId": "###",
"startDate": ### date object ###,
"endDate": ### date object ###
},
,
,
]
如果我误解了您的问题并且这不是您想要的方向,我深表歉意.
If I misunderstood your question and this was not the direction you want, I apologize.
由此,我无法理解您想要一个数组还是一个对象.所以我想提出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;
});
在此模式中,返回一个对象,其中包括事件标题和开始日期对象分别是键和值.
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 脚本日历服务:仅获取所有重复(全天)事件的第一个事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
用于创建头像的 jQuery/JavaScript 库?jQuery/JavaScript Library for avatar creation?(用于创建头像的 jQuery/JavaScript 库?)
如何做以下掩码输入问题?How to do following mask input problem?(如何做以下掩码输入问题?)
使用 DropKick Javascript 设置值/标签的问题Issues Setting Value/Label Using DropKick Javascript(使用 DropKick Javascript 设置值/标签的问题)
如何对 jquery 插件中的私有方法进行单元测试?how to unit-test private methods in jquery plugins?(如何对 jquery 插件中的私有方法进行单元测试?)
stellar.js - 为垂直滚动网站配置偏移量/对齐元素stellar.js - configuring offsets / aligning elements for a vertical scrolling website?(stellar.js - 为垂直滚动网站配置偏移量/对齐元素?)
jQuery 屏蔽输入插件.当文本框获得焦点时选择所有jQuery masked input plugin. select all content when textbox receives focus(jQuery 屏蔽输入插件.当文本框获得焦点时选择所有内容)