Class File

ファイル

Google ドライブ内のファイル。ファイルは DriveApp からアクセスまたは作成できます。

// Trash every untitled spreadsheet that hasn't been updated in a week.
const files = DriveApp.getFilesByName('Untitled spreadsheet');
while (files.hasNext()) {
  const file = files.next();
  if (new Date() - file.getLastUpdated() > 7 * 24 * 60 * 60 * 1000) {
    file.setTrashed(true);
  }
}

メソッド

メソッド戻り値の型概要
addCommenter(emailAddress)File指定されたユーザーを File のコメント投稿者のリストに追加します。
addCommenter(user)File指定されたユーザーを File のコメント投稿者のリストに追加します。
addCommenters(emailAddresses)File指定されたユーザーの配列を File のコメント投稿者のリストに追加します。
addEditor(emailAddress)File指定されたユーザーを File の編集者のリストに追加します。
addEditor(user)File指定されたユーザーを File の編集者のリストに追加します。
addEditors(emailAddresses)File指定されたユーザーの配列を File の編集者のリストに追加します。
addViewer(emailAddress)File指定されたユーザーを File の閲覧者リストに追加します。
addViewer(user)File指定されたユーザーを File の閲覧者リストに追加します。
addViewers(emailAddresses)File指定されたユーザーの配列を File の視聴者リストに追加します。
getAccess(email)Permission特定のユーザーに付与された権限を取得します。
getAccess(user)Permission特定のユーザーに付与された権限を取得します。
getAs(contentType)Blobこのオブジェクト内のデータを、指定されたコンテンツ タイプに変換された blob として返します。
getBlob()Blobこのオブジェクト内のデータを blob として返します。
getDateCreated()DateFile が作成された日付を取得します。
getDescription()StringFile の説明を取得します。
getDownloadUrl()Stringファイルのダウンロードに使用できる URL を取得します。
getEditors()User[]この File の編集者のリストを取得します。
getId()StringFile の ID を取得します。
getLastUpdated()DateFile が最後に更新された日付を取得します。
getMimeType()Stringファイルの MIME タイプを取得します。
getName()StringFile の名前を取得します。
getOwner()Userファイルのオーナーを取得します。
getParents()FolderIteratorFile の直近の親であるフォルダのコレクションを取得します。
getResourceKey()Stringリンクを使用して共有されたアイテムにアクセスするために必要な File のリソースキーを取得します。
getSecurityUpdateEligible()Booleanこの File が、リンクを使用して共有されたときにアクセスにリソースキーを必要とするセキュリティ アップデートを適用できるかどうかを取得します。
getSecurityUpdateEnabled()Booleanこの File がリンクを使用して共有されるときに、アクセスにリソースキーが必要かどうかを取得します。
getSharingAccess()Access明示的にアクセス権が付与された個々のユーザーを除き、どのクラスのユーザーが File にアクセスできるかを取得します。
getSharingPermission()PermissionFile にアクセスできるユーザーに付与された権限を取得します。明示的にアクセス権が付与された個々のユーザーは除きます。
getSize()Integerドライブに File を保存するために使用されるバイト数を取得します。
getTargetId()Stringこれがショートカットの場合、指しているアイテムの ID を返します。
getTargetMimeType()Stringこれがショートカットの場合、参照先のアイテムの MIME タイプを返します。
getTargetResourceKey()Stringファイルがショートカットの場合、そのショートカットが指すアイテムのリソースキーを返します。
getThumbnail()Blob|nullファイルのサムネイル画像を取得します。サムネイルが存在しない場合は null を返します。
getUrl()Stringドライブやドキュメントなどの Google アプリで File を開くために使用できる URL を取得します。
getViewers()User[]この File の閲覧者とコメント投稿者のリストを取得します。
isShareableByEditors()BooleanFile の編集権限を持つユーザーが、他のユーザーと共有したり、権限を変更したりできるかどうかを指定します。
isStarred()BooleanFile がユーザーのドライブでスター付きになっているかどうかを判断します。
isTrashed()BooleanFile がユーザーのドライブのゴミ箱にあるかどうかを判断します。
makeCopy()Fileファイルのコピーを作成します。
makeCopy(destination)File移動先のディレクトリにファイルのコピーを作成します。
makeCopy(name)Fileファイルのコピーを作成し、指定された名前を付けます。
makeCopy(name, destination)File宛先ディレクトリにファイルのコピーを作成し、指定された名前を付けます。
moveTo(destination)Fileこのアイテムを指定された移動先フォルダに移動します。
removeCommenter(emailAddress)File指定されたユーザーを File のコメント投稿者リストから削除します。
removeCommenter(user)File指定されたユーザーを File のコメント投稿者リストから削除します。
removeEditor(emailAddress)File指定されたユーザーを File の編集者リストから削除します。
removeEditor(user)File指定されたユーザーを File の編集者リストから削除します。
removeViewer(emailAddress)File指定されたユーザーを File の閲覧者とコメント投稿者のリストから削除します。
removeViewer(user)File指定されたユーザーを File の閲覧者とコメント投稿者のリストから削除します。
revokePermissions(emailAddress)File指定されたユーザーに付与された File へのアクセス権を取り消します。
revokePermissions(user)File指定されたユーザーに付与された File へのアクセス権を取り消します。
setContent(content)Fileファイルの内容を指定された置換で上書きします。
setDescription(description)FileFile の説明を設定します。
setName(name)FileFile の名前を設定します。
setOwner(emailAddress)FileFile のオーナーを変更します。
setOwner(user)FileFile のオーナーを変更します。
setSecurityUpdateEnabled(enabled)Fileリンクを使用して共有するときに、File がアクセスにリソースキーを必要とするかどうかを設定します。
setShareableByEditors(shareable)FileFile の編集権限を持つユーザーが他のユーザーと共有したり、権限を変更したりすることを許可するかどうかを設定します。
setSharing(accessType, permissionType)FileFile にアクセスできるユーザーのクラスと、明示的にアクセス権が付与された個々のユーザーに加えて、それらのユーザーに付与される権限を設定します。
setStarred(starred)Fileユーザーのドライブで File にスターを付けるかどうかを設定します。
setTrashed(trashed)FileFile がユーザーのドライブのゴミ箱にあるかどうかを設定します。

