Mit Sammlungen den Überblick behalten Sie können Inhalte basierend auf Ihren Einstellungen speichern und kategorisieren.
Auf dieser Seite der Google Docs API wird beschrieben, wie Sie bestimmte allgemeine Aufgaben mit Google Docs-Dokumenten ausführen, z. B.:
Dokument erstellen
Vorhandenes Dokument kopieren
In den folgenden Abschnitten werden diese Aufgaben ausführlich beschrieben.
Leeres Dokument erstellen
Verwenden Sie zum Erstellen eines Dokuments die Methode documents.create für die Sammlung documents.
Das folgende Codebeispiel zeigt, wie ein leeres Dokument mit einem bestimmten Titel erstellt wird:
Java
privatestaticvoidcreateDoc(Docsservice)throwsIOException{Documentdoc=newDocument().setTitle("My Document");doc=service.documents().create(doc).execute();System.out.println("Created document with title: "+doc.getTitle());}
PHP
$title = 'My Document';$document = new Google_Service_Docs_Document(array( 'title' => $title));$document = $service->documents->create($document);printf("Created document with title: %s\n", $document->title);
Python
title='My Document'body={'title':title}doc=service.documents() \ .create(body=body).execute()print('Created document with title: {0}'.format(doc.get('title')))
Mit Google Drive-Ordnern arbeiten
Mit der Docs API können Sie kein Dokument direkt in einem bestimmten Drive-Ordner erstellen. Standardmäßig wird das erstellte Dokument im Stammordner des Nutzers in Drive gespeichert.
Es gibt jedoch zwei Alternativen zum Speichern einer Datei in einem Drive-Ordner:
Verschieben Sie das Dokument nach dem Erstellen mit der Methode files.update der Drive API in einen bestimmten Ordner. Weitere Informationen zum Verschieben von Dateien finden Sie unter Dateien zwischen Ordnern verschieben.
Fügen Sie einem Ordner mithilfe der Methode files.create der Drive API ein leeres Dokument hinzu und geben Sie application/vnd.google-apps.document als mimeType an. Weitere Informationen zum Erstellen von Dateien finden Sie unter Datei in einem bestimmten Ordner erstellen.
Verwenden Sie die Methode files.copy der Drive API, um ein Dokument zu kopieren.
Im folgenden Codebeispiel wird gezeigt, wie ein vorhandenes Dokument kopiert wird. Die für den Drive API-Aufruf zu verwendende ID finden Sie in der Dokument-URL. Weitere Informationen finden Sie unter Dokument-ID.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Benötigte Informationen nicht gefunden","missingTheInformationINeed","thumb-down"],["Zu umständlich/zu viele Schritte","tooComplicatedTooManySteps","thumb-down"],["Nicht mehr aktuell","outOfDate","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Problem mit Beispielen/Code","samplesCodeIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-08-01 (UTC)."],[],[],null,["This Google Docs API page describes how to perform certain high-level tasks\ninvolving Google Docs documents, such as:\n\n- Create a document\n- Copy an existing document\n\nThe following paragraphs describe these tasks in detail.\n\nCreate a blank document\n\nTo create a document, use the\n[`documents.create`](/workspace/docs/api/reference/rest/v1/documents/create) method on the\n[`documents`](/workspace/docs/api/reference/rest/v1/documents) collection.\n\nThe following code sample shows how to create a blank document with a specified\ntitle: \n\nJava\n\n\n```java\nprivate static void createDoc(Docs service) throws IOException {\n Document doc = new Document()\n .setTitle(\"My Document\");\n doc = service.documents().create(doc)\n .execute();\n System.out.println(\"Created document with title: \" + doc.getTitle());\n}\n```\n\n\u003cbr /\u003e\n\nPHP\n\n\n```php\n$title = 'My Document';\n$document = new Google_Service_Docs_Document(array(\n 'title' =\u003e $title\n));\n\n$document = $service-\u003edocuments-\u003ecreate($document);\nprintf(\"Created document with title: %s\\n\", $document-\u003etitle);\n```\n\n\u003cbr /\u003e\n\nPython\n\n\n```python\ntitle = 'My Document'\nbody = {\n 'title': title\n}\ndoc = service.documents() \\\n .create(body=body).execute()\nprint('Created document with title: {0}'.format(\n doc.get('title')))\n```\n\n\u003cbr /\u003e\n\nWork with Google Drive folders\n\nThere's no option to create a document directly within a specified\nDrive folder using the Docs API. By default, the\ncreated document is saved to the user's root folder on Drive.\n\nHowever, there are two alternatives to saving a file to a Drive\nfolder:\n\n- After the document is created, move it to a specific folder using\n Drive API's\n [`files.update`](/workspace/drive/api/v3/reference/files/update) method. For\n more information on moving files, see [Move files between\n folders](/workspace/drive/api/guides/folder#move-files).\n\n- Add a blank document to a folder using the Drive API's\n [`files.create`](/workspace/drive/api/v3/reference/files/create) method,\n specifying `application/vnd.google-apps.document` as the `mimeType`. For\n more information on creating files, see [Create a file in a specific\n folder](/workspace/drive/api/guides/folder#create-file).\n\nFor either alternative, you'll need to add the appropriate [Drive API\nscopes](/workspace/drive/api/v3/reference/files/create#authorization-scopes) to\nauthorize the call. For more information on Drive scopes, see\n[Choose Google Drive API scopes](/workspace/drive/api/guides/api-specific-auth).\n\nTo move or create a file within a shared drive folder, see [Implement shared\ndrive support](/workspace/drive/api/guides/enable-shareddrives).\n\nCopy an existing document\n\nTo copy a document, use Drive API's\n[`files.copy`](/workspace/drive/v3/reference/files/copy) method.\n\nThe following code sample shows how to copy an existing document. You can find\nthe ID to use for the Drive API call in the document URL. For more\ninformation, see\n[Document ID](/workspace/docs/api/concepts/document#document-id). \n\n https://docs.google.com/document/d/\u003cvar translate=\"no\"\u003eDOCUMENT_ID\u003c/var\u003e/edit\n\nJava\n\n\n```java\nString copyTitle = \"Copy Title\";\nFile copyMetadata = new File().setName(copyTitle);\nFile documentCopyFile =\n driveService.files().copy(documentId, copyMetadata).execute();\nString documentCopyId = documentCopyFile.getId();\n```\n\n\u003cbr /\u003e\n\nNode.js\n\n\n```javascript\nvar copyTitle = \"Copy Title\";\nlet request = {\n name: copyTitle,\n};\nthis.driveService.files.copy({\n fileId: documentId,\n resource: request,\n}, (err, driveResponse) =\u003e {\n let documentCopyId = driveResponse.id;\n});\n```\n\n\u003cbr /\u003e\n\nPHP\n\n\n```php\n\u003c?php\n$copyTitle = 'Copy Title';\n$copy = new Google_Service_Drive_DriveFile(array(\n 'name' =\u003e $copyTitle\n));\n$driveResponse = $driveService-\u003efiles-\u003ecopy($documentId, $copy);\n$documentCopyId = $driveResponse-\u003eid;\n```\n\n\u003cbr /\u003e\n\nPython\n\n\n```python\ncopy_title = 'Copy Title'\nbody = {\n 'name': copy_title\n}\ndrive_response = drive_service.files().copy(\n fileId=document_id, body=body).execute()\ndocument_copy_id = drive_response.get('id')\n```\n\n\u003cbr /\u003e\n\nNote that you need to use an appropriate [Drive API\nscope](/workspace/drive/v3/reference/files/copy#authorization-scopes)\nto authorize the call. For more information on Drive scopes, see\n[Choose Google Drive API scopes](/workspace/drive/api/guides/api-specific-auth).\n\nRelated topics\n\n- [Insert, delete, and move text](/workspace/docs/api/how-tos/move-text)\n- [Merge text into a document](/workspace/docs/api/how-tos/merge)\n- [Document concepts](/workspace/docs/api/concepts/document)"]]