如何从 Google 日历 API .NET 中删除特定日历中的所有事件条目

发布于 2024-11-09 02:29:16 字数 2269 浏览 0 评论 0原文

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

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

 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 insertEntry = calendarService。 Insert(new Uri(calendarEntry.Links[0].AbsoluteUri), eventEntry);

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

I am trying editing a tool to allow a user to select from a list of their calendars and then clear all event entries / add new ones based on Microsoft project tasks.
Heres the original tool: http://daball.github.com/Microsoft-Project-to-Google-Calendar/

I am completely unexperienced with Google APIs / the calendar API, and am having some trouble. The program I'm editing keeps track of which CalendarEntry the user has selected from a list of their calendars. What I am currently trying to do is create a EventFeed which gives me the EventEntries of that selected calendar, so I can then delete all of them. The purpose of this is to allow someone to use this tool to also update the calendar from the project file whenever changes are made. Here's my function attempting the delete.

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

    }

My feed doesn't return the results I was hoping for, but to be honest I'm not sure how to request the events correctly.

query.Uri = new Uri(calendarEntry.Links[0].AbsoluteUri); is something I grabbed from the portion of the program which is adding event to a specific calendar

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

These posts are definitely related to what I'm looking for but I haven't arrived at a solution

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

数理化全能战士 2024-11-16 02:29:17

我最终找到的一种方法是从日历的 URI 中收集 calendarID,然后使用该 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!");
        }

    }

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

A way I eventually arrived at was gathering the calendarID from the URI of the calendar, and then creating a new EventQuery using that id. Here's the new version of the code above

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!");
        }

    }

I'm not particular happy with this, it seems odd to use string manipulation to gather the info and chop together my own query. But it works and I have been struggling to find a way to get this done.

北城挽邺 2024-11-16 02:29:16

尝试这样的操作:

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

这是相关帖子:

google calendar api asp.net c# delete event

这是一个有用的文档:

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

Try something like this:

            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)
            {
            }

You can find "Calendar ID" (something like this: 45kk8jl9nodfri1qgepsb65fnc%40group.calendar.google.com) from Calendar Details page inside Google Calendar.

this is a related post:

google calendar api asp.net c# delete event

this is a useful doc:

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文