简体   繁体   English

通过Google Calendar API v3创建重复事件

[英]Creating recurring events via Google Calendar API v3

I've got a web application that a user fills out a form and on submit it adds an event to a google calendar. 我有一个Web应用程序,用户可以填写表单,并在提交后将事件添加到Google日历。 I can insert a single instance of an event onto the calendar but when I try to set the recurrence of the event I am unable to do so. 我可以将事件的单个实例插入日历,但是当我尝试设置事件的重复发生时,我无法这样做。 My methods so far have either accomplished nothing or the event is created, but only for a single occurrence. 到目前为止,我的方法没有完成任何事情,也没有创建事件,但仅发生一次。

I am using google api calendar v3 below is my code to insert the event. 我正在使用下面的Google API日历v3 ,这是我插入事件的代码。

try{
    Google.Apis.Calendar.v3.CalendarService g = new
    Google.Apis.Calendar.v3.CalendarService();

    Google.Apis.Calendar.v3.Data.Event ev = new 
    Google.Apis.Calendar.v3.Data.Event();

    //Create Date Times for start and end time
    EventDateTime starter = new EventDateTime();
    starter.DateTime = start;
    EventDateTime ender = new EventDateTime();
    ender.DateTime = end;

    //Add values to the event
    ev.Start = starter;
    ev.End = ender;
    ev.Summary = summary.Text;
    ev.Location = location.Text;
    ev.Description = description.Text;
    String[] recd = {"RRULE:FREQ=WEEKLY;COUNT=2"};

    Random rnd = new Random();
    ev.RecurringEventId = "asdf" + rnd.Next(9999).ToString();
    ev.Recurrence = recd;

    //Add to calendar
    addEvent(service, ev);
    g.Events.Insert(ev, "********");    
}

EDIT 1: I've rewritten my event creation code as follows: 编辑1:我重写了事件创建代码,如下所示:

EventDateTime starter = new EventDateTime();
starter.DateTime = start;

EventDateTime ender = new EventDateTime();
ender.DateTime = end;

Event newEvent = new Event()
  {
    Summary = summary.Text,
    Location = location.Text,
    Description = description.Text,
    Start = starter,
    End = ender,
    Recurrence = new String[] {"RRULE:FREQ=DAILY;COUNT=2"}
  };
    String calendarId = "****@group.calendar.google.com";
    EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
    Event createdEvent = request.Execute();

The only problem being that the event will not be created nor its recurrence. 唯一的问题是将不会创建该事件,也不会重复发生该事件。 If I leave off that line, the code will run and insert the single event into the calendar. 如果我退出该行,则代码将运行并将单个事件插入日历。

Did you forget to call .execute() ? 您忘了调用.execute()吗?

Event newEvent = new Event()
            {
                Summary = "Read Awesome Blog posts by Linda ",
                Location = "1600 Amphitheatre Parkway., Mountain View, CA 94043",
                Description = "A chance to learn more about Google APIs.",
                Start = new EventDateTime()
                {
                    DateTime = DateTime.Parse("2015-09-20T09:00:00-07:00"),
                    TimeZone = "America/Los_Angeles",
                },
                End = new EventDateTime()
                {
                    DateTime = DateTime.Parse("2015-09-20T17:00:00-07:00"),
                    TimeZone = "America/Los_Angeles",
                },
                Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=2" },
                Attendees = new EventAttendee[] {
                    new EventAttendee() { Email = "test@test.com" },
                },
                Reminders = new Event.RemindersData()
                {
                    UseDefault = false,
                    Overrides = new EventReminder[] {
                        new EventReminder() { Method = "email", Minutes = 24 * 60 },
                        new EventReminder() { Method = "sms", Minutes = 10 },
                }
                }
            };

            String calendarId = "primary";
            EventsResource.InsertRequest request = service.Events.Insert(newEvent, calendarId);
            Event createdEvent = request.Execute();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM