4

今日以降のすべてのカレンダー エントリを削除しようとしています。クエリを実行し、クエリ結果に対して getEntries() を呼び出します。getEntries() は常に 25 エントリ (カレンダーのエントリが 25 未満の場合はそれ以下) を返します。すべてのエントリが返されないのはなぜですか? 約80点のエントリーを予定しています。

テストとして、クエリの実行、返された 25 エントリの削除、クエリの再実行、削除などを試してみました。これは機能しますが、もっと良い方法があるはずです。

以下は、クエリを 1 回だけ実行する 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

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

APIページ http://code.google.com/apis/calendar/data/1.0/reference.html#Parametersに小さな引用があります

注:Calendarのmax-resultsクエリパラメーターはデフォルトで25に設定されているため、誤ってカレンダーフィード全体を受信することはありません。フィード全体を受け取りたい場合は、max-resultsに非常に大きな数を指定できます。

したがって、Googleカレンダーフィードからすべてのイベントを取得するには、次のようにします。

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

APIでは、setMaxResults=999999を使用してクエリを実行することもできます

于 2010-09-03T01:47:13.157 に答える
1

Python ソリューションを探しているときにここにたどり着きました。誰かが同じように行き詰まった場合、重要なのは 4 行目です。

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

残念ながら、Google は取得できるクエリの最大数を制限しようとしています。これは、クエリ ガバナーをガイドラインに準拠させるためです (たとえば、HTTP 要求は 30 秒以上かかることは許可されていません)。彼らはこれを中心にアーキテクチャ全体を構築したので、あなたが持っているのと同じようにロジックを構築することもできます.

于 2010-04-15T15:37:11.017 に答える