問題描述
除了 這個(gè)問題 我想問一下如何有效地只檢索所有重復(fù)(全天)事件的第一個(gè)事件.為每個(gè)事件調(diào)用函數(shù) findFirstEvent()
似乎不合理.所以我的方法是過濾所有事件的數(shù)組.
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() {
...
}
我最終真正需要的是一個(gè)數(shù)組,其中事件標(biāo)題為鍵,Date
對(duì)象為值.
What I actually need in the end is an array with the event titles as keys and Date
objects as values.
推薦答案
- 您想從 Google 日歷中檢索所有定期活動(dòng)和全天活動(dòng).
- 特別是,您要檢索重復(fù)事件的開始事件的日期對(duì)象.
- 您希望使用 Google Apps 腳本實(shí)現(xiàn)此目的.
- 本例使用
isRecurringEvent()
和isAllDayEvent()
方法. getEvents()
按降序返回事件.使用它,可以檢索到您期望的結(jié)果.- In this case, the methods of
isRecurringEvent()
andisAllDayEvent()
are used. getEvents()
returns the events with the descending order. Using this, the result you expect is retrieved.
如果我的理解是正確的,那么這個(gè)答案呢?請(qǐng)認(rèn)為這只是幾個(gè)可能的答案之一.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
當(dāng)以上幾點(diǎn)反映到你的腳本中時(shí),它變成如下.
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;
}, []);
結(jié)果:
運(yùn)行上述腳本時(shí),返回以下值.
Result:
When above script is run, the following value is returned.
[
{
"eventTitle": "###",
"eventId": "###",
"startDate": ### date object ###,
"endDate": ### date object ###
},
,
,
]
參考資料:
- isRecurringEvent()
- isAllDayEvent()李>
- getId()
- 所以你會(huì) for 循環(huán)遍歷結(jié)果數(shù)組 firstEvents 以獲取所需的數(shù)組,其中事件標(biāo)題作為鍵,日期對(duì)象作為值?
如果我誤解了您的問題并且這不是您想要的方向,我深表歉意.
If I misunderstood your question and this was not the direction you want, I apologize.
由此,我無法理解您想要一個(gè)數(shù)組還是一個(gè)對(duì)象.所以我想提出2種模式.在這種情況下,我認(rèn)為可以使用當(dāng)前腳本的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.
在此模式中,返回一個(gè)數(shù)組,其中包括事件標(biāo)題和開始日期對(duì)象分別是鍵和值.請(qǐng)進(jìn)行如下修改.
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:
在此模式中,返回一個(gè)對(duì)象,其中包括事件標(biāo)題和開始日期對(duì)象分別是鍵和值.
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;
}, {});
這篇關(guān)于Google Apps 腳本日歷服務(wù):僅獲取所有重復(fù)(全天)事件的第一個(gè)事件的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!