【问题标题】:What kind of object is a CalendarEvent?什么样的对象是 CalendarEvent?
【发布时间】:2014-05-29 01:16:37
【问题描述】:

我正在编写一个脚本,它将从 Google 日历中提取一天的事件,并最终制作一个电子邮件议程。我正在改编来自https://developers.google.com/apps-script/templates 的sn-p。

// Get today's events
var now = new Date();
var morning = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
var night = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999)
var cals = [[in the real code, this contains about 30 valid, properly structured calendar IDs with commas and single quotes in all the right places]];
Logger.log(cals.length); //double-checking number to ensure that I didn't miss a calendar
//cycle through calendars and get events
var events = [];
var working;
var currentCal;
for (var i=0; i<cals.length; i++){
  currentCal = cals[i];
  working = CalendarApp.getCalendarById(currentCal).getEvents(morning,night);
  Logger.log(working.getTitle());
  events.push(working[0]);
}
Logger.log(events);

我在倒数第二个 Logger.log 调用中尝试了各种策略。最初,它只是记录工作。问题是,我无法让它停止记录“CalendarEvent”——字符串,字面意思,就像它在这里一样——而不是实际的日历事件。事件记录为空数组和偶尔的“CalendarEvent”。我怀疑我在这里忽略了一些愚蠢的东西,比如“工作”的对象类型实际上是。

那么,CalendarEvent 应该是一个什么样的对象呢?正如我所怀疑的那样,它是它自己的结构化事物,还是只是一个包含事件标题、时间、描述等的数组? (如您在上面看到的,我尝试将其视为一个数组,但无济于事。)

【问题讨论】:

  • 在控制台console.log(cals)运行,你会看到结构。
  • 我刚试过。正如我所期望的那样,它输出日历 ID 数组。事件仍然记录为 [null, null, CalendarEvent, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ,空,空,空,空,空,空,空,空,空]。
  • 实际上,CalendarEvent 甚至不应该存在 - 今天的任何日历上都没有事件。
  • 猜你没有得到很好的回应。日历事件应该是这样的developers.google.com/google-apps/calendar/v3/reference/…
  • 不过,这看起来像是针对 API 的。日历事件的 API 版本和 Google Apps 脚本版本之间有区别吗?好消息是,它现在只在实际有一个数组时才将“CalendarEvent”放入该数组,但 thatArray.toString() == '' 仍然评估为真。

标签: javascript google-apps-script google-calendar-api


【解决方案1】:

在这堵特殊的墙上扔了几天几乎所有东西之后,我似乎偶然发现了一个解决方案!在此处发布以防其他人遇到此特定问题。

我无法解释原因,但以下方法有效:

// Get today's events
var now = new Date();
var morning = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
var night = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 23, 59, 59, 999)
var cals = [properly formatted array of about 30 different calendar IDs];
Logger.log(cals.length); //double-checking number of employees to ensure that I didn't miss a calendar
//cycle through calendars and get events
var events = [];
var working = [];
var currentCal;
for (var i=0; i<cals.length; i++){
  currentCal = cals[i];
  working = CalendarApp.getCalendarById(currentCal).getEvents(morning,night); 
if (working.length != 0) {
  Logger.log("Working "+ working[0]);
  events.push.apply(events,working);
  Logger.log("First event "+events[0]);
  Logger.log(events[0].getTitle());
}}

明确地将工作和事件视为数组似乎有很大帮助......或者这可能与它没有任何关系。我所知道的是,这现在从正确的事件中提取了一个事件标题。 (不过,我仍然需要使用多个日历中的多个事件对其进行测试。)我还更改了上面的代码,以仅在 events 数组中包含 calendarEvents,而不是一个 calendarEvent 和 30 个空值。

【讨论】:

    猜你喜欢
    • 2020-10-03
    • 1970-01-01
    • 1970-01-01
    • 2017-02-14
    • 1970-01-01
    • 2012-01-05
    • 2020-02-11
    • 2022-08-11
    • 2020-12-23
    相关资源
    最近更新 更多