Membuat dan mengelola dokumen

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

  • Buat dokumen
  • Menyalin dokumen yang sudah ada

Paragraf berikut menjelaskan tugas ini secara mendetail.

Membuat dokumen kosong

Untuk membuat dokumen, gunakan Metode documents.create di Koleksi documents.

Contoh kode berikut menunjukkan cara membuat dokumen kosong dengan title:

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 menggunakan Docs API. Secara default, dokumen yang dibuat disimpan ke folder root pengguna di Drive.

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

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

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

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

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

Menyalin dokumen yang sudah ada

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

Contoh kode berikut menunjukkan cara menyalin dokumen yang sudah ada. Anda dapat menemukan ID yang akan digunakan untuk panggilan Drive API di URL dokumen. Untuk selengkapnya informasinya, lihat 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 perlu menggunakan Drive API yang sesuai cakupan untuk mengizinkan panggilan telepon. Untuk informasi selengkapnya tentang cakupan Drive, lihat Pilih Cakupan Google Drive API.