Membuat dan mengelola dokumen

Halaman Google Docs API ini menjelaskan cara melakukan tugas tingkat tinggi tertentu yang melibatkan dokumen Google Dokumen, seperti:

  • Buat dokumen
  • Menyalin dokumen yang ada

Paragraf berikut menjelaskan tugas-tugas ini secara detail.

Buat dokumen kosong

Untuk membuat dokumen, gunakan metode documents.create pada koleksi documents.

Contoh kode berikut menunjukkan cara membuat dokumen kosong dengan judul yang ditentukan:

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')))

Menggunakan folder Google Drive

Tidak ada opsi untuk membuat dokumen secara langsung dalam folder Drive yang ditentukan menggunakan Docs API. Secara default, dokumen yang dibuat disimpan ke folder root pengguna di Drive.

Namun, ada dua alternatif untuk menyimpan file ke folder Drive:

  • Setelah dokumen dibuat, pindahkan ke folder tertentu menggunakan metode files.update Drive API. Untuk mengetahui informasi selengkapnya tentang cara memindahkan file, lihat Memindahkan file antarfolder.

  • Tambahkan dokumen kosong ke folder menggunakan metode files.create Drive API, dengan menentukan application/vnd.google-apps.document sebagai mimeType. Untuk mengetahui informasi selengkapnya tentang cara membuat file, lihat Membuat file di folder tertentu.

Untuk kedua alternatif tersebut, Anda harus menambahkan cakupan Drive API yang sesuai untuk mengizinkan panggilan. Untuk informasi selengkapnya tentang cakupan Drive, lihat Memilih cakupan Google Drive API.

Untuk memindahkan atau membuat file dalam folder drive bersama, lihat Menerapkan dukungan drive bersama.

Menyalin dokumen yang ada

Untuk menyalin dokumen, gunakan metode files.copy Drive API.

Contoh kode berikut menunjukkan cara menyalin dokumen yang ada. Anda dapat menemukan ID yang akan digunakan untuk panggilan Drive API di URL dokumen. Untuk mengetahui informasi selengkapnya, baca ID Dokumen.

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')

Perhatikan bahwa Anda harus menggunakan cakupan Drive API yang sesuai untuk mengizinkan panggilan. Untuk informasi selengkapnya tentang cakupan Drive, lihat Memilih cakupan Google Drive API.