Class DriveApp

DriveApp

スクリプトを使用して 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());
}

プロパティ

プロパティ種類説明
AccessAccessファイルまたはフォルダにアクセスできるユーザーのクラスと、明示的にアクセス権が付与されている個々のユーザーのクラスを表す列挙型。
PermissionPermissionアクセス権を明示的に付与された個々のユーザーに加え、ファイルやフォルダにアクセスできるユーザーに付与される権限を表す列挙型。

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アイテムの親に影響するすべての呼び出しに対する enforcingSingleParent の動作を有効または無効にします。
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 週間です。

// Continues getting a list of all 'Untitled document' files in the user's Drive.
// Creates a file iterator named 'previousIterator'.
const previousIterator = DriveApp.getFilesByName('Untitled document');

// Gets continuation token from the previous file iterator.
const continuationToken = previousIterator.getContinuationToken();

// Creates a new iterator using the continuation token from the previous file iterator.
const newIterator = DriveApp.continueFileIterator(continuationToken);

// Resumes the file iteration using a continuation token from 'firstIterator' and
// logs the file name.
if (newIterator.hasNext()) {
  const file = newIterator.next();
  console.log(file.getName());
}

パラメータ

名前説明
continuationTokenString前のファイル イテレータからの継続トークン。

リターン

FileIterator - 継続トークンの生成時に前のイテレータに残っていたファイルのコレクション。


continueFolderIterator(continuationToken)

前のイテレータの継続トークンを使用して、フォルダの反復処理を再開します。このメソッドは、1 回の実行でイテレータの処理が最大実行時間を超える場合に便利です。通常、継続トークンの有効期限は 1 週間です。

// Continues getting a list of all folders in user's Drive.
// Creates a folder iterator named 'previousIterator'.
const previousIterator = DriveApp.getFolders();

// Gets continuation token from the previous folder iterator.
const continuationToken = previousIterator.getContinuationToken();

// Creates a new iterator using the continuation token from the previous folder iterator.
const newIterator = DriveApp.continueFolderIterator(continuationToken);

// Resumes the folder iteration using a continuation token from the previous iterator and logs
// the folder name.
if (newIterator.hasNext()) {
  const folder = newIterator.next();
  console.log(folder.getName());
}

パラメータ

名前説明
continuationTokenString前のフォルダ イテレータの継続トークン。

リターン

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

パラメータ

名前説明
blobBlobSource新しいファイルのデータ。

リターン

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!');

パラメータ

名前説明
nameString新しいファイルの名前。
contentString新しいファイルの内容。

リターン

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

パラメータ

名前説明
nameString新しいファイルの名前。
contentString新しいファイルの内容。
mimeTypeString新しいファイルの MIME タイプ。

リターン

File - 新しいファイル。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/drive

createFolder(name)

指定した名前で、ユーザーのドライブのルートにフォルダを作成します。

パラメータ

名前説明
nameString新しいフォルダの名前。

リターン

Folder - 新しいフォルダ。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/drive

createShortcut(targetId)

指定されたドライブのアイテム ID へのショートカットを作成して返します。

パラメータ

名前説明
targetIdStringターゲットのファイルまたはフォルダのファイル ID。

リターン

File - 新しいショートカット。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/drive

createShortcutForTargetIdAndResourceKey(targetId, targetResourceKey)

指定されたドライブのアイテム ID とリソースキーへのショートカットを作成して返します。リソースキーは、リンクを使用して共有されたターゲット ファイルまたはフォルダにアクセスするために渡す必要がある追加のパラメータです。

// Creates shortcuts for all folders in the user's drive that have a specific name.
// TODO(developer): Replace 'Test-Folder' with a valid folder name in your drive.
const folders = DriveApp.getFoldersByName('Test-Folder');

// Iterates through all folders named 'Test-Folder'.
while (folders.hasNext()) {
  const folder = folders.next();

  // Creates a shortcut to the provided Drive item ID and resource key, and returns it.
  DriveApp.createShortcutForTargetIdAndResourceKey(folder.getId(), folder.getResourceKey());
}

パラメータ

名前説明
targetIdStringターゲット ファイルまたはフォルダの ID。
targetResourceKeyStringターゲット ファイルまたはフォルダのリソースキー。

リターン

File - 新しいショートカット。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/drive

enforceSingleParent(value)

アイテムの親に影響するすべての呼び出しに対する enforcingSingleParent の動作を有効または無効にします。

詳しくは、ブログ「 Google ドライブのフォルダ構造と共有モデルを簡素化」をご覧ください。

// Enables enforceSingleParent behavior for all calls affecting item parents.
DriveApp.enforceSingleParent(true);

パラメータ

名前説明
valueBooleanenforcingSingleParent フラグの新しい状態。

承認

この方法を使用するスクリプトには、次の 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());
}

パラメータ

名前説明
idStringファイルの 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());
}

パラメータ

名前説明
idStringファイルの ID。
resourceKeyStringフォルダのリソースキー。

リターン

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)

ユーザーのドライブ内にある、指定した名前を持つすべてのファイルのコレクションを取得します。

パラメータ

名前説明
nameString検索するファイルの名前。

リターン

FileIterator - ユーザーのドライブ内にある、指定した名前を持つすべてのファイルのコレクション。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFilesByType(mimeType)

ユーザーのドライブ内にある、指定された MIME タイプのすべてのファイルのコレクションを取得します。

パラメータ

名前説明
mimeTypeString検索するファイルの MIME タイプ。

リターン

FileIterator - ユーザーのドライブ内にある、指定された MIME タイプのすべてのファイルのコレクション。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFolderById(id)

指定された ID のフォルダを取得します。フォルダが存在しない場合、またはユーザーにフォルダへのアクセス権限がない場合は、スクリプト例外をスローします。

パラメータ

名前説明
idStringフォルダの ID。

リターン

Folder - 指定された ID のフォルダ。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

getFolderByIdAndResourceKey(id, resourceKey)

指定された ID とリソースキーを持つフォルダを取得します。リソースキーは、リンクを使用して共有されているフォルダにアクセスするために渡す必要がある追加のパラメータです。

フォルダが存在しない場合、またはユーザーがフォルダへのアクセス権限を持っていない場合は、スクリプト例外をスローします。

パラメータ

名前説明
idStringフォルダの ID。
resourceKeyStringフォルダのリソースキー。

リターン

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)

ユーザーのドライブ内にある、指定した名前を持つすべてのフォルダのコレクションを取得します。

パラメータ

名前説明
nameString検索するフォルダの名前。

リターン

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

パラメータ

名前説明
paramsString検索条件。詳細は 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());
}

パラメータ

名前説明
paramsString検索条件。詳細は Google ドライブ SDK のドキュメントをご覧ください。

リターン

FolderIterator - ユーザーのドライブ内にある、検索条件に一致するすべてのフォルダのコレクション。

承認

この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。

  • https://www.googleapis.com/auth/drive.readonly
  • https://www.googleapis.com/auth/drive

サポート終了のメソッド