詳細なドキュメント

addCommenter(emailAddress)

指定されたユーザーを File のコメント投稿者のリストに追加します。ユーザーが視聴者リストにすでに登録されている場合、このメソッドはユーザーを視聴者リストから削除します。

const files = DriveApp.getFilesByName('Test');

// Loops through the files
while (files.hasNext()) {
  const file = files.next();
  file.addCommenter('hello@example.com');
}

パラメータ

名前説明
emailAddressString追加するユーザーのメールアドレス。

戻る

File - チェーン用の File

承認

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

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

addCommenter(user)

指定されたユーザーを File のコメント投稿者のリストに追加します。ユーザーが視聴者リストにすでに登録されている場合、このメソッドはユーザーを視聴者リストから削除します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Adds the active user as a commenter.
while (files.hasNext()) {
  const file = files.next();
  file.addCommenter(Session.getActiveUser());
}

パラメータ

名前説明
userUser追加するユーザーの表現。

戻る

File - チェーン用の File

承認

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

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

addCommenters(emailAddresses)

指定されたユーザーの配列を File のコメント投稿者のリストに追加します。ユーザーが視聴者リストにすでに含まれている場合、このメソッドは視聴者リストからそのユーザーを削除します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

while (files.hasNext()) {
  const file = files.next();
  // TODO(developer): Replace 'cloudysanfrancisco@gmail.com' and
  // 'baklavainthebalkans@gmail.com' with the email addresses to add as
  // commenters.
  const emails = [
    'cloudysanfrancisco@gmail.com',
    'baklavainthebalkans@gmail.com',
  ];
  console.log(file.addCommenters(emails));
}

パラメータ

