Class Protection

保護

保護された範囲とシートにアクセスして変更できます。保護された範囲では、静的なセル範囲または名前付き範囲を保護できます。保護されたシートには、保護されていない領域が含まれている場合があります。古いバージョンの Google スプレッドシートで作成されたスプレッドシートの場合は、代わりに PageProtection クラスを使用します。

// Protect range A1:B10, then remove all other users from the list of editors. const ss = SpreadsheetApp.getActive(); const range = ss.getRange('A1:B10'); const protection = range.protect().setDescription('Sample protected range');  // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) {   protection.setDomainEdit(false); }
// Remove all range protections in the spreadsheet that the user has permission // to edit. const ss = SpreadsheetApp.getActive(); const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (let i = 0; i < protections.length; i++) {   const protection = protections[i];   if (protection.canEdit()) {     protection.remove();   } }
// Protect the active sheet, then remove all other users from the list of // editors. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect().setDescription('Sample protected sheet');  // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) {   protection.setDomainEdit(false); }

メソッド

メソッド戻り値の型概要
addEditor(emailAddress)Protection指定したユーザーを、保護されたシートまたは範囲の編集者のリストに追加します。
addEditor(user)Protection指定したユーザーを、保護されたシートまたは範囲の編集者のリストに追加します。
addEditors(emailAddresses)Protection指定されたユーザーの配列を、保護されたシートまたは範囲の編集者のリストに追加します。
addTargetAudience(audienceId)Protection指定された対象グループを保護された範囲の編集者として追加します。
canDomainEdit()Booleanスプレッドシートを所有するドメイン内のすべてのユーザーに、保護された範囲またはシートを編集する権限があるかどうかを決定します。
canEdit()Booleanユーザーが保護された範囲またはシートを編集する権限を持っているかどうかを判断します。
getDescription()String保護されている範囲またはシートの説明を取得します。
getEditors()User[]保護されている範囲またはシートの編集者のリストを取得します。
getProtectionType()ProtectionType保護区域のタイプ(RANGE または SHEET)を取得します。
getRange()Range保護されている範囲を取得します。
getRangeName()String保護された範囲が名前付き範囲に関連付けられている場合は、その名前を取得します。
getTargetAudiences()TargetAudience[]保護された範囲を編集できるターゲット オーディエンスの ID を返します。
getUnprotectedRanges()Range[]保護されているシート内の保護されていない範囲の配列を取得します。
isWarningOnly()Boolean保護領域で「警告ベース」の保護が使用されているかどうかを判断します。
remove()void範囲またはシートの保護を解除します。
removeEditor(emailAddress)Protection保護されたシートまたは範囲の編集者のリストから指定したユーザーを削除します。
removeEditor(user)Protection保護されたシートまたは範囲の編集者のリストから指定したユーザーを削除します。
removeEditors(emailAddresses)Protection保護されたシートまたは範囲の編集者のリストから、指定されたユーザーの配列を削除します。
removeTargetAudience(audienceId)Protection指定した対象グループを、保護された範囲の編集者として削除します。
setDescription(description)Protection保護された範囲またはシートの説明を設定します。
setDomainEdit(editable)Protectionスプレッドシートを所有するドメイン内のすべてのユーザーに、保護された範囲またはシートを編集する権限があるかどうかを設定します。
setNamedRange(namedRange)Protection保護された範囲を既存の名前付き範囲に関連付けます。
setRange(range)Protection保護されている範囲を調整します。
setRangeName(rangeName)Protection保護された範囲を既存の名前付き範囲に関連付けます。
setUnprotectedRanges(ranges)Protection保護されたシート内の指定された範囲の配列の保護を解除します。
setWarningOnly(warningOnly)Protectionこの保護範囲で「警告ベース」の保護を使用するかどうかを設定します。

詳細なドキュメント

addEditor(emailAddress)

指定したユーザーを、保護されたシートまたは範囲の編集者のリストに追加します。このメソッドでは、ユーザーにスプレッドシート自体を編集する権限が自動的に付与されるわけではありません。追加で Spreadsheet.addEditor(emailAddress) を呼び出す必要があります。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Adds an editor to the spreadsheet using an email address. // TODO(developer): Replace the email address with a valid email. ss.addEditor('[email protected]');  // Gets a sheet by its name and protects it. const sheet = ss.getSheetByName('Sheet1'); const sampleProtectedSheet = sheet.protect();  // Adds an editor of the protected sheet using an email address. // TODO(developer): Replace the email address with a valid email. sampleProtectedSheet.addEditor('[email protected]');  // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors();  // Logs the editors' email addresses to the console. for (const editor of editors) {   console.log(editor.getEmail()); }

