如果您熟悉 Google Drive API,documentId 對應於 files 資源中的 id。
管理 Google 雲端硬碟中的文件
文件檔案會儲存在 Google 雲端硬碟,也就是我們的雲端儲存服務。雖然文件 API 有自己的獨立方法,但通常也需要使用 Google Drive API 方法與使用者的文件檔案互動。舉例來說,如要複製 Google 文件檔案,請使用 Drive API 的 files.copy 方法。詳情請參閱「複製現有文件」。
根據預設,使用 Google 文件 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"]],["上次更新時間: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)"]]