4

我正在尝试从今天开始删除所有日历条目。我运行一个查询,然后在查询结果上调用 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:我查看了Data API Developer's GuideGoogle Data API Javadoc。这些网站还可以,但不是很好。有人知道其他 Google API 文档吗?

4

5 回答 5

9

您可以使用 增加结果数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)

于 2010-04-15T15:30:28.190 回答
3

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());
}
于 2010-04-20T18:49:17.630 回答
2

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

于 2010-09-03T01:47:13.157 回答
1

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
于 2011-08-12T07:50:00.240 回答
0

不幸的是,谷歌将限制您可以检索的最大查询数。这是为了让查询管理器保持在他们的指导方针中(例如,HTTP 请求不允许超过 30 秒)。他们已经围绕这个构建了他们的整个架构,所以你也可以像你一样构建逻辑。

于 2010-04-15T15:37:11.017 回答