保護されている範囲やシートへのアクセスと変更。保護されている範囲では、セルの静的な範囲または名前付き範囲を保護できます。保護されているシートには、保護されていない領域が含まれている可能性があります。以前のバージョンの Google スプレッドシートで作成したスプレッドシートには、代わりに
クラスを使用してください。
PageProtection
// Protect range A1:B10, then remove all other users from the list of editors. var ss = SpreadsheetApp.getActive(); var range = ss.getRange('A1:B10'); var 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. var 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. var ss = SpreadsheetApp.getActive(); var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (var i = 0; i < protections.length; i++) { var protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
// Protect the active sheet, then remove all other users from the list of editors. var sheet = SpreadsheetApp.getActiveSheet(); var 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. var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
Methods
メソッド | 戻り値の型 | 概要 |
---|---|---|
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('cloudysanfrancisco@gmail.com'); // 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('cloudysanfrancisco@gmail.com'); // 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()); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
emailAddress | String | 追加するユーザーのメールアドレス。 |
リターン
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()); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
user | User | 追加するユーザーの表現。 |
リターン
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 = 'cloudysanfrancisco@gmail.com'; const TEST_EMAIL_2 = 'baklavainthebalkans@gmail.com'; // 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()); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
emailAddresses | String[] | 追加するユーザーのメールアドレスの配列。 |
リターン
Protection
- チェーンの保護設定を表すオブジェクト。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addTargetAudience(audienceId)
指定した対象グループを保護されている範囲の編集者として追加します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
audienceId | String | 追加する対象グループの 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. var ss = SpreadsheetApp.getActive(); var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (var i = 0; i < protections.length; i++) { var 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. var sheet = SpreadsheetApp.getActiveSheet(); var 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. var 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. var ss = SpreadsheetApp.getActive(); var range = ss.getRange('A1:B10'); var 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. var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect(); var 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. var ss = SpreadsheetApp.getActive(); var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE); for (var i = 0; i < protections.length; i++) { var protection = protections[i]; if (protection.canEdit()) { protection.remove(); } }
// Remove sheet protection from the active sheet, if the user has permission to edit it. var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0]; if (protection && 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 = 'baklavainthebalkans@gmail.com'; // 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()); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
emailAddress | String | 削除するユーザーのメールアドレス。 |
リターン
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()); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
user | User | 削除するユーザーの表現。 |
リターン
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. var sheet = SpreadsheetApp.getActiveSheet(); var 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. var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
emailAddresses | String[] | 削除するユーザーのメールアドレスの配列。 |
リターン
Protection
- チェーン用の保護設定を表すオブジェクト
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeTargetAudience(audienceId)
保護されている範囲の編集者として、指定した対象グループを削除します。
パラメータ
名前 | 型 | 説明 |
---|---|---|
audienceId | String | 削除するターゲット ユーザーの 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);
パラメータ
名前 | 型 | 説明 |
---|---|---|
description | String | 保護されている範囲またはシートの説明。 |
リターン
Protection
- チェーンの保護設定を表すオブジェクト。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setDomainEdit(editable)
スプレッドシートを所有するドメイン内のすべてのユーザーに、保護された範囲またはシートを編集する権限を与えるかどうかを設定します。なお、明示的な編集権限を持っているユーザーは、この設定に関係なく、保護エリアを編集できます。スプレッドシートが Google Workspace ドメインに属していない場合(つまり、gmail.com アカウントによって所有されている場合)は例外をスローします。
パラメータ
名前 | 型 | 説明 |
---|---|---|
editable | Boolean | スプレッドシートを所有するドメイン内のすべてのユーザーに、保護された範囲やシートの編集権限を与える場合は true 、そうでない場合は 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());
パラメータ
名前 | 型 | 説明 |
---|---|---|
namedRange | NamedRange | 保護されている範囲に関連付ける既存の名前付き範囲。 |
リターン
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());
パラメータ
名前 | 型 | 説明 |
---|---|---|
range | Range | 編集から保護する新しい範囲です。 |
リターン
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. var ss = SpreadsheetApp.getActive(); var range = ss.getRange('A1:B10'); var 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.
パラメータ
名前 | 型 | 説明 |
---|---|---|
rangeName | String | 保護する名前付き範囲の名前。 |
リターン
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. var sheet = SpreadsheetApp.getActiveSheet(); var protection = sheet.protect().setDescription('Sample protected sheet'); var 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. var me = Session.getEffectiveUser(); protection.addEditor(me); protection.removeEditors(protection.getEditors()); if (protection.canDomainEdit()) { protection.setDomainEdit(false); }
パラメータ
名前 | 型 | 説明 |
---|---|---|
ranges | Range[] | 保護されたシート内で保護されないようにする範囲の配列。 |
リターン
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());
パラメータ
名前 | 型 | 説明 |
---|---|---|
warningOnly | Boolean |
リターン
Protection
- チェーンの保護設定を表すオブジェクト。
承認
この方法を使用するスクリプトには、次の 1 つ以上のスコープによる承認が必要です。
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets