Google Apps Script Calendar Service: How to store and retrieve CalendarEvents using PropertiesService?

空扰寡人 提交于 2021-01-29 15:45:58

问题


I'm referring to this question.

As this code

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);

returns:

Info [CalendarEvent, CalendarEvent, CalendarEvent, CalendarEvent, CalendarEvent]
Info [{}, {}, {}, {}, {}]

How to store and retrieve CalendarEvents (not only their IDs) using PropertiesService?


回答1:


Workflow:

I'm thinking the best option is to do the following:

  • 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.

Code sample:

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());

Notes:

  • 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.

Reference:

  • Calendar.getEvents(startTime, endTime)
  • Class PropertiesService


来源:https://stackoverflow.com/questions/61229940/google-apps-script-calendar-service-how-to-store-and-retrieve-calendarevents-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!