この Google Docs API ページでは、特定のタスクを実行する方法について説明します 次のようなドキュメントが含まれます。
- ドキュメントを作成して
- 既存のドキュメントをコピーする
以降のセクションで、これらのタスクについて詳しく説明します。
空白のドキュメントを作成する
ドキュメントを作成するには、
次のオブジェクトの documents.create
メソッド:
documents
コレクション。
次のコードサンプルは、指定した内容で空白のドキュメントを作成する方法を示しています。 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')))
Google ドライブ フォルダを操作する
指定したフォルダ内に直接ドキュメントを作成するオプションが Docs API を使用するドライブ フォルダです。デフォルトでは、 ユーザーのドライブ上のルートフォルダに保存されます。
ドライブにファイルを保存する方法は 2 つあります。 フォルダ:
ドキュメントが作成されたら、次のコマンドを使用して特定のフォルダに移動します。 Drive API の
files.update
メソッドを呼び出します。ファイルの移動について詳しくは、アカウント間でのファイルの移動をご覧ください フォルダをご覧ください。Drive API の
files.create
メソッドで、mimeType
としてのapplication/vnd.google-apps.document
。詳細情報 ファイルの作成について詳しくは、特定のフォルダでファイルを作成する フォルダです。
どちらの方法でも、適切な Drive API を アクセス スコープを できます。ドライブのスコープについて詳しくは、Google ドライブ内の 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; });
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')
適切なドライブ API を使用する必要があります。 scope を あります。ドライブのスコープについて詳しくは、Google ドライブ内の Google Drive API のスコープ。