Dokumente erstellen und verwalten

Auf dieser Seite zum Google Docs-API wird beschrieben, wie bestimmte übergeordnete Aufgaben ausgeführt werden. mit Google Docs-Dokumenten wie

  • 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 den Sammlung documents.

Das folgende Codebeispiel zeigt, wie Sie ein leeres Dokument mit einem Titel:

Java

private static void createDoc(Docs service) throws IOException {
    Document doc = new Document()
            .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

Es gibt keine Option, ein Dokument direkt in einem bestimmten Drive-Ordner, der die Docs API verwendet. Standardmäßig enthält der Parameter erstelltes Dokument wird im Stammordner des Nutzers in Drive gespeichert.

Es gibt jedoch zwei Alternativen zum Speichern einer Datei in Google Drive. Ordner:

  • Nachdem das Dokument erstellt wurde, verschieben Sie es mithilfe von files.update der Drive API . Weitere Informationen zum Verschieben von Dateien finden Sie unter Dateien zwischen Ordner.

  • Ein leeres Dokument mithilfe der Drive APIs zu einem Ordner hinzufügen Methode files.create mit Angabe von application/vnd.google-apps.document als mimeType. Weitere Informationen Informationen zum Erstellen von Dateien finden Sie unter Erstellen einer Datei in einem bestimmten .

In beiden Fällen müssen Sie die entsprechende Drive API hinzufügen Bereiche zum Autorisieren den Anruf. Weitere Informationen zu Drive-Bereichen finden Sie unter Auswählen von Google Drive API-Bereiche.

Weitere Informationen zum Verschieben oder Erstellen einer Datei in einem Ordner einer geteilten Ablage finden Sie unter Freigegebene Dateien implementieren Drive-Support.

Vorhandenes Dokument kopieren

Verwenden Sie zum Kopieren eines Dokuments die Drive APIs files.copy-Methode.

Das folgende Codebeispiel zeigt, wie Sie ein vorhandenes Dokument kopieren. Sie finden Die ID, die für den Drive API-Aufruf in der Dokument-URL verwendet werden soll. Weitere Informationen Weitere Informationen finden Sie unter Dokument-ID.

https://docs.google.com/document/d/DOCUMENT_ID/edit

Java

String copyTitle = "Copy Title";
File copyMetadata = new File().setName(copyTitle);
File documentCopyFile =
        driveService.files().copy(documentId, copyMetadata).execute();
String documentCopyId = documentCopyFile.getId();

Node.js

var copyTitle = "Copy Title";
let request = {
  name: copyTitle,
};
this.driveService.files.copy({
  fileId: documentId,
  resource: request,
}, (err, driveResponse) => {
  let documentCopyId = driveResponse.id;
});

PHP

<?php
$copyTitle = 'Copy Title';
$copy = new Google_Service_Drive_DriveFile(array(
    'name' => $copyTitle
));
$driveResponse = $driveService->files->copy($documentId, $copy);
$documentCopyId = $driveResponse->id;

Python

copy_title = 'Copy Title'
body = {
    'name': copy_title
}
drive_response = drive_service.files().copy(
    fileId=document_id, body=body).execute()
document_copy_id = drive_response.get('id')

Sie müssen eine geeignete Drive API verwenden. aufrufen. Weitere Informationen zu Drive-Bereichen finden Sie unter Auswählen von Google Drive API-Bereiche.