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)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()Permission明示的にアクセス権が付与されている個々のユーザーに加え、File にアクセスできるユーザーに付与されている権限を取得します。
getSize()Integerドライブに File を保存するために使用されるバイト数を取得します。
getTargetId()Stringショートカットの場合は、キーが指しているアイテムの ID を返します。
getTargetMimeType()Stringショートカットの場合、ポイントしているアイテムの MIME タイプを返します。
getTargetResourceKey()Stringファイルがショートカットの場合は、それが指しているアイテムのリソースキーを返します。
getThumbnail()Blobファイルのサムネイル画像を取得します。サムネイルが存在しない場合は null を取得します。
getUrl()Stringドライブやドキュメントなどの Google アプリで File を開くために使用できる URL を取得します。
getViewers()User[]この File の閲覧者とコメント投稿者のリストを取得します。
isShareableByEditors()BooleanFile に対する編集権限を持つユーザーに、他のユーザーと共有したり、権限を変更したりできるかどうかを決定します。
isStarred()Booleanユーザーのドライブで File にスターが付いているかどうかを判断します。
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));
}

パラメータ

名前説明
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)

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

パラメータ

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

リターン

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

承認

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

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

getAccess(user)

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

パラメータ

名前説明
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' のいずれかも有効です。

リターン

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

承認

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

パラメータ

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

パラメータ

名前説明
accessTypeAccessFile へのアクセスを許可するユーザーのクラス
permissionTypePermissionFile にアクセスできるユーザーに付与する必要がある権限。

リターン

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

承認

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

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

setStarred(starred)

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

パラメータ

名前説明
starredBooleanユーザーのドライブで File にスターを付ける必要がある場合は 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