名前説明
emailAddressesString[]追加するユーザーのメールアドレスの配列。

戻る

File - チェーン用の File

承認

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

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

addEditor(emailAddress)

指定されたユーザーを File の編集者のリストに追加します。ユーザーがすでに視聴者リストに登録されている場合、このメソッドはユーザーを視聴者リストから昇格させます。

パラメータ

名前説明
emailAddressString追加するユーザーのメールアドレス。

戻る

File - チェーン用の File

承認

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

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

addEditor(user)

指定されたユーザーを File の編集者のリストに追加します。ユーザーがすでに視聴者リストに登録されている場合、このメソッドはユーザーを視聴者リストから昇格させます。

パラメータ

名前説明
userUser追加するユーザーの表現。

戻る

File - チェーン用の File

承認

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

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

addEditors(emailAddresses)

指定されたユーザーの配列を File の編集者のリストに追加します。ユーザーが視聴者リストにすでに含まれている場合、このメソッドは視聴者リストからそのユーザーを昇格させます。

パラメータ

名前説明
emailAddressesString[]追加するユーザーのメールアドレスの配列。

戻る

File - チェーン用の File

承認

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

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

addViewer(emailAddress)

指定されたユーザーを File の閲覧者リストに追加します。ユーザーがすでに編集者のリストに登録されている場合、このメソッドは効力がありません。

パラメータ

名前説明
emailAddressString追加するユーザーのメールアドレス。

戻る

File - チェーン用の File

承認

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

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

addViewer(user)

指定されたユーザーを File の閲覧者リストに追加します。ユーザーがすでに編集者のリストに登録されている場合、このメソッドは効力がありません。

パラメータ

名前説明
userUser追加するユーザーの表現。

戻る

File - チェーン用の File

承認

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

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

addViewers(emailAddresses)

指定されたユーザーの配列を File の視聴者リストに追加します。ユーザーがすでに編集者のリストに含まれている場合、このメソッドは効果がありません。

パラメータ

名前説明
emailAddressesString[]追加するユーザーのメールアドレスの配列。

戻る

File - チェーン用の File

承認

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

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

getAccess(email)

特定のユーザーに付与された権限を取得します。このメソッドは、Google グループの権限や、Google グループを通じて継承された権限を返すことをサポートしていません。

パラメータ

名前説明
emailString権限を確認するユーザーのメールアドレス。Google グループはサポートされていません。

戻る

Permission - ユーザーに付与された権限。

承認

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

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

getAccess(user)

特定のユーザーに付与された権限を取得します。このメソッドは、Google グループの権限や、Google グループを通じて継承された権限を返すことをサポートしていません。

パラメータ

名前説明
userUser権限を確認するユーザーの表現。

戻る

Permission - ユーザーに付与された権限。

承認

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

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

getAs(contentType)

このオブジェクト内のデータを、指定されたコンテンツ タイプに変換された blob として返します。このメソッドは、ファイル名に適切な拡張子(例: 「myfile.pdf」)を追加します。ただし、最後のピリオドの後に続くファイル名の一部(存在する場合)は、置き換える必要がある既存の拡張子であると想定されます。その結果、「ShoppingList.12.25.2014」は「ShoppingList.12.25.pdf」になります。

コンバージョンの 1 日あたりの割り当てを確認するには、Google サービスの割り当てをご覧ください。新しく作成された Google Workspace ドメインには、一時的に厳しい割り当てが適用されることがあります。

パラメータ

名前説明
contentTypeString変換先の MIME タイプ。ほとんどの BLOB では、'application/pdf' が唯一の有効なオプションです。BMP、GIF、JPEG、PNG 形式の画像の場合、'image/bmp''image/gif''image/jpeg''image/png' のいずれも有効です。Google ドキュメントの場合、'text/markdown' も有効です。

戻る

Blob - blob としてのデータ。


getBlob()

このオブジェクト内のデータを blob として返します。

戻る

Blob - blob としてのデータ。


getDateCreated()

File が作成された日付を取得します。

