問題描述
除了 這個問題 我想問一下如何有效地只檢索所有重復(全天)事件的第一個事件.為每個事件調用函數 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 腳本實現此目的.
- 本例使用
isRecurringEvent()
和isAllDayEvent()
方法. getEvents()
按降序返回事件.使用它,可以檢索到您期望的結果.- 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.
如果我的理解是正確的,那么這個答案呢?請認為這只是幾個可能的答案之一.
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
當以上幾點反映到你的腳本中時,它變成如下.
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()
- 所以你會 for 循環遍歷結果數組 firstEvents 以獲取所需的數組,其中事件標題作為鍵,日期對象作為值?
如果我誤解了您的問題并且這不是您想要的方向,我深表歉意.
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;
});
模式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 腳本日歷服務:僅獲取所有重復(全天)事件的第一個事件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!