如果您熟悉 Google 云端硬盘 API,则 documentId 对应于 files 资源中的 id。
管理 Google 云端硬盘中的文档
文档文件存储在 Google 云端硬盘(我们的云端存储服务)中。虽然 Docs API 有自己的独立方法,但通常还需要使用 Google Drive API 方法来与用户的 Google 文档文件互动。例如,如需复制 Google 文档文件,请使用 Drive API 的 files.copy 方法。如需了解详情,请参阅复制现有文档。
默认情况下,使用 Docs API 时,新文档会保存到用户在云端硬盘上的根文件夹中。您可以选择将文件保存到云端硬盘文件夹。如需了解详情,请参阅使用 Google 云端硬盘文件夹。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-08-29。"],[],[],null,["# Document\n\nThis guide introduces concepts such as the primary methods that make up the\nGoogle Docs API, how to access a document, and the workflow when creating a\ndocument.\n\nAPI methods\n-----------\n\nThe [`documents`](/workspace/docs/api/reference/rest/v1/documents) resource\nprovides methods you use to invoke the Docs API. The following methods\nlet you create, read, and update Docs documents:\n\n- Use the [`documents.create`](/workspace/docs/api/reference/rest/v1/documents/create) method to create a document.\n- Use the [`documents.get`](/workspace/docs/api/reference/rest/v1/documents/get) method to retrieve the contents of a specified document.\n- Use the [`documents.batchUpdate`](/workspace/docs/api/reference/rest/v1/documents/batchUpdate) method to atomically perform a set of updates on a specified document.\n\nThe `documents.get` and `documents.batchUpdate` methods require a `documentId`\nas a parameter to specify the target document. The `documents.create` method\nreturns an instance of the created document, from which you can read the\n`documentId`. For more information about Docs API requests and\nresponse methods, see [Requests and\nresponses](/workspace/docs/api/concepts/request-response).\n\nDocument ID\n-----------\n\nThe `documentId` is the unique identifier for the document and it can be derived\nfrom a document's URL. It's a particular string containing letters, numbers, and\nsome special characters. Document IDs are stable, even if the document name\nchanges. \n\n https://docs.google.com/document/d/\u003cvar translate=\"no\"\u003eDOCUMENT_ID\u003c/var\u003e/edit\n\nThe following regular expression can be used to extract the `documentId` from a\nGoogle Docs URL: \n\n```\n/document/d/([a-zA-Z0-9-_]+)\n```\n\nIf you're familiar with the Google Drive API, the `documentId` corresponds to `id`\nin the [`files`](/workspace/drive/api/reference/rest/v3/files) resource.\n| **Tip:** If you're unsure what the `documentId` is for your file, experiment using the [Google APIs Explorer](https://developers.google.com/explorer-help/) on the [`documents.get`](/workspace/docs/api/reference/rest/v1/documents/get) method to retrieve a JSON representation of your document.\n\nManage documents in Google Drive\n--------------------------------\n\nDocs files are stored in Google Drive, our cloud-based storage\nservice. While Docs API has its own standalone methods, it's often\nnecessary to also use Google Drive API methods to interact with a user's\nDocs files. For example, to copy Docs files, use\nDrive API's [`files.copy`](/workspace/drive/api/reference/rest/v3/files/copy)\nmethod. For more information, see [Copy an existing\ndocument](/workspace/docs/api/how-tos/documents#copy_an_existing_document).\n\nBy default, when using the Docs API a new document is saved to the\nuser's root folder on Drive. There are options for saving a file\nto a Drive folder. For more information, see [Work with\nGoogle Drive folders](/workspace/docs/api/how-tos/documents#work_with_folders).\n\n### Work with Docs files\n\nTo retrieve a document from a user's My Drive, it's often\nnecessary to first use Drive's\n[`files.list`](/workspace/drive/api/reference/rest/v3/files/list) method to\nretrieve the ID for a file. Calling the method without any parameters returns a\nlist of all files and folders, including the IDs, for the user.\n\nA document's MIME type indicates the data type and format. The MIME type format\nfor Docs is `application/vnd.google-apps.document`. For a list of\nMIME types, see [Google Workspace and Google Drive supported MIME\ntypes](/workspace/drive/api/guides/mime-types).\n\nTo search by MIME type for just Docs files within My\nDrive, append the following query string filter: \n\n q: mimeType = 'application/vnd.google-apps.document'\n\nFor more information about query string filters, see [Search for files and\nfolders](/workspace/drive/api/guides/search-files).\n\nOnce you know the `documentId`, use the\n[`documents.get`](/workspace/docs/api/reference/rest/v1/documents/get) method to\nretrieve a complete instance of the specified document. For more information,\nsee [Requests and responses](/workspace/docs/api/concepts/request-response).\n\nTo export Google Workspace document byte content, use Drive's\n[`files.export`](/drive/api/v3/reference/files/export) method with the\n`documentId` of the file to export and the correct [export MIME\ntype](/workspace/drive/api/guides/ref-export-formats). For more information, see\n[Export Google Workspace document content](/workspace/drive/api/guides/manage-downloads#export-content).\n\n### Compare the `Get` and `List` methods\n\nThe following table describes the differences between the Drive\nand Docs methods, and the data that's returned with each:\n\n| Operator | Description | Usage |\n|-----------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------|\n| [`drive.files.get`](/workspace/drive/api/reference/rest/v3/files/get) | Gets a file's metadata by ID. Returns an instance of the [`files`](/workspace/drive/api/reference/rest/v3/files) resource. | Get the metadata for a specific file. |\n| [`drive.files.list`](/workspace/drive/api/reference/rest/v3/files/list) | Gets a user's files. Returns a list of files. | Get a list of user files when you're unsure which file you must modify. |\n| [`docs.documents.get`](/workspace/docs/api/reference/rest/v1/documents/get) | Gets the latest version of the specified document, including all formatting and text. Returns an instance of the [`documents`](/workspace/docs/api/reference/rest/v1/documents) resource. | Get the document for a specific document ID. |\n\nDocument creation workflow\n--------------------------\n\nCreating and populating a new document is straightforward, since there's no\nexisting content to worry about and there are no collaborators who can alter the\ndocument state. Conceptually, this works as shown in the following sequence\ndiagram:\n**Figure 1.** Workflow to create and populate a new document.\n\nIn the Figure 1., a user interacting with the\n[`documents`](/workspace/docs/api/reference/rest/v1/documents) resource has the\nfollowing flow of information:\n\n1. An app calls the [`documents.create`](/workspace/docs/api/reference/rest/v1/documents/create) method on a web server.\n2. The web server sends an HTTP response that contains an instance of the created document as a `documents` resource.\n3. Optionally, the app calls the [`documents.batchUpdate`](/workspace/docs/api/reference/rest/v1/documents/batchUpdate) method to atomically perform a set of edit requests to populate the document with data.\n4. The web server sends an HTTP response. Some `documents.batchUpdate` methods provide a response body with information about the applied requests, whereas others surface an empty response.\n\nDocument update workflow\n------------------------\n\nUpdating an existing document is more complex. Before you can make meaningful\ncalls to update a document, you must know its current state: what elements make\nit up, what content is in those elements, and the order of the elements within\nthe document. The following sequence diagram shows how this works:\n**Figure 2.** Workflow to update a document.\n\nIn the Figure 2., a user interacting with the `documents` resource has the\nfollowing flow of information:\n\n1. An app calls the [`documents.get`](/workspace/docs/api/reference/rest/v1/documents/get) method on a web server, with the `documentId` of the file to find.\n2. The web server sends an HTTP response that contains an instance of the specified document as a `documents` resource. The returned JSON contains the document content, formatting, and other features.\n3. The app parses the JSON so the user can determine what content or format to update.\n4. The app calls the `documents.batchUpdate` method to atomically perform a set of edit requests to update the document.\n5. The web server sends an HTTP response. Some `documents.batchUpdate` methods provide a response body with information about the applied requests, whereas others surface an empty response.\n\nThis diagram doesn't consider workflows where concurrent updates by other\ncollaborators are made in the same document. For more information, see the best\npractices section [Plan for\ncollaboration](/workspace/docs/api/how-tos/best-practices#plan-for-collaboration).\n\nRelated topics\n--------------\n\n- [Structure of a Google Docs document](/workspace/docs/api/concepts/structure)\n- [Requests and responses](/workspace/docs/api/concepts/request-response)\n- [Structural edit rules and behavior](/workspace/docs/api/concepts/rules-behavior)\n- [Best practices for best results](/workspace/docs/api/how-tos/best-practices)"]]