戻る

Date - File が作成された日付

承認

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

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

getDescription()

File の説明を取得します。

戻る

String - File の説明

承認

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

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

getDownloadUrl()

ファイルのダウンロードに使用できる URL を取得します。Google ドライブでファイルを開く権限を持つユーザーのみが URL にアクセスできます。この URL はブラウザでファイル ダウンロードに使用できますが、UrlFetchApp でファイルを取得するために使用することはできません。スクリプトでファイルの内容が必要な場合は、getBlob() を使用します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files and logs the download URLs to the console.
while (files.hasNext()) {
  const file = files.next();
  console.log(file.getDownloadUrl());
}

戻る

String - ファイルのダウンロードに使用できる URL。

承認

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

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

getEditors()

この File の編集者のリストを取得します。スクリプトを実行するユーザーに File の編集権限がない場合、このメソッドは空の配列を返します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files.
while (files.hasNext()) {
  const file = files.next();

  // Adds the email addresses in the array as editors of each file.
  // TODO(developer): Replace 'cloudysanfrancisco@gmail.com'
  // and 'baklavainthebalkans@gmail.com' with valid email addresses.
  file.addEditors([
    'cloudysanfrancisco@gmail.com',
    'baklavainthebalkans@gmail.com',
  ]);

  // Gets a list of the file editors.
  const editors = file.getEditors();

  // For each file, logs the editors' email addresses to the console.
  for (const editor of editors) {
    console.log(editor.getEmail());
  }
}

戻る

User[] - ユーザーがこの File の編集権限を持っている場合、編集者のリストを返します。ユーザーに編集権限がない場合は、空の配列を返します。

承認

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

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

getId()

File の ID を取得します。

戻る

String - File の ID

承認

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

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

getLastUpdated()

File が最後に更新された日付を取得します。

戻る

Date - File が最後に更新された日付

承認

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

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

getMimeType()

ファイルの MIME タイプを取得します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files and logs the MIME type to the console.
while (files.hasNext()) {
  const file = files.next();
  console.log(file.getMimeType());
}

戻る

String - ファイルの MIME タイプ。

承認

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

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

getName()

File の名前を取得します。

戻る

String - File の名前

承認

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

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

getOwner()

ファイルのオーナーを取得します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files and logs the names of the file owners to the console.
while (files.hasNext()) {
  const file = files.next();
  console.log(file.getOwner().getName());
}

戻る

User - ファイルのオーナー。

承認

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

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

getParents()

File の直近の親であるフォルダのコレクションを取得します。

戻る

FolderIterator - File の直近の親であるフォルダのコレクション

承認

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

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

getResourceKey()

リンクを使用して共有されたアイテムにアクセスするために必要な File のリソースキーを取得します。

戻る

String - File のリソースキー。

承認

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

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

getSecurityUpdateEligible()

この File が、リンクを使用して共有されたときにアクセスにリソースキーを必要とするセキュリティ アップデートを適用できるかどうかを取得します。

リンクを使用して共有された一部のファイルやフォルダにアクセスするには、ドライブでリソースキーが必要です。この変更は、セキュリティ アップデートの一部です。この更新は、対象となるファイルとフォルダに対してデフォルトで有効になっています。対象となるファイルのリソースキー要件をオンまたはオフにするには、setSecurityUpdateEnabled を使用します。

詳しくは、Google ドライブのセキュリティ アップデートをご覧ください。

戻る

Boolean - リソースキーの要件を File に適用できるかどうか。

承認

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

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

getSecurityUpdateEnabled()

この File がリンクを使用して共有されるときに、アクセスにリソースキーが必要かどうかを取得します。この要件は、対象となるファイルとフォルダに対してデフォルトで有効になっています。対象となるファイルのリソースキー要件をオンまたはオフにするには、 setSecurityUpdateEnabled を使用します。

詳しくは、Google ドライブのセキュリティ アップデートをご覧ください。

戻る

Boolean - この File でリソースキーの要件が有効かどうか。

承認

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

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

