我正在建立一个聊天机器人,并添加了Google日历服务(以在特定的日期和时间添加事件)。
我可以添加事件,但我想添加一种可能性,以检查是否可以在该日期/时间添加事件。
这是我的代码:
UserCredential credential;
string[] Scopes = { CalendarService.Scope.Calendar };
string credPath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.Personal);
using (var stream =
new FileStream(path, FileMode.Open, FileAccess.Read))
credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
Scopes,
"user",
CancellationToken.None,
new FileDataStore(credPath, true)).Result;
string ApplicationName = "Google Calendar API .NET Quickstart";
var service = new CalendarService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = ApplicationName,
});
Event newEvent = new Event()
{
//Summary = assuntoAgendamento,
Summary = "Event",
Location = "eee",
Description = "Event is scheduled for: " + eventi.Date.ToString("dd-MM-yyyy") + " " + eventi.Time.ToShortTimeString(),
Start = new EventDateTime()
{
// DateTime = eventi.Date,
DateTime = new DateTime(eventi.Date.Year, eventi.Date.Month, eventi.Date.Day, eventi.Time.Hour, eventi.Time.Minute, 0),
TimeZone = "Europe/Brussels",
},
End = new EventDateTime()
{
DateTime = eventi.Date.AddMinutes(60),
// DateTime = new DateTime(eventi.Date.Year, eventi.Date.Month, eventi.Date.Day, eventi.Time.Hour, eventi.Time.Hour +1 ,0),
TimeZone = "Europe/Brussels",
},
Recurrence = new String[] { "RRULE:FREQ=DAILY;COUNT=1" },
Attendees = new EventAttendee[] {
new EventAttendee() { Email = "xxxxx@gmail.com" },
},
///////////////////////////////////////////
///////////////////////////////////////////
Reminders = new Event.RemindersData()
{
UseDefault = false,
Overrides = new EventReminder[] {
new EventReminder() { Method = "email", Minutes = 1 * 60 },
new EventReminder() { Method = "sms", Minutes = 10 },
}
}
};
String calendarId = "primary";
EventsResource.InsertRequest request1 = service.Events.Insert(newEvent, calendarId);
Event createdEvent = request1.Execute();
Console.WriteLine("Event created: {0}", createdEvent.HtmlLink);
// Define parameters of request.
EventsResource.ListRequest request = service.Events.List("primary");
request.TimeMin = eventi.Time;
request.ShowDeleted = false;
request.SingleEvents = true;
request.MaxResults = 10;
request.OrderBy = EventsResource.ListRequest.OrderByEnum.StartTime;
// List events.
Events events = request.Execute();
Console.WriteLine("Upcoming events:");
if (events.Items != null && events.Items.Count > 0)
{
foreach (var eventItem in events.Items)
{
string when = eventItem.Start.Date;
if (String.IsNullOrEmpty(when))
{
when = eventItem.Start.Date;
}
Console.WriteLine("{0} ({1})", eventItem.Summary, when);
}
}
else
{
Console.WriteLine("No upcoming events found.");
}
Console.Read();
}
请问我如何做到这一点?预先谢谢你!
答案 0 :(得分:0)
我想添加一种可能性,以检查是否可以在该日期/时间内添加事件
要满足您的要求:检查具有指定开始时间和结束时间的新事件是否与其他现有事件重叠,可以参考以下代码段。
if (events.Items != null && events.Items.Count > 0)
{
var overlaplist = events.Items.Where(e => (Convert.ToDateTime(e.Start.DateTime) < eventi && Convert.ToDateTime(e.End.DateTime) > eventi.AddMinutes(60))
|| (Convert.ToDateTime(e.Start.DateTime) < eventi && Convert.ToDateTime(e.End.DateTime) > eventi)
|| (Convert.ToDateTime(e.Start.DateTime) > eventi && Convert.ToDateTime(e.Start.DateTime) < eventi.AddMinutes(60))
|| (Convert.ToDateTime(e.End.DateTime) > eventi && Convert.ToDateTime(e.End.DateTime) < eventi.AddMinutes(60))).ToList();
if (overlaplist.Count() > 0)
{
foreach (var eventItem in overlaplist)
{
Console.WriteLine("Date range of your new event overlaps with {0}, which start at {1} and end at {2})", eventItem.Summary, eventItem.Start.DateTime, eventItem.End.DateTime);
}
}
else
{
Console.WriteLine("Your new event is available!");
}
}
else
{
Console.WriteLine("No upcoming events found.");
}
测试结果:
日历事件: