Google Apps脚本日历服务:如何使用PropertiesService存储和检索CalendarEvent? [英] Google Apps Script Calendar Service: How to store and retrieve CalendarEvents using PropertiesService?
本文介绍了Google Apps脚本日历服务:如何使用PropertiesService存储和检索CalendarEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我指的是这个问题.
此代码
var cal = CalendarApp.getCalendarById("Calendar Id");
var startTime = new Date(1850, 0, 1);
var endTime = new Date(2100, 0, 1);
var events = cal.getEvents(startTime, endTime);
Logger.log(events);
var sp = PropertiesService.getScriptProperties();
sp.setProperty("events", JSON.stringify(events));
events = JSON.parse(sp.getProperty("events"));
Logger.log(events);
返回:
Info [CalendarEvent, CalendarEvent, CalendarEvent, CalendarEvent, CalendarEvent]
Info [{}, {}, {}, {}, {}]
如何使用PropertiesService存储和检索CalendarEvent(不仅是ID)?
How to store and retrieve CalendarEvents (not only their IDs) using PropertiesService?
推荐答案
工作流程:
我认为最好的选择是执行以下操作:
Workflow:
I'm thinking the best option is to do the following:
- 从以前处理的事件中检索事件ID,存储在属性中(变量
oldEventIDs
). - 使用 getEvents (变量
allEvents
). - 过滤日历中的事件,删除属性"中已有的事件(变量
newEvents
). - 在处理每个新事件后,将事件ID附加到
oldEventIDs
. - 在属性中存储
oldEventIDs
.
- Retrieve event IDs from previously processed events, stored in Properties (variable
oldEventIDs
). - Retrieve all events in the calendar with getEvents (variable
allEvents
). - Filter the events in the calendar, removing the ones already in Properties (variable
newEvents
). - After processing each new event, append the event ID to
oldEventIDs
. - Store
oldEventIDs
in Properties.
var sp = PropertiesService.getScriptProperties();
var oldEventIDs = sp.getProperty("events").split(",");
var cal = CalendarApp.getCalendarById("Calendar Id");
var startTime = new Date(1850, 0, 1);
var endTime = new Date(2100, 0, 1);
var allEvents = cal.getEvents(startTime, endTime);
var newEvents = allEvents.filter(event => !oldEventIDs.includes(event.getId()));
newEvents.forEach(event => {
// Event processing
oldEventIDs.push(event.getId());
});
sp.setProperty("events", oldEventIDs.toString());
注意:
- 无论如何,每次执行都必须调用
getEvents
.我看不到如何将事件存储在Properties中可以避免这种情况.这就是您用来从日历中获取事件的方法.无论如何,我认为花费更多时间的是处理"事件,有关该事件没有任何信息. - 您必须编辑此示例,以便在达到时间限制之前停止执行.
- You have to call
getEvents
in each execution no matter what. I don't see how storing the events in Properties will let you avoid that. That's the method you use to get the events from the Calendar. In any case, I assume what's taking more time is the event "processing", about which no information was given. - You would have to edit this sample so that the execution stops before reaching time limit.
Notes:
这篇关于Google Apps脚本日历服务:如何使用PropertiesService存储和检索CalendarEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文