パラメータ

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

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

addEditor(user)

指定したユーザーを、保護されたシートまたは範囲の編集者のリストに追加します。このメソッドでは、ユーザーにスプレッドシート自体を編集する権限が自動的に付与されるわけではありません。追加で Spreadsheet.addEditor(user) を呼び出す必要があります。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1');  // Protects the sheet. const sampleProtectedSheet = sheet.protect();  // Adds the active user as an editor of the protected sheet. sampleProtectedSheet.addEditor(Session.getActiveUser());  // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors();  // Logs the editors' email addresses to the console. for (const editor of editors) {   console.log(editor.getEmail()); }

パラメータ

名前説明
userUser追加するユーザーを表します。

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

addEditors(emailAddresses)

指定されたユーザーの配列を、保護されたシートまたは範囲の編集者のリストに追加します。この方法では、ユーザーにスプレッドシート自体を編集する権限が自動的に付与されるわけではありません。そのためには、Spreadsheet.addEditors(emailAddresses) を呼び出す必要があります。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1');  // Creates variables for the email addresses to add as editors. // TODO(developer): Replace the email addresses with valid ones. const TEST_EMAIL_1 = '[email protected]'; const TEST_EMAIL_2 = '[email protected]';  // Protects the sheet. const sampleProtectedSheet = sheet.protect();  // Adds editors to the protected sheet using the email address variables. sampleProtectedSheet.addEditors([TEST_EMAIL_1, TEST_EMAIL_2]);  // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors();  // Logs the editors' email addresses to the console. for (const editor of editors) {   console.log(editor.getEmail()); }

パラメータ

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

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

addTargetAudience(audienceId)

指定された対象グループを保護された範囲の編集者として追加します。

パラメータ

名前説明
audienceIdString追加する対象グループの ID。

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

canDomainEdit()

スプレッドシートを所有するドメイン内のすべてのユーザーに、保護された範囲またはシートを編集する権限があるかどうかを決定します。保護された範囲またはシートを編集する権限がない場合は、例外をスローします。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1');  // Protects the sheet. const sampleProtectedSheet = sheet.protect();  // Logs whether domain users have permission to edit the protected sheet to the // console. console.log(sampleProtectedSheet.canDomainEdit());

戻る

Boolean - スプレッドシートを所有するドメイン内のすべてのユーザーが、保護された範囲またはシートを編集する権限を持っている場合は true、そうでない場合は false

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

canEdit()

ユーザーが保護された範囲またはシートを編集する権限を持っているかどうかを判断します。スプレッドシートのオーナーは、保護された範囲とシートをいつでも編集できます。

// Remove all range protections in the spreadsheet that the user has permission // to edit. const ss = SpreadsheetApp.getActive(); const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (let i = 0; i < protections.length; i++) {   const protection = protections[i];   if (protection.canEdit()) {     protection.remove();   } }

戻る

Boolean - 保護された範囲またはシートを編集する権限がある場合は true、ない場合は false

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getDescription()

保護されている範囲またはシートの説明を取得します。説明が設定されていない場合、このメソッドは空の文字列を返します。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1');  // Protects the sheet and sets the description. const sampleProtectedSheet =     sheet.protect().setDescription('Sample sheet is protected');  // Gets the description of the protected sheet and logs it to the console. const sampleProtectedSheetDescription = sampleProtectedSheet.getDescription(); console.log(sampleProtectedSheetDescription);

戻る

String - 保護された範囲またはシートの説明。説明が設定されていない場合は空の文字列。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getEditors()

保護されている範囲またはシートの編集者のリストを取得します。保護された範囲またはシートを編集する権限がない場合は、例外をスローします。

// Protect the active sheet, then remove all other users from the list of // editors. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect().setDescription('Sample protected sheet');  // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) {   protection.setDomainEdit(false); }

戻る

User[] - 保護された範囲またはシートを編集する権限を持つユーザーの配列

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getProtectionType()

保護区域のタイプ(RANGE または SHEET)を取得します。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1');  // Protects the sheet. const sampleProtectedSheet = sheet.protect();  // Gets the type of the protected area. const protectionType = sampleProtectedSheet.getProtectionType();  // Logs 'SHEET'to the console since the type of the protected area is a sheet. console.log(protectionType.toString());

戻る

ProtectionType - 保護区域のタイプ(RANGE または SHEET)。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getRange()

