Class File

ファイル

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

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

Methods

メソッド戻り値の型概要
addCommenter(emailAddress)FileFile のコメント投稿者のリストに特定のユーザーを追加します。
addCommenter(user)FileFile のコメント投稿者のリストに特定のユーザーを追加します。
addCommenters(emailAddresses)File指定したユーザーの配列を File のコメント投稿者のリストに追加します。
addEditor(emailAddress)FileFile の編集者のリストに、指定されたユーザーを追加します。
addEditor(user)FileFile の編集者のリストに、指定されたユーザーを追加します。
addEditors(emailAddresses)File指定したユーザーの配列を File の編集者のリストに追加します。
addViewer(emailAddress)FileFile の閲覧者リストに特定のユーザーを追加します。
addViewer(user)FileFile の閲覧者リストに特定のユーザーを追加します。
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()Permission明示的にアクセス権が付与されている個別のユーザーを除く、File にアクセスできるユーザーに付与されている権限を取得します。
getSize()IntegerFile をドライブに保存するために使用されるバイト数を取得します。
getTargetId()Stringショートカットの場合は、参照するアイテムの ID を返します。
getTargetMimeType()Stringショートカットの場合は、参照するアイテムの MIME タイプを返します。
getTargetResourceKey()Stringファイルがショートカットの場合は、参照するアイテムのリソースキーを返します。
getThumbnail()Blobファイルのサムネイル画像を取得します。サムネイルが存在しない場合は、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)FileFile のコメント投稿者リストから指定されたユーザーを削除します。
removeCommenter(user)FileFile のコメント投稿者リストから指定されたユーザーを削除します。
removeEditor(emailAddress)FileFile の編集者のリストから指定されたユーザーを削除します。
removeEditor(user)FileFile の編集者のリストから指定されたユーザーを削除します。
removeViewer(emailAddress)FileFile の閲覧者とコメント投稿者のリストから指定されたユーザーを削除します。
removeViewer(user)FileFile の閲覧者とコメント投稿者のリストから指定されたユーザーを削除します。
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)File明示的にアクセス権を与えられている個々のユーザーの他に、File にアクセスできるユーザーのクラスと、そのユーザーに付与される権限を設定します。
setStarred(starred)Fileユーザーのドライブでスターを付ける File を設定します。
setTrashed(trashed)FileFile をユーザーのドライブのゴミ箱に入れるかどうかを設定します。

詳細なドキュメント

addCommenter(emailAddress)

File のコメント投稿者のリストに特定のユーザーを追加します。ユーザーがすでに閲覧者のリストに含まれていた場合、このメソッドはユーザーを閲覧者リストから昇格させます。

// Gets a list of all files in Google Drive with the given name.
// TODO(developer): Replace 'cloudysanfrancisco@gmail.com' with the email address that you
// want to add as a commenter.
while (files.hasNext()) {
  const file = files.next();
  email = 'cloudysanfrancisco@gmail.com';
  console.log(file.addCommenter(email));
}

パラメータ

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

パラメータ

Name説明
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));
}

パラメータ

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

復路

File - チェーン用の File

承認

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

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

addEditor(emailAddress)

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

パラメータ

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

復路

File - チェーン用の File

承認

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

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

addEditor(user)

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

パラメータ

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

復路

File - チェーン用の File

承認

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

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

addEditors(emailAddresses)

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

パラメータ

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

復路

File - チェーン用の File

承認

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

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

addViewer(emailAddress)

File の閲覧者リストに特定のユーザーを追加します。ユーザーがすでに編集者のリストに含まれていた場合、このメソッドは無視されます。

パラメータ

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

復路

File - チェーン用の File

承認

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

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

addViewer(user)

File の閲覧者リストに特定のユーザーを追加します。ユーザーがすでに編集者のリストに含まれていた場合、このメソッドは無視されます。

パラメータ

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

復路

File - チェーン用の File

承認

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

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

addViewers(emailAddresses)

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

パラメータ

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

復路

File - チェーン用の File

承認

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

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

getAccess(email)

指定したユーザーに付与された権限を取得します。

