문서 작성 및 관리

이 Google Docs API 페이지에서는 다음과 같은 Google Docs 문서와 관련된 상위 수준의 작업을 수행하는 방법을 설명합니다.

  • 문서 새로 만들어 줘
  • 기존 문서 복사

다음 단락에서는 이러한 작업을 자세히 설명합니다.

빈 문서 만들기

문서를 만들려면 documents 컬렉션의 documents.create 메서드를 사용합니다.

다음 코드 샘플은 지정된 제목으로 빈 문서를 만드는 방법을 보여줍니다.

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

2,399필리핀

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

Google Drive 폴더 사용

Docs API를 사용하여 지정된 Drive 폴더 내에 직접 문서를 만드는 옵션은 없습니다. 기본적으로 생성된 문서는 Drive의 사용자 루트 폴더에 저장됩니다.

하지만 파일을 Drive 폴더에 저장하는 방법에는 두 가지가 있습니다.

  • 문서를 만든 후 Drive API의 files.update 메서드를 사용하여 특정 폴더로 이동합니다. 파일 이동에 관한 자세한 내용은 폴더 간 파일 이동을 참고하세요.

  • Drive API의 files.create 메서드를 사용하여 폴더에 빈 문서를 추가하고 application/vnd.google-apps.documentmimeType로 지정합니다. 파일 만들기에 관한 자세한 내용은 특정 폴더에서 파일 만들기를 참고하세요.

두 방법 모두 적절한 Drive API 범위를 추가하여 호출을 승인해야 합니다. 드라이브 범위에 대한 자세한 내용은 Google Drive API 범위 선택을 참고하세요.

공유 드라이브 폴더 내에서 파일을 이동하거나 만들려면 공유 드라이브 지원 구현을 참고하세요.

기존 문서 복사

문서를 복사하려면 Drive API의 files.copy 메서드를 사용합니다.

다음 코드 샘플은 기존 문서를 복사하는 방법을 보여줍니다. 문서 URL에서 Drive API 호출에 사용할 ID를 찾을 수 있습니다. 자세한 내용은 문서 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;
});

2,399필리핀

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

호출을 승인하려면 적절한 Drive API 범위를 사용해야 합니다. 드라이브 범위에 대한 자세한 내용은 Google Drive API 범위 선택을 참고하세요.