保護されている範囲を取得します。保護が範囲ではなくシートに適用されている場合、このメソッドはシート全体にまたがる範囲を返します。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1');  // Gets the range 'A1:B10' of Sheet1. const range = sheet.getRange('A1:B10');  // Makes cells A1:B10 a protected range. const sampleProtectedRange = range.protect();  // Gets the protected ranges on the sheet. const protections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);  // Logs the A1 notation of the first protected range on the sheet. console.log(protections[0].getRange().getA1Notation());

戻る

Range - 保護されている範囲。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getRangeName()

保護された範囲が名前付き範囲に関連付けられている場合は、その名前を取得します。保護が名前付き範囲に関連付けられていない場合は、null を返します。スクリプトで保護された範囲を名前付き範囲に関連付けるには、setRangeName(rangeName) を明示的に呼び出す必要があります。setRangeName(rangeName) を呼び出すことなく、Range.protect() を呼び出して名前付き範囲である Range から保護を作成しても、関連付けには不十分です。ただし、Google スプレッドシートの UI で名前付き範囲から保護範囲を作成すると、それらが自動的に関連付けられます。

// Protect a named range in a spreadsheet and log the name of the protected // range. const ss = SpreadsheetApp.getActive(); const range = ss.getRange('A1:B10'); const protection = range.protect(); ss.setNamedRange('Test', range);  // Create a named range. protection.setRangeName(     'Test');  // Associate the protection with the named range. Logger.log(     protection.getRangeName());  // Verify the name of the protected range.

戻る

String - 保護された名前付き範囲の名前。保護された範囲が名前付き範囲に関連付けられていない場合は null

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getTargetAudiences()

保護された範囲を編集できるターゲット オーディエンスの ID を返します。

戻る

TargetAudience[] - 対象グループの ID の配列。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

getUnprotectedRanges()

保護されているシート内の保護されていない範囲の配列を取得します。Protection オブジェクトが保護されたシートではなく保護された範囲に対応している場合、このメソッドは空の配列を返します。保護されていない範囲を変更するには、setUnprotectedRanges(ranges) を使用して範囲の新しい配列を設定します。シート全体を再度保護するには、空の配列を設定します。

// Unprotect cells E2:F5 in addition to any other unprotected ranges in the // protected sheet. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect(); const unprotected = protection.getUnprotectedRanges(); unprotected.push(sheet.getRange('E2:F5')); protection.setUnprotectedRanges(unprotected);

戻る

Range[] - 保護されているシート内の保護されていない範囲の配列

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

isWarningOnly()

保護領域で「警告ベース」の保護が使用されているかどうかを判断します。警告ベースの保護とは、すべてのユーザーがその領域のデータを編集できることを意味します。ただし、編集時に、編集の確認を求める警告が表示されます。デフォルトでは、保護された範囲またはシートは警告ベースではありません。警告状態に変更するには、setWarningOnly(warningOnly) を使用します。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1');  // Protects the sheet. const sampleProtectedSheet = sheet.protect();  // Sets the warning status for the protected sheet as true. sampleProtectedSheet.setWarningOnly(true);  const protectedSheetWarningStatus = sampleProtectedSheet.isWarningOnly();  // Logs the warning status of the protected sheet to the console. console.log(protectedSheetWarningStatus);

戻る

Boolean - 保護されている範囲またはシートで警告ベースの保護のみを使用している場合は true

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

remove()

範囲またはシートの保護を解除します。

// Remove all range protections in the spreadsheet that the user has permission // to edit. const ss = SpreadsheetApp.getActive(); const protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (let i = 0; i < protections.length; i++) {   const protection = protections[i];   if (protection.canEdit()) {     protection.remove();   } }
// Remove sheet protection from the active sheet, if the user has permission to // edit it. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0]; if (protection?.canEdit()) {   protection.remove(); }

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeEditor(emailAddress)

保護されたシートまたは範囲の編集者のリストから指定したユーザーを削除します。ただし、ユーザーが編集権限を持つ Google グループのメンバーである場合や、ドメイン内のすべてのユーザーが編集権限を持っている場合は、保護された領域を編集できます。スプレッドシートのオーナーと現在のユーザーは削除できません。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1');  // Creates a variable for an email address. // TODO(developer): Replace the email address with a valid one. const TEST_EMAIL = '[email protected]';  // Protects the sheet. const sampleProtectedSheet = sheet.protect();  // Adds an editor to the protected sheet using the email address variable. sampleProtectedSheet.addEditor(TEST_EMAIL);  // Removes the editor from the protected sheet using the email address variable. sampleProtectedSheet.removeEditor(TEST_EMAIL);  // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors();  // Logs the editors' email addresses to the console. for (const editor of editors) {   console.log(editor.getEmail()); }

パラメータ

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

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeEditor(user)

保護されたシートまたは範囲の編集者のリストから指定したユーザーを削除します。ただし、ユーザーが編集権限を持つ Google グループのメンバーである場合や、ドメイン内のすべてのユーザーが編集権限を持っている場合は、保護された領域を編集できます。スプレッドシートの所有者と現在のユーザーは削除できません。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets a sheet by its name. const sheet = ss.getSheetByName('Sheet1');  // Protects the sheet. const sampleProtectedSheet = sheet.protect();  // Removes the active user from the editors of the protected sheet. sampleProtectedSheet.removeEditor(Session.getActiveUser());  // Gets the editors of the protected sheet. const editors = sampleProtectedSheet.getEditors();  // Logs the editors' email addresses to the console. for (const editor of editors) {   console.log(editor.getEmail()); }

パラメータ

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

戻る

Protection - チェーン用の保護設定を表すオブジェクト

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeEditors(emailAddresses)

保護されたシートまたは範囲の編集者のリストから、指定されたユーザーの配列を削除します。ただし、編集権限を持つ Google グループのメンバーであるユーザーや、ドメイン内のすべてのユーザーに編集権限が付与されているユーザーは、保護された領域を編集できます。スプレッドシートの所有者と現在のユーザーは削除できません。

// Protect the active sheet, then remove all other users from the list of // editors. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect().setDescription('Sample protected sheet');  // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) {   protection.setDomainEdit(false); }

パラメータ

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

戻る

Protection - チェーン用の保護設定を表すオブジェクト

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

removeTargetAudience(audienceId)

指定した対象グループを、保護された範囲の編集者として削除します。

パラメータ

名前説明
audienceIdString削除する対象グループの ID。

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setDescription(description)

保護された範囲またはシートの説明を設定します。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets the sheet 'Sheet1' by its name. const sheet = ss.getSheetByName('Sheet1');  // Protects the sheet. const sampleProtectedSheet = sheet.protect();  // Sets the sheet description to 'Sheet1 is protected.' sampleProtectedSheet.setDescription('Sheet1 is protected');  // Gets the description of the protected sheet. const sampleProtectedSheetDescription = sampleProtectedSheet.getDescription();  // Logs the description of the protected sheet to the console. console.log(sampleProtectedSheetDescription);

パラメータ

名前説明
descriptionString保護されている範囲またはシートの説明。

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setDomainEdit(editable)

スプレッドシートを所有するドメイン内のすべてのユーザーに、保護された範囲またはシートを編集する権限があるかどうかを設定します。明示的な編集権限を持つユーザーは、この設定に関係なく保護された領域を編集できます。スプレッドシートが Google Workspace ドメインに属していない場合(つまり、gmail.com アカウントが所有している場合)は、例外をスローします。

パラメータ

名前説明
editableBooleantrue: スプレッドシートを所有するドメイン内のすべてのユーザーに、保護された範囲またはシートを編集する権限が必要な場合。そうでない場合は false

戻る

Protection - チェーン用の保護設定を表すオブジェクト

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setNamedRange(namedRange)

保護された範囲を既存の名前付き範囲に関連付けます。名前付きの範囲が現在の保護範囲とは異なる領域をカバーしている場合、このメソッドは保護を移動して、代わりに名前付きの範囲をカバーします。名前付き範囲は、現在の保護範囲と同じシート上にある必要があります。保護された範囲を名前付き範囲に関連付けるには、スクリプトでこのメソッドを明示的に呼び出す必要があります。setRangeName(rangeName) を呼び出すことなく Range.protect() を呼び出して、名前付き範囲である Range から保護を作成しても、関連付けには不十分です。ただし、Google スプレッドシートの UI で名前付き範囲から保護範囲を作成すると、自動的に関連付けられます。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Protects cells A1:D10 on Sheet1. const sheet = ss.getSheetByName('Sheet1'); const protectedRange = sheet.getRange('A1:D10').protect();  // Logs the current protected range, A1:D10. console.log(protectedRange.getRange().getA1Notation());  // Creates a named range for cells E1:J10 called 'NewRange.' const newRange = sheet.getRange('E1:J10'); ss.setNamedRange('NewRange', newRange); const namedRange = ss.getNamedRanges()[0];  // Updates the protected range to the named range, 'NewRange.' // This updates the protected range on Sheet1 from A1:D10 to E1:J10. protectedRange.setNamedRange(namedRange);  // Logs the updated protected range to the console. console.log(protectedRange.getRange().getA1Notation());

パラメータ

名前説明
namedRangeNamedRange保護された範囲に関連付ける既存の名前付き範囲。

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setRange(range)

保護されている範囲を調整します。指定された範囲が現在の保護範囲とは異なる領域をカバーしている場合、このメソッドは保護を移動して、代わりに新しい範囲をカバーします。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Protects cells A1:D10 on Sheet1 of the spreadsheet. const sheet = ss.getSheetByName('Sheet1'); const protectedRange = sheet.getRange('A1:D10').protect();  // Logs the original protected range, A1:D10, to the console. console.log(protectedRange.getRange().getA1Notation());  // Gets the range E1:J10. const newRange = sheet.getRange('E1:J10');  // Updates the protected range to E1:J10. protectedRange.setRange(newRange);  // Logs the updated protected range to the console. console.log(protectedRange.getRange().getA1Notation());

パラメータ

名前説明
rangeRange編集から保護する新しい範囲。

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setRangeName(rangeName)

保護された範囲を既存の名前付き範囲に関連付けます。名前付きの範囲が現在の保護範囲とは異なる領域をカバーしている場合、このメソッドは保護を移動して、代わりに名前付きの範囲をカバーします。名前付き範囲は、現在の保護範囲と同じシート上にある必要があります。保護された範囲を名前付き範囲に関連付けるには、スクリプトでこのメソッドを明示的に呼び出す必要があります。setRangeName(rangeName) を呼び出すことなく Range.protect() を呼び出して、名前付き範囲である Range から保護を作成しても、関連付けには不十分です。ただし、Google スプレッドシートの UI で名前付き範囲から保護範囲を作成すると、自動的に関連付けられます。

// Protect a named range in a spreadsheet and log the name of the protected // range. const ss = SpreadsheetApp.getActive(); const range = ss.getRange('A1:B10'); const protection = range.protect(); ss.setNamedRange('Test', range);  // Create a named range. protection.setRangeName(     'Test');  // Associate the protection with the named range. Logger.log(     protection.getRangeName());  // Verify the name of the protected range.

パラメータ

名前説明
rangeNameString保護する名前付き範囲の名前。

戻る

Protection - チェーン用の保護設定を表すオブジェクト

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setUnprotectedRanges(ranges)

保護されたシート内の指定された範囲の配列の保護を解除します。Protection オブジェクトが保護されたシートではなく保護された範囲に対応している場合、またはいずれかの範囲が保護されたシートにない場合は、例外をスローします。保護されていない範囲を変更するには、範囲の新しい配列を設定します。シート全体を再保護するには、空の配列を設定します。

// Protect the active sheet except B2:C5, then remove all other users from the // list of editors. const sheet = SpreadsheetApp.getActiveSheet(); const protection = sheet.protect().setDescription('Sample protected sheet'); const unprotected = sheet.getRange('B2:C5'); protection.setUnprotectedRanges([unprotected]);  // Ensure the current user is an editor before removing others. Otherwise, if // the user's edit permission comes from a group, the script throws an exception // upon removing the group. const me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) {   protection.setDomainEdit(false); }

パラメータ

名前説明
rangesRange[]保護されているシート内で保護を解除する範囲の配列。

戻る

Protection - チェーン用の保護設定を表すオブジェクト

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets

setWarningOnly(warningOnly)

この保護範囲で「警告ベース」の保護を使用するかどうかを設定します。警告ベースの保護とは、すべてのユーザーがその領域のデータの編集を行えるが、編集時に編集の確認を求める警告が表示されることを意味します。デフォルトでは、保護された範囲またはシートは警告ベースではありません。警告状態を確認するには、isWarningOnly() を使用します。

// Opens the spreadsheet file by its URL. If you created your script from within // a Google Sheets file, you can use SpreadsheetApp.getActiveSpreadsheet() // instead. // TODO(developer): Replace the URL with your own. const ss = SpreadsheetApp.openByUrl(     'https://docs.google.com/spreadsheets/d/abc123456/edit', );  // Gets the sheet 'Sheet1' by its name. const sheet = ss.getSheetByName('Sheet1');  // Protects the sheet and sets the protection to warning-based. const sampleProtectedSheet = sheet.protect().setWarningOnly(true);  // Logs whether the protected sheet is warning-based to the console. console.log(sampleProtectedSheet.isWarningOnly());

パラメータ

名前説明
warningOnlyBoolean

戻る

Protection - チェーン用の保護設定を表すオブジェクト。

承認

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

  • https://www.googleapis.com/auth/spreadsheets.currentonly
  • https://www.googleapis.com/auth/spreadsheets