Google 日历查询最多返回 25 个条目

发布于 2024-08-28 20:15:40 字数 1374 浏览 6 评论 0原文

我正在尝试删除从今天开始的所有日历条目。我运行一个查询,然后对查询结果调用 getEntries() 。 getEntries() 始终返回 25 个条目(如果日历上的条目少于 25 个,则返回更少)。为什么没有返回所有条目?我预计大约有 80 个条目。

作为测试,我尝试运行查询,删除返回的 25 个条目,再次运行查询,再次删除等等。这可行,但必须有更好的方法。

下面是只运行一次查询的 Java 代码。

CalendarQuery myQuery = new CalendarQuery(feedUrl);

DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'");
Date dt = Calendar.getInstance().getTime();

myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
// Make the end time far into the future so we delete everything
myQuery.setMaximumStartTime(DateTime.parseDateTime("2099-12-31T23:59:59"));

// Execute the query and get the response
CalendarEventFeed resultFeed = service.query(myQuery, CalendarEventFeed.class);

// !!! This returns 25 (or less if there are fewer than 25 entries on the calendar) !!!
int test = resultFeed.getEntries().size();

// Delete all the entries returned by the query
for (int j = 0; j < resultFeed.getEntries().size(); j++) {
   CalendarEventEntry entry = resultFeed.getEntries().get(j);

   entry.delete();
}

PS:我查看了数据 API 开发人员指南以及 Google Data API Javadoc。这些网站还可以,但不是很好。有谁知道额外的 Google API 文档吗?

I'm trying to delete all calendar entries from today forward. I run a query then call getEntries() on the query result. getEntries() always returns 25 entries (or less if there are fewer than 25 entries on the calendar). Why aren't all the entries returned? I'm expecting about 80 entries.

As a test, I tried running the query, deleting the 25 entries returned, running the query again, deleting again, etc. This works, but there must be a better way.

Below is the Java code that only runs the query once.

CalendarQuery myQuery = new CalendarQuery(feedUrl);

DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'");
Date dt = Calendar.getInstance().getTime();

myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt)));
// Make the end time far into the future so we delete everything
myQuery.setMaximumStartTime(DateTime.parseDateTime("2099-12-31T23:59:59"));

// Execute the query and get the response
CalendarEventFeed resultFeed = service.query(myQuery, CalendarEventFeed.class);

// !!! This returns 25 (or less if there are fewer than 25 entries on the calendar) !!!
int test = resultFeed.getEntries().size();

// Delete all the entries returned by the query
for (int j = 0; j < resultFeed.getEntries().size(); j++) {
   CalendarEventEntry entry = resultFeed.getEntries().get(j);

   entry.delete();
}

PS: I've looked at the Data API Developer's Guide and the Google Data API Javadoc. These sites are okay, but not great. Does anyone know of additional Google API documentation?

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

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

发布评论

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

评论(5

黄昏下泛黄的笔记 2024-09-04 20:15:40

您可以使用 myQuery.setMaxResults() 增加结果数量。不过,会有一个最大的最大值,因此您可以通过改变 myQuery.setStartIndex() 来进行多个查询(“分页”结果)。

http://code. google.com/apis/gdata/javadoc/com/google/gdata/client/Query.html#setMaxResults(int)
http://code. google.com/apis/gdata/javadoc/com/google/gdata/client/Query.html#setStartIndex(int)

You can increase the number of results with myQuery.setMaxResults(). There will be a maximum maximum though, so you can make multiple queries ('paged' results) by varying myQuery.setStartIndex().

http://code.google.com/apis/gdata/javadoc/com/google/gdata/client/Query.html#setMaxResults(int)
http://code.google.com/apis/gdata/javadoc/com/google/gdata/client/Query.html#setStartIndex(int)

没有心的人 2024-09-04 20:15:40

根据 Jim Blackler 和 Chris Kaminski 的回答,我增强了代码以读取页面中的查询结果。我还进行批量删除,这应该比单独删除要快。

我在这里提供 Java 代码,以防它对任何人有用。

CalendarQuery myQuery = new CalendarQuery(feedUrl);

DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'"); 
Date dt = Calendar.getInstance().getTime(); 

myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt))); 
// Make the end time far into the future so we delete everything 
myQuery.setMaximumStartTime(DateTime.parseDateTime("2099-12-31T23:59:59")); 

// Set the maximum number of results to return for the query.
// Note: A GData server may choose to provide fewer results, but will never provide
// more than the requested maximum.
myQuery.setMaxResults(5000);
int startIndex = 1;
int entriesReturned;

List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>();
CalendarEventFeed resultFeed;

// Run our query as many times as necessary to get all the
// Google calendar entries we want
while (true) {
    myQuery.setStartIndex(startIndex);

    // Execute the query and get the response
    resultFeed = service.query(myQuery, CalendarEventFeed.class);

    entriesReturned = resultFeed.getEntries().size();
    if (entriesReturned == 0)
        // We've hit the end of the list
        break;

    // Add the returned entries to our local list
    allCalEntries.addAll(resultFeed.getEntries());

    startIndex = startIndex + entriesReturned;
}

// Delete all the entries as a batch delete
CalendarEventFeed batchRequest = new CalendarEventFeed();

for (int i = 0; i < allCalEntries.size(); i++) {
    CalendarEventEntry entry = allCalEntries.get(i);

    BatchUtils.setBatchId(entry, Integer.toString(i));
    BatchUtils.setBatchOperationType(entry, BatchOperationType.DELETE);
    batchRequest.getEntries().add(entry);
}

// Get the batch link URL and send the batch request
Link batchLink = resultFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
CalendarEventFeed batchResponse = service.batch(new URL(batchLink.getHref()), batchRequest);

// Ensure that all the operations were successful
boolean isSuccess = true;
StringBuffer batchFailureMsg = new StringBuffer("These entries in the batch delete failed:");
for (CalendarEventEntry entry : batchResponse.getEntries()) {
    String batchId = BatchUtils.getBatchId(entry);
    if (!BatchUtils.isSuccess(entry)) {
        isSuccess = false;
        BatchStatus status = BatchUtils.getBatchStatus(entry);
        batchFailureMsg.append("\nID: " + batchId + "  Reason: " + status.getReason());
    }
}

if (!isSuccess) {
    throw new Exception(batchFailureMsg.toString());
}

Based on the answers from Jim Blackler and Chris Kaminski, I enhanced my code to read the query results in pages. I also do the delete as a batch, which should be faster than doing individual deletions.

I'm providing the Java code here in case it is useful to anyone.

CalendarQuery myQuery = new CalendarQuery(feedUrl);

DateFormat dfGoogle = new SimpleDateFormat("yyyy-MM-dd'T00:00:00'"); 
Date dt = Calendar.getInstance().getTime(); 

myQuery.setMinimumStartTime(DateTime.parseDateTime(dfGoogle.format(dt))); 
// Make the end time far into the future so we delete everything 
myQuery.setMaximumStartTime(DateTime.parseDateTime("2099-12-31T23:59:59")); 

// Set the maximum number of results to return for the query.
// Note: A GData server may choose to provide fewer results, but will never provide
// more than the requested maximum.
myQuery.setMaxResults(5000);
int startIndex = 1;
int entriesReturned;

List<CalendarEventEntry> allCalEntries = new ArrayList<CalendarEventEntry>();
CalendarEventFeed resultFeed;

// Run our query as many times as necessary to get all the
// Google calendar entries we want
while (true) {
    myQuery.setStartIndex(startIndex);

    // Execute the query and get the response
    resultFeed = service.query(myQuery, CalendarEventFeed.class);

    entriesReturned = resultFeed.getEntries().size();
    if (entriesReturned == 0)
        // We've hit the end of the list
        break;

    // Add the returned entries to our local list
    allCalEntries.addAll(resultFeed.getEntries());

    startIndex = startIndex + entriesReturned;
}

// Delete all the entries as a batch delete
CalendarEventFeed batchRequest = new CalendarEventFeed();

for (int i = 0; i < allCalEntries.size(); i++) {
    CalendarEventEntry entry = allCalEntries.get(i);

    BatchUtils.setBatchId(entry, Integer.toString(i));
    BatchUtils.setBatchOperationType(entry, BatchOperationType.DELETE);
    batchRequest.getEntries().add(entry);
}

// Get the batch link URL and send the batch request
Link batchLink = resultFeed.getLink(Link.Rel.FEED_BATCH, Link.Type.ATOM);
CalendarEventFeed batchResponse = service.batch(new URL(batchLink.getHref()), batchRequest);

// Ensure that all the operations were successful
boolean isSuccess = true;
StringBuffer batchFailureMsg = new StringBuffer("These entries in the batch delete failed:");
for (CalendarEventEntry entry : batchResponse.getEntries()) {
    String batchId = BatchUtils.getBatchId(entry);
    if (!BatchUtils.isSuccess(entry)) {
        isSuccess = false;
        BatchStatus status = BatchUtils.getBatchStatus(entry);
        batchFailureMsg.append("\nID: " + batchId + "  Reason: " + status.getReason());
    }
}

if (!isSuccess) {
    throw new Exception(batchFailureMsg.toString());
}
内心激荡 2024-09-04 20:15:40

API页面上有一个小引用
http://code.google.com/apis/calendar/ data/1.0/reference.html#参数

注意:日历的最大结果查询参数默认设置为 25,
这样您就不会收到完整的
日历意外进给。如果你想
要接收整个提要,您可以
指定一个非常大的数字
最大结果。

因此,要从 Google 日历 Feed 获取所有事件,我们执行以下操作:

google.calendarurl.com/.../basic?max-results=999999您也可以使用 setMaxResults=999999 进行查询

API 中的

There is a small quote on the API page
http://code.google.com/apis/calendar/data/1.0/reference.html#Parameters

Note: The max-results query parameter for Calendar is set to 25 by default,
so that you won't receive an entire
calendar feed by accident. If you want
to receive the entire feed, you can
specify a very large number for
max-results.

So to get all events from a google calendar feed, we do this:

google.calendarurl.com/.../basic?max-results=999999

in the API you can also query with setMaxResults=999999

绻影浮沉 2024-09-04 20:15:40

我是在寻找 Python 解决方案时来到这里的;
如果有人遇到同样的问题,重要的是第四行:

query = gdata.calendar.service.CalendarEventQuery(cal, visibility, projection)
query.start_min = start_date
query.start_max = end_date 
query.max_results = 1000

I got here while searching for a Python solution;
Should anyone be stuck in the same way, the important line is the fourth:

query = gdata.calendar.service.CalendarEventQuery(cal, visibility, projection)
query.start_min = start_date
query.start_max = end_date 
query.max_results = 1000
℉服软 2024-09-04 20:15:40

不幸的是,谷歌将限制您可以检索的最大查询数量。这是为了让查询调控器遵守其指导原则(例如,HTTP 请求的时间不允许超过 30 秒)。他们已经围绕此构建了整个架构,因此您不妨构建现有的逻辑。

Unfortunately, Google is going to limit the maximum number of queries you can retrieve. This is so as to keep the query governor in their guidelines (HTTP requests not allowed to take more than 30 seconds, for example). They've built their whole architecture around this, so you might as well build the logic as you have.

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