getSharingAccess()

明示的にアクセス権が付与された個々のユーザーを除き、どのクラスのユーザーが File にアクセスできるかを取得します。

戻る

Access - File にアクセスできるユーザーのクラス

承認

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

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

getSharingPermission()

File にアクセスできるユーザーに付与された権限を取得します。明示的にアクセス権が付与された個々のユーザーは除きます。

戻る

Permission - File にアクセスできるユーザーに付与される権限

承認

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

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

getSize()

ドライブに File を保存するために使用されるバイト数を取得します。Google Workspace アプリケーション ファイルはドライブの保存容量の上限にカウントされないため、0 バイトが返されます。

戻る

Integer - ドライブに File を保存するために使用されるバイト数

承認

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

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

getTargetId()

これがショートカットの場合、指しているアイテムの ID を返します。

それ以外の場合は null を返します。

// The ID of the file for which to make a shortcut and the ID of
// the folder to which you want to add the shortcut.
// TODO(developer): Replace the file and folder IDs with your IDs.
const fileId = 'abc123456';
const folderId = 'xyz987654';

// Gets the folder to add the shortcut to.
const folder = DriveApp.getFolderById(folderId);

// Creates a shortcut of the file and moves it to the specified folder.
const shortcut = DriveApp.createShortcut(fileId).moveTo(folder);

// Logs the target ID of the shortcut.
console.log(`${shortcut.getName()}=${shortcut.getTargetId()}`);

戻る

String - ターゲット アイテムの ID。


getTargetMimeType()

これがショートカットの場合、参照先のアイテムの MIME タイプを返します。

それ以外の場合は null を返します。

// The ID of the file for which to make a shortcut and the ID of
// the folder to which you want to add the shortcut.
// TODO(developer): Replace the file and folder IDs with your IDs.
const fileId = 'abc123456';
const folderId = 'xyz987654';

// Gets the folder to add the shortcut to.
const folder = DriveApp.getFolderById(folderId);

// Creates a shortcut of the file and moves it to the specified folder.
const shortcut = DriveApp.createShortcut(fileId).moveTo(folder);

// Logs the MIME type of the file that the shortcut points to.
console.log(`MIME type of the shortcut: ${shortcut.getTargetMimeType()}`);

戻る

String - ターゲット アイテムの MIME タイプ。


getTargetResourceKey()

ファイルがショートカットの場合、そのファイルが指すアイテムのリソースキーを返します。リソースキーは、リンクを使用して共有されたファイルにアクセスするために渡す必要がある追加のパラメータです。

ファイルがショートカットでない場合は、null を返します。

//  Gets a file by its ID.
//  TODO(developer): Replace 'abc123456' with your file ID.
const file = DriveApp.getFileById('abc123456');

// If the file is a shortcut, returns the resource key of the file that it
// points to.
console.log(file.getTargetResourceKey());

戻る

String - ターゲット アイテムのリソースキー。ファイルがショートカットでない場合は null

承認

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

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

getThumbnail()

ファイルのサムネイル画像を取得します。サムネイルが存在しない場合は null を返します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files.
while (files.hasNext()) {
  const file = files.next();

  // Logs the thumbnail image for each file to the console as a blob,
  // or null if no thumbnail exists.
  console.log(file.getThumbnail());
}

戻る

Blob|null - ファイルのサムネイル画像。

承認

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

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

getUrl()

ドライブやドキュメントなどの Google アプリで File を開くために使用できる URL を取得します。

戻る

String - ドライブやドキュメントなどの Google アプリでこの File を表示するために使用できる URL

承認

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

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

getViewers()

この File の閲覧者とコメント投稿者のリストを取得します。スクリプトを実行するユーザーに File の編集権限がない場合、このメソッドは空の配列を返します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files.
while (files.hasNext()) {
  const file = files.next();

  // For each file, logs the viewers' email addresses to the console.
  const viewers = file.getViewers();
  for (const viewer of viewers) {
    console.log(viewer.getEmail());
  }
}

戻る

