Google ドライブのファイルやフォルダを作成、検索、変更できます。共有ドライブ内のファイルやフォルダにアクセスするには、高度なドライブ サービスを使用します。
// Logs the name of every file in the user's Drive. var files = DriveApp.getFiles(); while (files.hasNext()) { var file = files.next(); console.log(file.getName()); }
プロパティ
プロパティ | タイプ | 説明 |
---|---|---|
Access | Access | ファイルまたはフォルダにアクセスできるユーザーのクラスを表す列挙型。明示的にアクセス権が付与された個別のユーザーも含まれます。 |
Permission | Permission | 明示的にアクセス権が付与されている個々のユーザーに加えて、ファイルまたはフォルダにアクセスできるユーザーに付与された権限を表す列挙型。 |
Methods
メソッド | 戻り値の型 | 概要 |
---|---|---|
continueFileIterator(continuationToken) | FileIterator | 前のイテレータの継続トークンを使用してファイルの反復処理を再開します。 |
continueFolderIterator(continuationToken) | FolderIterator | 前のイテレータの継続トークンを使用してフォルダの反復処理を再開します。 |
createFile(blob) | File | 任意のデータを指定して、Blob をユーザーのドライブのルートに作成します。 |
createFile(name, content) | File | ユーザーのドライブのルートに、指定した名前とコンテンツでテキスト ファイルを作成します。 |
createFile(name, content, mimeType) | File | ユーザーのドライブのルートに、指定した名前、内容、MIME タイプのファイルを作成します。 |
createFolder(name) | Folder | ユーザーのドライブのルートに、指定した名前のフォルダを作成します。 |
createShortcut(targetId) | File | 指定したドライブ アイテム ID へのショートカットを作成して返します。 |
createShortcutForTargetIdAndResourceKey(targetId, targetResourceKey) | File | 指定したドライブのアイテム ID とリソースキーへのショートカットを作成して返します。 |
enforceSingleParent(value) | void | アイテムの親に影響するすべての呼び出しに対する forceSingleParent の動作を有効または無効にします。 |
getFileById(id) | File | 指定された ID のファイルを取得します。 |
getFileByIdAndResourceKey(id, resourceKey) | File | 指定された ID とリソースキーでファイルを取得します。 |
getFiles() | FileIterator | ユーザーのドライブ内のすべてのファイルのコレクションを取得します。 |
getFilesByName(name) | FileIterator | ユーザーのドライブ内にある、指定した名前を持つすべてのファイルのコレクションを取得します。 |
getFilesByType(mimeType) | FileIterator | 指定した MIME タイプのユーザーのドライブ内にあるすべてのファイルのコレクションを取得します。 |
getFolderById(id) | Folder | 指定された ID のフォルダを取得します。 |
getFolderByIdAndResourceKey(id, resourceKey) | Folder | 指定された ID とリソースキーを持つフォルダを取得します。 |
getFolders() | FolderIterator | ユーザーのドライブ内のすべてのフォルダのコレクションを取得します。 |
getFoldersByName(name) | FolderIterator | ユーザーのドライブ内にある、指定した名前を持つすべてのフォルダのコレクションを取得します。 |
getRootFolder() | Folder | ユーザーのドライブのルートにあるフォルダを取得します。 |
getStorageLimit() | Integer | ユーザーがドライブに保存できるバイト数を取得します。 |
getStorageUsed() | Integer | ユーザーが現在ドライブに保存しているバイト数を取得します。 |
getTrashedFiles() | FileIterator | ユーザーのドライブのゴミ箱内にあるすべてのファイルのコレクションを取得します。 |
getTrashedFolders() | FolderIterator | ユーザーのドライブのゴミ箱にあるすべてのフォルダのコレクションを取得します。 |
searchFiles(params) | FileIterator | 指定された検索条件に一致するユーザーのドライブ内のすべてのファイルのコレクションを取得します。 |
searchFolders(params) | FolderIterator | 指定した検索条件に一致するユーザーのドライブ内のすべてのフォルダのコレクションを取得します。 |
詳細なドキュメント
continueFileIterator(continuationToken)
前のイテレータの継続トークンを使用してファイルの反復処理を再開します。このメソッドは、1 回の実行でイテレータを処理するときに最大実行時間を超える場合に便利です。通常、継続トークンは 1 週間有効です。
パラメータ
名前 | 型 | 説明 |
---|---|---|
continuationToken | String | 前のファイル イテレータの継続トークン。 |
復路
FileIterator
- 継続トークンの生成時に前のイテレータに残ったファイルのコレクション。
continueFolderIterator(continuationToken)
前のイテレータの継続トークンを使用してフォルダの反復処理を再開します。このメソッドは、1 回の実行でイテレータを処理するときに最大実行時間を超える場合に便利です。通常、継続トークンは 1 週間有効です。
パラメータ
名前 | 型 | 説明 |
---|---|---|
continuationToken | String | 前のフォルダ イテレータの継続トークン。 |
復路
FolderIterator
- 継続トークンの生成時に前のイテレータに残ったフォルダのコレクション。
createFile(blob)
任意のデータの指定された Blob
から、ユーザーのドライブのルートにファイルを作成します。
// Create an image file in Google Drive using the Maps service. var blob = Maps.newStaticMap().setCenter('76 9th Avenue, New York NY').getBlob(); DriveApp.createFile(blob);
パラメータ
名前 | 型 | 説明 |
---|---|---|
blob | BlobSource | 新しいファイルのデータ。 |
復路
File
- 新しいファイル。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive
createFile(name, content)
ユーザーのドライブのルートに、指定した名前とコンテンツでテキスト ファイルを作成します。content
が 50 MB を超えると、例外をスローします。
// Create a text file with the content "Hello, world!" DriveApp.createFile('New Text File', 'Hello, world!');
パラメータ
名前 | 型 | 説明 |
---|---|---|
name | String | 新しいファイルの名前。 |
content | String | 新しいファイルのコンテンツ。 |
復路
File
- 新しいファイル。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive
createFile(name, content, mimeType)
ユーザーのドライブのルートに、指定した名前、内容、MIME タイプのファイルを作成します。content
が 10 MB を超えると、例外をスローします。
// Create an HTML file with the content "Hello, world!" DriveApp.createFile('New HTML File', '<b>Hello, world!</b>', MimeType.HTML);
パラメータ
名前 | 型 | 説明 |
---|---|---|
name | String | 新しいファイルの名前。 |
content | String | 新しいファイルのコンテンツ。 |
mimeType | String | 新しいファイルの MIME タイプ。 |
復路
File
- 新しいファイル。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive
createFolder(name)
ユーザーのドライブのルートに、指定した名前のフォルダを作成します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
name | String | 新しいフォルダの名前。 |
復路
Folder
- 新しいフォルダ。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive
createShortcut(targetId)
指定したドライブ アイテム ID へのショートカットを作成して返します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
targetId | String | ターゲット ファイルまたはフォルダのファイル ID。 |
復路
File
- 新しいショートカット。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive
createShortcutForTargetIdAndResourceKey(targetId, targetResourceKey)
指定したドライブのアイテム ID とリソースキーへのショートカットを作成して返します。リソースキーは、リンクを使用して共有されたターゲット ファイルまたはフォルダにアクセスするために渡す必要がある追加のパラメータです。
パラメータ
名前 | 型 | 説明 |
---|---|---|
targetId | String | ターゲット ファイルまたはフォルダの ID。 |
targetResourceKey | String | ターゲット ファイルまたはフォルダのリソースキー。 |
復路
File
- 新しいショートカット。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive
enforceSingleParent(value)
アイテムの親に影響するすべての呼び出しに対する enforcedSingleParent の動作を有効または無効にします。
詳しくは、 Google ドライブのフォルダ構造の簡素化とモデルの共有に関するブログをご覧ください。
パラメータ
名前 | 型 | 説明 |
---|---|---|
value | Boolean | enforcedSingleParent フラグの新しい状態。 |
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive
getFileById(id)
指定された ID のファイルを取得します。ファイルが存在しないか、ユーザーにファイルへのアクセス権限がない場合は、スクリプト例外をスローします。
// Gets a list of all files in Google Drive with the given name. // TODO(developer): Replace 'Test' with your file name. const files = DriveApp.getFilesByName('Test'); if (files.hasNext()) { // Gets the ID of each file in the list. const fileId = files.next().getId(); // Gets the file name using its ID and logs it to the console. console.log(DriveApp.getFileById(fileId).getName()); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
id | String | ファイルの ID。 |
復路
File
- 指定された ID を持つファイル。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFileByIdAndResourceKey(id, resourceKey)
指定された ID とリソースキーでファイルを取得します。リソースキーは、リンクを使用して共有されたファイルにアクセスするために渡す必要がある追加のパラメータです。
ファイルが存在しないか、ユーザーにファイルへのアクセス権限がない場合は、スクリプト例外をスローします。
// Gets a list of all files in Drive with the given name. // TODO(developer): Replace 'Test' with your file name. const files = DriveApp.getFilesByName('Test'); if (files.hasNext()) { // Gets the first file in the list. const file = files.next(); // Gets the ID and resource key. const key = file.getResourceKey(); const id = file.getId(); // Logs the file name to the console using its ID and resource key. console.log(DriveApp.getFileByIdAndResourceKey(id, key).getName()); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
id | String | ファイルの ID。 |
resourceKey | String | フォルダのリソースキー。 |
復路
File
- 指定された ID を持つファイル。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFiles()
ユーザーのドライブ内のすべてのファイルのコレクションを取得します。
復路
FileIterator
- ユーザーのドライブにあるすべてのファイルのコレクション。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFilesByName(name)
ユーザーのドライブ内にある、指定した名前を持つすべてのファイルのコレクションを取得します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
name | String | 検索するファイルの名前。 |
復路
FileIterator
- 指定した名前のユーザーのドライブ内にあるすべてのファイルのコレクション。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFilesByType(mimeType)
指定した MIME タイプのユーザーのドライブ内にあるすべてのファイルのコレクションを取得します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
mimeType | String | 検索するファイルの MIME タイプ。 |
復路
FileIterator
- 指定された MIME タイプでユーザーのドライブ内にあるすべてのファイルのコレクション。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFolderById(id)
指定された ID のフォルダを取得します。フォルダが存在しない場合、またはユーザーにフォルダへのアクセス権限がない場合は、スクリプト例外をスローします。
パラメータ
名前 | 型 | 説明 |
---|---|---|
id | String | フォルダの ID。 |
復路
Folder
- 指定された ID のフォルダ。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFolderByIdAndResourceKey(id, resourceKey)
指定された ID とリソースキーを持つフォルダを取得します。リソースキーは、リンクを使用して共有フォルダにアクセスするために渡す必要がある追加のパラメータです。
フォルダが存在しない場合、またはユーザーにフォルダへのアクセス権限がない場合は、スクリプト例外をスローします。
パラメータ
名前 | 型 | 説明 |
---|---|---|
id | String | フォルダの ID。 |
resourceKey | String | フォルダのリソースキー。 |
復路
Folder
- 指定された ID のフォルダ。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFolders()
ユーザーのドライブ内のすべてのフォルダのコレクションを取得します。
復路
FolderIterator
- ユーザーのドライブ内のすべてのフォルダのコレクション。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getFoldersByName(name)
ユーザーのドライブ内にある、指定した名前を持つすべてのフォルダのコレクションを取得します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
name | String | 検索するフォルダの名前。 |
復路
FolderIterator
- 指定した名前のユーザーのドライブ内にあるすべてのフォルダのコレクション。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getRootFolder()
ユーザーのドライブのルートにあるフォルダを取得します。
// Gets the user's My Drive folder and logs its name to the console. console.log(DriveApp.getRootFolder().getName()); // Logs the Drive owner's name to the console. console.log(DriveApp.getRootFolder().getOwner().getName());
復路
Folder
- ユーザーのドライブのルートフォルダ。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getStorageLimit()
ユーザーがドライブに保存できるバイト数を取得します。
// Gets the number of bytes the user can store in Drive and logs it to the console. console.log(DriveApp.getStorageLimit());
復路
Integer
- ドライブにユーザーが保存できるバイト数。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getStorageUsed()
ユーザーが現在ドライブに保存しているバイト数を取得します。
// Gets the number of bytes the user is currently storing in Drive and logs it to the console. console.log(DriveApp.getStorageUsed());
復路
Integer
- ユーザーが現在ドライブに保存しているバイト数。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getTrashedFiles()
ユーザーのドライブのゴミ箱内にあるすべてのファイルのコレクションを取得します。
// Gets a list of all the files in the trash of the user's Drive. const trashFiles = DriveApp.getTrashedFiles(); // Logs the trash file names to the console. while (trashFiles.hasNext()) { const file = trashFiles.next(); console.log(file.getName()); }
復路
FileIterator
- ゴミ箱内のファイルのコレクション。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
getTrashedFolders()
ユーザーのドライブのゴミ箱にあるすべてのフォルダのコレクションを取得します。
// Gets a collection of all the folders in the trash of the user's Drive. const trashFolders = DriveApp.getTrashedFolders(); // Logs the trash folder names to the console. while (trashFolders.hasNext()) { const folder = trashFolders.next(); console.log(folder.getName()); }
復路
FolderIterator
- ゴミ箱内のフォルダのコレクション。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
searchFiles(params)
指定された検索条件に一致するユーザーのドライブ内のすべてのファイルのコレクションを取得します。検索条件について詳しくは、Google ドライブ SDK のドキュメントをご覧ください。ドライブ サービスは Drive API の v2 を使用するため、一部のクエリ フィールドは v3 とは異なることに注意してください。v2 と v3 のフィールドの違いを確認します。
params
引数は、文字列値を格納できるクエリ文字列なので、引用符を正しくエスケープする必要があります(例: "title contains 'Gulliver\\'s
Travels'"
、'title contains "Gulliver\'s Travels"'
)。
// Logs the name of every file in the user's Drive that modified after February 28, // 2022 whose name contains "untitled."" var files = DriveApp.searchFiles( 'modifiedDate > "2022-02-28" and title contains "untitled"'); while (files.hasNext()) { var file = files.next(); console.log(file.getName()); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
params | String | 検索条件。詳細については、Google ドライブ SDK のドキュメントをご覧ください。 |
復路
FileIterator
- 検索条件と一致するユーザーのドライブ内のすべてのファイルのコレクション。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive
searchFolders(params)
指定した検索条件に一致するユーザーのドライブ内のすべてのフォルダのコレクションを取得します。検索条件について詳しくは、Google ドライブ SDK のドキュメントをご覧ください。ドライブ サービスは Drive API の v2 を使用するため、一部のクエリ フィールドは v3 とは異なることに注意してください。v2 と v3 のフィールドの違いを確認します。
params
引数は、文字列値を格納できるクエリ文字列なので、引用符を正しくエスケープする必要があります(例: "title contains 'Gulliver\\'s
Travels'"
、'title contains "Gulliver\'s Travels"'
)。
// Logs the name of every folder in the user's Drive that you own and is starred. var folders = DriveApp.searchFolders('starred = true and "me" in owners'); while (folders.hasNext()) { var folder = folders.next(); console.log(folder.getName()); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
params | String | 検索条件。詳細については、Google ドライブ SDK のドキュメントをご覧ください。 |
復路
FolderIterator
- 検索条件と一致するユーザーのドライブ内のすべてのフォルダのコレクション。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープで承認が必要です。
-
https://www.googleapis.com/auth/drive.readonly
-
https://www.googleapis.com/auth/drive