パラメータ

Name説明
emailString権限を確認するユーザーのメールアドレス

復路

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

承認

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

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

getAccess(user)

指定したユーザーに付与された権限を取得します。

パラメータ

Name説明
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 ドメインは、一時的により厳格な割り当ての対象となる場合があります。

パラメータ

Name説明
contentTypeString変換する MIME タイプ。ほとんどの blob で有効なオプションは 'application/pdf' です。BMP、GIF、JPEG、PNG 形式の画像の場合、'image/bmp''image/gif''image/jpeg''image/png' のいずれかも有効です。

復路

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 を取得します。URL にアクセスできるのは、Google ドライブでファイルを開く権限を持つユーザーのみです。ブラウザでこの 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 を返します。

復路

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 - ファイルのサムネイル画像。

承認

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

パラメータ

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

パラメータ

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

パラメータ

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

復路

File - 新しいコピー。

承認

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

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

moveTo(destination)

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

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

パラメータ

Name説明
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');
}

パラメータ

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

パラメータ

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

復路

File - チェーン用の File

承認

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

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

removeEditor(emailAddress)

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

ドライブ内のファイルの場合、閲覧者のリストからユーザーが削除されます。

パラメータ

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

復路

File - チェーン用の File

承認

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

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

removeEditor(user)

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

ドライブ内のファイルの場合、閲覧者のリストからユーザーが削除されます。

パラメータ

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

復路

File - チェーン用の File

承認

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

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

removeViewer(emailAddress)

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

ドライブ ファイルの場合は、編集者のリストからユーザーが削除されます。

パラメータ

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

復路

File - チェーン用の File

承認

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

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

removeViewer(user)

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

ドライブ ファイルの場合は、編集者のリストからユーザーが削除されます。

パラメータ

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

復路

File - チェーン用の File

承認

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

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

revokePermissions(emailAddress)

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

パラメータ

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

復路

File - チェーン用の File

承認

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

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

revokePermissions(user)

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

パラメータ

Name説明
userUserアクセス権を取り消すユーザーの指定。

復路

File - チェーン用の File

承認

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

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

setContent(content)

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

// Creates a text file with the content 'Hello, world!'
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());

パラメータ

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

復路

File - チェーン用の File

承認

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

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

setDescription(description)

File の説明を設定します。

パラメータ

Name説明
descriptionStringFile の新しい説明

復路

File - この File(チェーン用)

承認

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

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

setName(name)

File の名前を設定します。

パラメータ

Name説明
nameStringFile の新しい名前

復路

File - この File(チェーン用)

承認

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

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

setOwner(emailAddress)

File のオーナーを変更します。このメソッドにより、前の所有者は File に対する明示的な編集アクセス権も付与されます。

パラメータ

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

復路

File - この File(チェーン用)

承認

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

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

setOwner(user)

File のオーナーを変更します。このメソッドにより、前の所有者は File に対する明示的な編集アクセス権も付与されます。

パラメータ

Name説明
userUser新しいオーナーとなるユーザー

復路

File - この File(チェーン用)

承認

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

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

setSecurityUpdateEnabled(enabled)

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

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

パラメータ

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

復路

File - チェーン用の File

承認

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

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

setShareableByEditors(shareable)

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

パラメータ

Name説明
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.)
var folder = DriveApp.createFolder('Shared Folder');
folder.setSharing(DriveApp.Access.ANYONE, DriveApp.Permission.EDIT);

パラメータ

Name説明
accessTypeAccessFile にアクセスを許可するユーザーのクラス
permissionTypePermissionFile にアクセスできるユーザーに付与する権限

復路

File - この File(チェーン用)

承認

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

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

setStarred(starred)

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

パラメータ

Name説明
starredBooleantrueユーザーのドライブでFileスターを付ける場合は false

復路

File - この File(チェーン用)

承認

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

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

setTrashed(trashed)

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

パラメータ

Name説明
trashedBooleantrue File をユーザーのドライブのゴミ箱に移動する場合は false

復路

File - この File(チェーン用)

承認

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

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