最良の結果を得るためのベスト プラクティス
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Google Docs API を使用する際は、いくつかの原則に従う必要があります。たとえば、次のような疑問があります。
- 効率的に逆方向に編集する
- コラボレーションを計画する
WriteControl
フィールドを使用して状態の整合性を確保する
- タブを考慮する
以降のセクションでは、これらの原則について説明します。
効率的に逆方向に編集する
documents.batchUpdate
メソッドの 1 回の呼び出し内で、インデックスの場所の降順でリクエストを並べ替えます。これにより、挿入と削除によるインデックスの変更を計算する必要がなくなります。
コラボレーションを計画する
ドキュメントの状態が変化することを想定します。次の図に示すように、あるメソッド呼び出しと別のメソッド呼び出しの間に、他の共同編集者がドキュメントを更新する可能性があります。

インデックスが間違っていると、エラーが発生する可能性があります。複数のユーザーが UI を使用してドキュメントを編集する場合、Google ドキュメントがこの処理を透過的に行います。ただし、API クライアントとして、アプリでこれを管理する必要があります。ドキュメントの共同編集を想定していない場合でも、防御的にプログラミングし、ドキュメントの状態が一貫性を保つようにすることが重要です。一貫性を確保する方法の 1 つについては、WriteControl
セクションをご覧ください。
WriteControl で状態の整合性を確立する
ドキュメントを読み取ってから更新する場合、documents.batchUpdate
メソッドの WriteControl
フィールドを使用して、競合する変更の処理方法を制御できます。WriteControl
は、書き込みリクエストの実行方法に対する権限を提供します。
使用方法は次のとおりです。
documents.get
メソッドを使用してドキュメントを取得し、返された documents
リソースから revisionId
を保存します。
- 更新リクエストを作成します。
- 次の 2 つのオプションのいずれかを含む、オプションの
WriteControl
オブジェクトを含めます。
requiredRevisionId
フィールドは、書き込みリクエストが適用されるドキュメントの revisionId
に設定されます。API 読み取りリクエスト以降にドキュメントが変更された場合、書き込みリクエストは処理されず、エラーが返されます。
targetRevisionId
フィールドは、書き込みリクエストが適用されるドキュメントの revisionId
に設定されます。API 読み取りリクエスト以降にドキュメントが変更された場合、書き込みリクエストの変更は共同編集者の変更に対して適用されます。書き込みリクエストの結果には、書き込みリクエストの変更と共同編集者の変更の両方が組み込まれ、ドキュメントの新しいリビジョンが作成されます。ドキュメント サーバーは、コンテンツの統合を担当します。
WriteControl
を使用してバッチ リクエストを作成する方法の例については、バッチ リクエストの例をご覧ください。
タブを考慮する
1 つのドキュメントに複数のタブを含めることができます。この場合、API リクエストで特別な処理が必要になります。
次の点にご留意ください。
documents.get
メソッドで includeTabsContent
パラメータを true
に設定して、ドキュメント内のすべてのタブからコンテンツを取得します。デフォルトでは、すべてのタブのコンテンツが返されるわけではありません。
documents.batchUpdate
メソッドで、各 Request
を適用するタブの ID を指定します。各 Request
には、更新を適用するタブを指定する方法が含まれています。デフォルトでは、タブが指定されていない場合、ほとんどの場合、Request
はドキュメントの最初のタブに適用されます。詳しくは、Request
のドキュメントをご覧ください。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-29 UTC。
[null,null,["最終更新日 2025-08-29 UTC。"],[],[],null,["# Best practices for best results\n\nThere are several principles you should follow when using the Google Docs API.\nThese include:\n\n- Edit backwards for efficiency\n- Plan for collaboration\n- Ensure state consistency using the [`WriteControl`](/workspace/docs/api/reference/rest/v1/documents/batchUpdate#writecontrol) field\n- Take tabs into account\n\nThe following sections explain these principles.\n\nEdit backwards for efficiency\n-----------------------------\n\nWithin a single call to the\n[`documents.batchUpdate`](/workspace/docs/api/reference/rest/v1/documents/batchUpdate)\nmethod, order your requests in\n*descending order* of index location. This eliminates the need to compute the\nindex changes due to insertions and deletions.\n\nPlan for collaboration\n----------------------\n\nExpect the document state to change. Between one method call and another, other\ncollaborators might update the document, as shown in the following diagram:\n\nThis can lead to errors if your indexes are wrong. With multiple users editing a\ndocument using the UI, Google Docs takes care of this transparently. However,\nas an API client your app must manage this. Even if you don't anticipate\ncollaboration on the document, it's important to program defensively and make\nsure the document state remains consistent. For one way to ensure consistency,\nreview the [`WriteControl`](#establish-state-consistency) section.\n\nEstablish state consistency with WriteControl\n---------------------------------------------\n\nWhen you read and then update a document, you can control the behavior of how\ncompeting changes are handled using the\n[`WriteControl`](/workspace/docs/api/reference/rest/v1/documents/batchUpdate#writecontrol)\nfield in the `documents.batchUpdate` method. `WriteControl` provides authority\nover how write requests are executed.\n\nHere's how you use it:\n\n1. Get the document using the [`documents.get`](/workspace/docs/api/reference/rest/v1/documents/get) method and save the [`revisionId`](/workspace/docs/api/reference/rest/v1/documents#Document.FIELDS.revision_id) from the returned `documents` resource.\n2. Compose your update requests.\n3. Include an optional [`WriteControl`](/workspace/docs/api/reference/rest/v1/documents/batchUpdate#writecontrol) object with one of two options:\n 1. The `requiredRevisionId` field is set to the `revisionId` of the document the write request is applied to. If the document was modified since the API read request, the write request isn't processed and it returns an error.\n 2. The `targetRevisionId` field is set to the `revisionId` of the document the write request is applied to. If the document was modified since the API read request, the write request changes are applied against the collaborator changes. The result of the write request incorporates both the write request changes and the collaborator changes into a new revision of the document. The Docs server is responsible for merging the content.\n\nFor an example of how to construct a batch request using `WriteControl`, see\nthis [batch request example](/workspace/docs/api/how-tos/batch#example).\n\nTake tabs into account\n----------------------\n\nA single document can contain multiple [tabs](/workspace/docs/api/how-tos/tabs),\nwhich require specific handling in your API requests.\n\nHere's what to remember:\n\n1. Set the `includeTabsContent` parameter to `true` in the [`documents.get`](/workspace/docs/api/reference/rest/v1/documents/get) method to retrieve the content from all tabs in a document. By default, not all tab contents are returned.\n2. Specify the ID(s) of the tab(s) to apply each [`Request`](/workspace/docs/api/reference/rest/v1/documents/request#request) to in the [`documents.batchUpdate`](/workspace/docs/api/reference/rest/v1/documents/batchUpdate) method. Each [`Request`](/workspace/docs/api/reference/rest/v1/documents/request#request) includes a way to specify the tabs to apply the update to. By default, if a tab is not specified, the [`Request`](/workspace/docs/api/reference/rest/v1/documents/request#request) will in most cases be applied to the first tab in the document. Refer to the [`Request`](/workspace/docs/api/reference/rest/v1/documents/request#request)s documentation for specifics.\n\nRelated topics\n--------------\n\n- [Batch requests](/workspace/docs/api/how-tos/batch)\n- [Requests and responses](/workspace/docs/api/concepts/request-response)\n- [Work with tabs](/workspace/docs/api/how-tos/tabs)"]]