User[] - ユーザーがこの File の編集権限を持っている場合、閲覧者とコメント投稿者のリストを返します。ユーザーに編集権限がない場合は、空の配列を返します。

承認

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

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

isShareableByEditors()

File の編集権限を持つユーザーが、他のユーザーと共有したり、権限を変更したりできるかどうかを指定します。

戻る

Boolean - 編集権限を持つユーザーが他のユーザーと共有したり、権限を変更したりできる場合は true、できない場合は false

承認

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

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

isStarred()

File がユーザーのドライブでスター付きになっているかどうかを判断します。

戻る

Boolean - File がユーザーのドライブでスター付きの場合は true、そうでない場合は false

承認

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

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

isTrashed()

File がユーザーのドライブのゴミ箱にあるかどうかを判断します。

戻る

Boolean - File がユーザーのドライブのゴミ箱にある場合は true、そうでない場合は false

承認

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

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

makeCopy()

ファイルのコピーを作成します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files.
while (files.hasNext()) {
  const file = files.next();

  // Creates a copy of each file and logs the file name to the console.
  console.log(file.makeCopy().getName());
}

戻る

File - 新しいコピー。

承認

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

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

makeCopy(destination)

宛先ディレクトリにファイルのコピーを作成します。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files.
while (files.hasNext()) {
  const file = files.next();

  // Creates a copy of each file and adds it to the specified folder.
  // TODO(developer): Replace the folder ID with your own.
  const destination = DriveApp.getFolderById('123456abcxyz');
  const copiedFile = file.makeCopy(destination);

  // Logs the file names to the console.
  console.log(copiedFile.getName());
}

パラメータ

名前説明
destinationFolderファイルのコピー先のディレクトリ。

戻る

File - 新しいコピー。

承認

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

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

makeCopy(name)

ファイルのコピーを作成し、指定された名前を付けます。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files.
while (files.hasNext()) {
  const file = files.next();

  // Creates a copy of each file and sets the name to 'Test-Copy.'
  const filename = file.makeCopy('Test-Copy');

  // Logs the copied file's name to the console.
  console.log(filename.getName());
}

パラメータ

名前説明
nameString新しいコピーに適用するファイル名。

戻る

File - 新しいコピー。

承認

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

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

makeCopy(name, destination)

宛先ディレクトリにファイルのコピーを作成し、指定された名前を付けます。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files.
while (files.hasNext()) {
  const file = files.next();

  // Creates a copy of each file, sets the file name, and adds the copied file
  // to the specified folder.
  // TODO(developer): Replace the folder ID with your own.
  const destination = DriveApp.getFolderById('123456abcxyz');
  const copiedFile = file.makeCopy('Test-Copy', destination);

  // Logs the file names to the console.
  console.log(copiedFile.getName());
}

パラメータ

名前説明
nameString新しいコピーに適用するファイル名。
destinationFolderファイルのコピー先のディレクトリ。

戻る

File - 新しいコピー。

承認

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

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

moveTo(destination)

このアイテムを指定された移動先フォルダに移動します。

アイテムを移動先のフォルダに移動するには、現在のユーザーがファイルの所有者であるか、アイテムの現在の親フォルダに対する編集権限を持っている必要があります。

パラメータ

名前説明
destinationFolder新しい親になるフォルダ。

戻る

File - チェーン用の File

承認

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

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

removeCommenter(emailAddress)

指定されたユーザーを File のコメント投稿者リストから削除します。このメソッドは、ユーザーが一般アクセス権を持つユーザーのクラスに属している場合(たとえば、File がユーザーのドメイン全体と共有されている場合など)に、ユーザーが File にアクセスすることをブロックしません。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files.
while (files.hasNext()) {
  const file = files.next();

  // Removes the given user from the list of commenters for each file.
  // TODO(developer): Replace the email with the email of the user you want to
  // remove.
  file.removeCommenter('cloudysanfrancisco@gmail.com');
}

パラメータ

名前説明
emailAddressString削除するユーザーのメールアドレス。

戻る

File - チェーン用の File

承認

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

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

removeCommenter(user)

指定されたユーザーを File のコメント投稿者リストから削除します。このメソッドは、ユーザーが一般アクセス権を持つユーザーのクラスに属している場合(たとえば、File がユーザーのドメイン全体と共有されている場合など)に、ユーザーが File にアクセスすることをブロックしません。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace the file name with your own.
const files = DriveApp.getFilesByName('Test');

// Loops through the files.
while (files.hasNext()) {
  const file = files.next();

  // Removes the given user from the list of commenters for each file.
  console.log(file.removeCommenter(Session.getActiveUser()));
}

パラメータ

名前説明
userUser削除するユーザーの表現。

戻る

File - チェーン用の File

承認

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

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

removeEditor(emailAddress)

指定されたユーザーを File の編集者リストから削除します。この方法では、ユーザーが一般的なアクセス権を持つユーザーのクラスに属している場合(たとえば、File がユーザーのドメイン全体と共有されている場合や、File がユーザーがアクセスできる共有ドライブにある場合など)、ユーザーが File にアクセスすることをブロックしません。

ドライブ ファイルの場合、この操作を行うと、閲覧者リストからもユーザーが削除されます。

パラメータ

名前説明
emailAddressString削除するユーザーのメールアドレス。

戻る

File - チェーン用の File

承認

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

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

removeEditor(user)

指定されたユーザーを File の編集者リストから削除します。この方法では、ユーザーが一般的なアクセス権を持つユーザーのクラスに属している場合(たとえば、File がユーザーのドメイン全体と共有されている場合や、File がユーザーがアクセスできる共有ドライブにある場合など)、ユーザーが File にアクセスすることをブロックしません。

ドライブ ファイルの場合、この操作を行うと、閲覧者リストからもユーザーが削除されます。

パラメータ

名前説明
userUser削除するユーザーの表現。

戻る

File - チェーン用の File

承認

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

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

removeViewer(emailAddress)

指定されたユーザーを File の閲覧者とコメント投稿者のリストから削除します。ユーザーが閲覧者やコメント投稿者ではなく編集者の場合、このメソッドは無効です。また、この方法では、ユーザーが一般アクセス権を持つユーザーのクラスに属している場合(たとえば、File がユーザーのドメイン全体と共有されている場合や、File がユーザーがアクセスできる共有ドライブにある場合など)、ユーザーが File にアクセスすることをブロックしません。

ドライブ ファイルの場合、この操作を行うと、ユーザーは編集者のリストからも削除されます。

パラメータ

名前説明
emailAddressString削除するユーザーのメールアドレス。

戻る

File - チェーン用の File

承認

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

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

removeViewer(user)

指定されたユーザーを File の閲覧者とコメント投稿者のリストから削除します。ユーザーが閲覧者ではなく編集者の場合、このメソッドは効果がありません。また、この方法では、一般アクセス権を持つユーザーのクラスに属しているユーザーが File にアクセスすることをブロックできません。たとえば、File がユーザーのドメイン全体と共有されている場合や、File がユーザーがアクセスできる共有ドライブにある場合などです。

ドライブ ファイルの場合、この操作を行うと、ユーザーは編集者のリストからも削除されます。

パラメータ

名前説明
userUser削除するユーザーの表現。

戻る

File - チェーン用の File

承認

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

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

revokePermissions(emailAddress)

指定されたユーザーに付与された File へのアクセス権を取り消します。このメソッドは、ユーザーが一般アクセス権を持つユーザーのクラスに属している場合(たとえば、File がユーザーのドメイン全体と共有されている場合など)、ユーザーが File にアクセスすることをブロックしません。

パラメータ

名前説明
emailAddressStringアクセス権を取り消すユーザーのメールアドレス。

戻る

File - チェーン用の File

承認

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

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

revokePermissions(user)

