Tạo và quản lý tài liệu

Trang API Google Tài liệu này mô tả cách thực hiện một số tác vụ cấp cao liên quan đến các tài liệu Google Tài liệu, chẳng hạn như:

  • Tạo một tài liệu
  • Sao chép tài liệu hiện có

Các đoạn sau đây sẽ mô tả chi tiết những công việc này.

Tạo tài liệu trống

Để tạo tài liệu, hãy sử dụng phương thức documents.create trên bộ sưu tập documents.

Mã mẫu sau đây cho biết cách tạo tài liệu trống có tiêu đề được chỉ định:

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());
}

1.199

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

Thao tác với các thư mục trên Google Drive

Bạn không thể tạo tài liệu ngay trong thư mục Drive được chỉ định bằng API Tài liệu. Theo mặc định, tài liệu đã tạo sẽ được lưu vào thư mục gốc của người dùng trên Drive.

Tuy nhiên, có hai phương án thay thế để lưu tệp vào thư mục Drive:

  • Sau khi tạo tài liệu, hãy di chuyển tài liệu đó vào một thư mục cụ thể bằng phương thức files.update của API Drive. Để biết thêm thông tin về cách di chuyển tệp, hãy xem phần Di chuyển tệp giữa các thư mục.

  • Thêm tài liệu trống vào một thư mục bằng phương thức files.create của API Drive, chỉ định application/vnd.google-apps.document làm mimeType. Để biết thêm thông tin về cách tạo tệp, hãy xem phần Tạo tệp trong một thư mục cụ thể.

Dù là cách nào khác, bạn cũng cần thêm phạm vi API Drive thích hợp để cho phép thực hiện lệnh gọi. Để biết thêm thông tin về các phạm vi trong Drive, hãy xem phần Chọn các phạm vi API Google Drive.

Để di chuyển hoặc tạo một tệp trong thư mục bộ nhớ dùng chung, hãy xem phần Triển khai hỗ trợ bộ nhớ dùng chung.

Sao chép tài liệu hiện có

Để sao chép tài liệu, hãy sử dụng phương thức files.copy của API Drive.

Mã mẫu sau đây cho biết cách sao chép một tài liệu hiện có. Bạn có thể tìm thấy mã nhận dạng để sử dụng cho lệnh gọi API Drive trong URL của tài liệu. Để biết thêm thông tin, hãy xem phần Mã tài liệu.

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;
});

1.199

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

Lưu ý rằng bạn cần sử dụng phạm vi API Drive thích hợp để cho phép thực hiện lệnh gọi. Để biết thêm thông tin về các phạm vi trong Drive, hãy xem phần Chọn các phạm vi API Google Drive.