访问和修改受保护的范围和工作表。受保护的范围可以保护单元格的静态范围或命名的范围。受保护的工作表可能包含不受保护的区域。对于使用旧版 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); }
方法
详细文档
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
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
addTargetAudience(audienceId)
将指定的目标对象群组添加为受保护范围的编辑者。
参数
名称 | 类型 | 说明 |
---|---|---|
audienceId | String | 要添加的目标受众群体 ID。 |
返程
Protection
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 对受保护范围或工作表的说明;如果未设置说明,则为空字符串。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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[]
- 一组用户,用于修改受保护的范围或工作表
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getProtectionType()
// 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
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 要保护的范围。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getRangeName()
获取受保护的范围的名称(如果与指定的范围相关联)。如果保护措施未与已命名的范围相关联,则返回 null
。请注意,脚本必须明确调用 setRangeName(rangeName)
才能将受保护的范围与命名的范围相关联;调用 Range.protect()
来为恰好为命名范围的 Range
创建保护措施,而不调用 setRangeName(rangeName)
是不够的。不过,如果通过 Google 表格界面中的命名范围创建受保护的范围,则系统会自动关联这些范围。
// 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
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
getTargetAudiences()
返回可修改受保护范围的目标受众群体的 ID。
返程
TargetAudience[]
- 目标对象群组的 ID 数组。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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[]
- 受保护的工作表中一系列不受保护的范围
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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(); }
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 表示保护设置的对象,用于串联
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 表示保护设置的对象,用于串联
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
removeTargetAudience(audienceId)
移除作为受保护范围的编辑者的指定目标对象群组。
参数
名称 | 类型 | 说明 |
---|---|---|
audienceId | String | 要移除的目标对象 ID。 |
返程
Protection
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setDomainEdit(editable)
设置此电子表格中拥有相应域名的所有用户是否都有权限修改受保护的范围或工作表。请注意,无论此设置如何,任何具有明确修改权限的用户都可以修改保护区。如果电子表格不属于 Google Workspace 网域(即,该电子表格归 gmail.com 帐号所有),则会抛出异常。
参数
名称 | 类型 | 说明 |
---|---|---|
editable | Boolean | 如果网域中所有拥有电子表格的用户都有权修改受保护的范围或工作表,则为 true ;否则为 false 。 |
返程
Protection
- 表示保护设置的对象,用于串联
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setNamedRange(namedRange)
将受保护的范围与现有命名范围相关联。如果命名范围与当前受保护的范围不同,则此方法会改用附加的保护范围来覆盖命名的范围。命名的范围必须与当前受保护的范围位于同一工作表中。请注意,脚本必须明确地将受保护的范围与命名的范围相关联;调用 Range.protect()
来为恰好为命名范围的 Range
创建保护,而不调用 setRangeName(rangeName)
是不够的。不过,如果通过 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'); // 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
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets
setRangeName(rangeName)
将受保护的范围与现有命名范围相关联。如果命名范围与当前受保护的范围不同,则此方法会改用附加的保护范围来覆盖命名的范围。命名的范围必须与当前受保护的范围位于同一工作表中。请注意,脚本必须明确地将受保护的范围与命名的范围相关联;调用 Range.protect()
来为恰好为命名范围的 Range
创建保护,而不调用 setRangeName(rangeName)
是不够的。不过,如果通过 Google 表格界面中的命名范围创建受保护的范围,则系统会自动关联这些范围。
// 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
- 表示保护设置的对象,用于串联
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 表示保护设置的对象,用于串联
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
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
- 表示需要保护的保护设置的对象。
授权
使用此方法的脚本需要获得以下一个或多个范围的授权:
-
https://www.googleapis.com/auth/spreadsheets.currentonly
-
https://www.googleapis.com/auth/spreadsheets