指定されたユーザーに付与された File へのアクセス権を取り消します。このメソッドは、ユーザーが一般アクセス権を持つユーザーのクラスに属している場合(たとえば、File がユーザーのドメイン全体と共有されている場合など)、ユーザーが File にアクセスすることをブロックしません。

パラメータ

名前説明
userUserアクセス権を取り消すユーザーの表現。

戻る

File - チェーン用の File

承認

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

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

setContent(content)

ファイルの内容を指定された置換で上書きします。content が 10 MB を超える場合は例外をスローします。

// Creates a text file with the content 'Hello, world!'
const file = DriveApp.createFile('New Text File', 'Hello, world!');

// Logs the content of the text file to the console.
console.log(file.getBlob().getDataAsString());

// Updates the content of the text file to 'Updated text!'
file.setContent('Updated text!');

// Logs content of the text file to the console.
console.log(file.getBlob().getDataAsString());

パラメータ

名前説明
contentStringファイルの新しいコンテンツ。

戻る

File - チェーン用の File

承認

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

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

setDescription(description)

File の説明を設定します。

パラメータ

名前説明
descriptionStringFile の新しい説明

戻る

File - チェーン用の File

承認

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

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

setName(name)

File の名前を設定します。

パラメータ

名前説明
nameStringFile の新しい名前

戻る

File - チェーン用の File

承認

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

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

setOwner(emailAddress)

File のオーナーを変更します。このメソッドは、以前のオーナーに File への明示的な編集アクセス権も付与します。

パラメータ

名前説明
emailAddressString新しいオーナーになるユーザーのメールアドレス

戻る

File - チェーン用の File

承認

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

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

setOwner(user)

File のオーナーを変更します。このメソッドは、以前のオーナーに File への明示的な編集アクセス権も付与します。

パラメータ

名前説明
userUser新しいオーナーになるユーザーの表現

戻る

File - チェーン用の File

承認

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

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

setSecurityUpdateEnabled(enabled)

リンクを使用して共有するときに、File がアクセスにリソースキーを必要とするかどうかを設定します。対象となるファイルとフォルダは、デフォルトで有効になっています。

詳しくは、Google ドライブのセキュリティ アップデートをご覧ください。

パラメータ

名前説明
enabledBooleanFile のリソースキー要件を有効にするかどうか。

戻る

File - チェーン用の File

承認

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

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

setShareableByEditors(shareable)

File の編集権限を持つユーザーが他のユーザーと共有したり、権限を変更したりすることを許可するかどうかを設定します。新しい File のデフォルトは true です。

パラメータ

名前説明
shareableBoolean編集権限を持つユーザーが他のユーザーと共有したり、権限を変更したりできるようにする場合は true、そうでない場合は false

戻る

File - チェーン用の File

承認

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

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

setSharing(accessType, permissionType)

File にアクセスできるユーザーのクラスと、明示的にアクセス権が付与された個々のユーザーに加えて、それらのユーザーに付与される権限を設定します。

// Creates a folder that anyone on the Internet can read from and write to.
// (Domain administrators can prohibit this setting for users of a Google
// Workspace domain.)
const folder = DriveApp.createFolder('Shared Folder');
folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);

パラメータ

名前説明
accessTypeAccessどのクラスのユーザーが File にアクセスできるようにするか
permissionTypePermissionFile にアクセスできるユーザーに付与する必要がある権限

戻る

File - チェーン用の File

承認

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

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

setStarred(starred)

ユーザーのドライブで File にスターを付けるかどうかを設定します。新しい File のデフォルトは false です。

パラメータ

名前説明
starredBooleanFile をユーザーのドライブでスター付きにする場合は true、そうでない場合は false

戻る

File - チェーン用の File

承認

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

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

setTrashed(trashed)

File がユーザーのドライブのゴミ箱にあるかどうかを設定します。File をゴミ箱に移動できるのはオーナーのみです。新しい File のデフォルトは false です。

パラメータ

名前説明
trashedBooleanFile をユーザーのドライブのごみ箱に移動する場合は true、移動しない場合は false

戻る

File - チェーン用の File

承認

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

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