如何从谷歌日历 API .NET 中删除特定日历中的所有活动条目

本文关键字:日历 活动 删除 谷歌 API NET | 更新日期: 2023-09-27 17:57:13

我正在尝试编辑一个工具,以允许用户从他们的日历列表中进行选择,然后清除所有事件条目/根据Microsoft项目任务添加新条目。这是原始工具:http://daball.github.com/Microsoft-Project-to-Google-Calendar/

我对谷歌API/日历API完全没有经验,并且遇到了一些麻烦。我正在编辑的程序会跟踪用户从其日历列表中选择了哪个日历条目。我目前正在尝试做的是创建一个事件源,它为我提供了所选日历的事件条目,以便我可以删除所有这些。这样做的目的是允许某人使用此工具在进行更改时从项目文件更新日历。这是我尝试删除的函数。

 private void clearPreviousCalendarEntries(CalendarEntry calendarEntry)
    {      
        EventQuery query = new EventQuery();
        query.Uri = new Uri(calendarEntry.Links[0].AbsoluteUri);
        EventFeed feed = (EventFeed)calendarService.Query(query);
        AtomFeed batchFeed = new AtomFeed(feed);
        foreach (EventEntry entry in feed.Entries)
        {
            entry.Id = new AtomId(entry.EditUri.ToString());
            entry.BatchData = new GDataBatchEntryData(GDataBatchOperationType.delete);
            batchFeed.Entries.Add(entry);
        }
        EventFeed batchResultFeed = (EventFeed)calendarService.Batch(batchFeed, new Uri(feed.Batch));
        foreach (EventEntry entry in batchResultFeed.Entries)
        {
            if (entry.BatchData.Status.Code != 200 && entry.BatchData.Status.Code != 201)
                this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Problem deleteing " + entry.Title.Text + " error code: " + entry.BatchData.Status.Code);
            else
                this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Deleted " + entry.Title.Text);
        }
    }

我的提要没有返回我希望的结果,但老实说,我不确定如何正确请求事件。

query.Uri = new Uri(calendarEntry.Links[0].AbsoluteUri);是我从程序的一部分抓取的东西,该程序正在将事件添加到特定日历

AtomEntry insertedEntry = calendarService.Insert(new Uri(calendarEntry.Links[0].AbsoluteUri), eventEntry);

这些帖子肯定与我正在寻找的内容有关,但我还没有找到解决方案

  • Google-calendar-get-events-from-a-specific-calendar(英语:google-calendar-get-events-from-a-specific-calendar)
  • 如何从我创建的日历(不是默认日历)中检索独占事件?

如何从谷歌日历 API .NET 中删除特定日历中的所有活动条目

尝试这样的事情:

            CalendarService myService = new CalendarService("your calendar name");
            myService.setUserCredentials(username, password);
            CalendarEntry calendar;
            try
            {
                calendar = (CalendarEntry)myService.Get(http://www.google.com/calendar/feeds/default/owncalendars/full/45kk8jl9nodfri1qgepsb65fnc%40group.calendar.google.com);
                foreach (AtomEntry item in calendar.Feed.Entries)
                {
                    item.Delete();
                }
            }
            catch (GDataRequestException)
            {
            }

您可以在Google日历的日历详细信息页面中找到"日历ID"(如下所示:45kk8jl9nodfri1qgepsb65fnc%40group.calendar.google.com)。

这是一个相关的帖子:

谷歌日历 API asp.net C# 删除事件

这是一个有用的文档:

http://code.google.com/intl/it-IT/apis/calendar/data/2.0/developers_guide_dotnet.html

我最终得出的一种方法是从日历的 URI 中收集日历 ID,然后使用该 ID 创建一个新的 EventQuery。这是上面代码的新版本

private void clearPreviousCalendarEntries(CalendarEntry calendarEntry)
    {
        this.listBoxResults.SelectedIndex = this.listBoxResults.Items.Add("Clearing previous calender entries");
        String calendarURI = calendarEntry.Id.Uri.ToString();
        //The last part of the calendarURI contains the calendarID we're looking for
        String calendarID = calendarURI.Substring(calendarURI.LastIndexOf("/")+1);
        EventQuery query = new EventQuery();
        query.Uri = new Uri("http://www.google.com/calendar/feeds/" + calendarID + "/private/full");
        EventFeed eventEntries = calendarService.Query(query);
        AtomFeed batchFeed = new AtomFeed(eventEntries);
        foreach (AtomEntry entry in eventEntries.Entries)
        {
            entry.Id = new AtomId(entry.EditUri.ToString());
            entry.BatchData = new GDataBatchEntryData(GDataBatchOperationType.delete);
            batchFeed.Entries.Add(entry);
        }
        EventFeed batchResultFeed = (EventFeed)calendarService.Batch(batchFeed, new Uri(eventEntries.Batch));
        //check the return values of the batch operations to make sure they all worked.
        //the insert operation should return a 201 and the rest should return 200
        bool success = true;
        foreach (EventEntry entry in batchResultFeed.Entries)
        {
            if (entry.BatchData.Status.Code != 200 && entry.BatchData.Status.Code != 201)
            {
                success = false;
                listBoxResults.SelectedIndex = listBoxResults.Items.Add("The batch operation for " +
                    entry.Title.Text + " failed.");
            }
        }
        if (success)
        {
            listBoxResults.SelectedIndex = listBoxResults.Items.Add("Calendar event clearing successful!");
        }
    }

对此不是特别满意,使用字符串操作来收集信息并将我自己的查询切碎似乎很奇怪。但它有效,我一直在努力寻找一种方法来完成这项工作。