Sub topic 4
Choosing who can edit a protected range
This chapter assumes you can protect a range from Data > Protect sheets and ranges, which the previous chapter covered. It also assumes you know the Viewer, Commenter and Editor access levels for a file. The Apps Script section needs basic JavaScript.
By the end you will be able to pick the right permission setting for a protected range. You will also know why a name is sometimes missing from the editor list, and how to set the same list from a script.
Two layers of permission
A protected range has its own list of people who may change it. That list is separate from the file's sharing settings, but it is limited by them.
- File access decides whether someone can edit the spreadsheet at all. Viewers and commenters can never edit cells, protected or not.
- Protection editors decide which of the people who already have edit access may change the protected cells.
The protection list can only narrow access. It cannot give edit rights to someone who has none. If you add [email protected] to a protection but that person only has Viewer access to the file, they still cannot edit the range.
The permission options in the interface
Select a range, open Data > Protect sheets and ranges, add a description if you like, and choose Set permissions. The dialog offers two modes:
- Show a warning when editing this range is the warning-only mode. It is covered in its own chapter, so this chapter only covers the restrictive mode.
- Restrict who can edit this range blocks edits by anyone not on the list.
Under the restrictive mode you pick one of these:
| Option | Who can edit the range | Typical use |
|---|---|---|
| Only you | You, plus the file owner | Headers, formulas you maintain yourself |
| Only domain (Workspace accounts only) | Everyone in your organization who has edit access to the file | Keep outside collaborators out of a range |
| Custom | Only the people you tick | A named team or a few approvers |
A fourth choice, Copy permissions from another range, loads the list from an existing protection. It saves time when many ranges share the same audience.
What "Only you" means
"Only you" is the default for a new restricted protection. The creator is on the list. The file owner can always edit protected ranges, even if the owner is not listed. This has two consequences:
- If you are not the owner, the owner can still change your protected cells.
- If you want a range that truly nobody except the owner edits, leave the list as the owner alone. You cannot lock the owner out.
Custom lists
In Custom mode, Sheets shows the people who currently have edit access to the file. You tick the ones who may edit the range. People with Viewer or Commenter access do not appear, because they cannot edit anyway.
Some practical points:
- Sharing changes feed through. If you remove someone's edit access to the file, they can no longer use any protection that lists them. Their name may remain on the list or disappear, depending on the interface. Either way the file permission is what stops them.
- Link-based editing is blunt. If the file is shared as "Anyone with the link can edit", anonymous users cannot be named on a protection list. A restricted range blocks them, so only listed named editors get through. This is usually what you want.
- Groups are not a list entry in the dialog. You pick individual accounts. To manage access by team, use Apps Script or the API and keep the email list in one place.
Who can change the protection itself
People on the editor list can edit the cells. They can also open the protection and change its settings, or delete it. Adding a colleague to a range therefore trusts them with the rule as well as the data. If someone should type values but never change the rules, do not list them on that range. Put the input cells on a protected sheet as an unprotected exception instead (a later chapter covers this).
A person on the list who is not the owner cannot remove the owner. They can remove other editors from the list, though.
Worked example: a header row only you can edit
Suppose a shared tracker has headers in A1:E1. You want only yourself, plus the owner, to change them.
Using the interface:
- Select
A1:E1. - Choose Data > Protect sheets and ranges > Add a sheet or range.
- Enter the description
Header row. - Choose Set permissions, then Restrict who can edit this range, then Only you.
- Choose Done.
Anyone else with edit access now sees an error if they try to type in those cells. They can still edit every other cell.
To widen it to a team, return to Set permissions and choose Custom. Tick the two people who maintain the headers and choose Done.
Setting editors with Apps Script
Apps Script exposes the list through the Protection class. The first example removes every editor and adds back the user running the script.
function protectHeaderRow() {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getRange('A1:E1');
const protection = range.protect().setDescription('Header row');
// Restrict editing to the user running the script (and the file owner).
protection.removeEditors(protection.getEditors());
protection.addEditor(Session.getEffectiveUser());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}Each step does a specific job:
range.protect()creates the protection. A new protection starts with the creator, the owner, and (in a Workspace domain) possibly the whole domain able to edit.getEditors()returns the current list asUserobjects, andremoveEditors()takes that list and removes it. The file owner cannot be removed, so the owner stays.addEditor()adds one person back. It accepts an email string or aUser.canDomainEdit()reports whether domain-wide editing is available.setDomainEdit(false)turns it off so that being in the same domain no longer grants access.
The order matters. If you removed editors after adding the current user, you would remove yourself too, and a later script run by a different person could fail.
Granting a named team
To give a fixed team edit access, collect their addresses in an array and call addEditors():
function protectForTeam() {
const teamEmails = ['[email protected]', '[email protected]'];
const sheet = SpreadsheetApp.getActiveSheet();
const protection = sheet.getRange('A1:E1').protect()
.setDescription('Header row - team editable');
protection.removeEditors(protection.getEditors());
protection.addEditors(teamEmails);
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}Two failure modes to expect:
- Addresses without edit access. Adding someone who has only Viewer access to the file does not raise an error that helps you. The person simply cannot edit. Share the file with them as an editor first, then add them.
- The running user lacks rights. Only people who can edit a protection may change its list. Calling
removeEditorsas someone outside the list fails. Test withprotection.canEdit()before changing a protection you did not create.
Checking who is on the list
Before changing a list, read it. This example logs the current editors of every range protection on the active sheet:
function logEditors() {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE).forEach(p => {
const emails = p.getEditors().map(user => user.getEmail());
Logger.log('%s -> %s', p.getDescription(), emails.join(', '));
});
}getEmail() can return an empty string for some users, depending on domain privacy settings, so do not use it as a unique key without checking.
The same restriction through the Sheets API
The Sheets API sets the list in the editors field of a protectedRange object. This spreadsheets.batchUpdate body protects A1:E1 on the sheet with ID 0 and lists one allowed user:
{
"requests": [
{
"addProtectedRange": {
"protectedRange": {
"range": {
"sheetId": 0,
"startRowIndex": 0,
"endRowIndex": 1,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"description": "Header row",
"warningOnly": false,
"editors": {
"users": ["[email protected]"]
}
}
}
}
]
}Note that row and column indexes are zero-based and the end indexes are exclusive, so endRowIndex: 1 means row 1 only. The editors object also accepts groups and a domainUsersCanEdit flag. The API therefore does what the dialog cannot: it can list a Google group directly. warningOnly must be false here, because a warning-only protection ignores the editor list.
Common mistakes
- Expecting a protection to grant access. It cannot. Fix the file's sharing first.
- Forgetting the owner. The owner can always edit, so a protection is never a barrier against the owner.
- Leaving domain editing on. In a Workspace domain, a range that looks restricted may still be editable by every colleague with file access. Check the domain option.
- Listing too many people on the protection. Everyone on it can also change or delete the rule. Keep the list short.
- Mixing up the two modes. Warning-only mode has no editor list. Switching a range from restricted to warning-only removes the restriction.
Summary
- Protection editors are a subset of the file's editors. They narrow access and never widen it.
- The file owner always keeps access.
- "Only you", "Only domain" and "Custom" cover most cases in the interface. Apps Script and the API add scripted lists and groups.
- Everyone on the list can change the protection itself, so keep the list small.
Exercise
A spreadsheet is shared with Dana as a Commenter. You protect B2:B20 with a Custom list. In a script you call protection.addEditor('[email protected]'). Can Dana now edit B2:B20? What would you change, and in what order would you do it so the range remains restricted to the intended people?
Show answer
No. Dana still has only Commenter access to the file, and a protection can only narrow access among people who can already edit. To let her edit the range, first change her file access to Editor. Then add her to the protection's editor list. The protection stays restricted throughout, because the range was already limited to its list. If the script's account is not on the list, check protection.canEdit() first.
Exercise
You run a script that calls protection.addEditor(Session.getEffectiveUser()) and then protection.removeEditors(protection.getEditors()). What is the likely problem, and how do you fix it?
Show answer
The second call removes every editor, including the user just added. The creator can then lose the ability to change the protection later. Call removeEditors(protection.getEditors()) first, then addEditor(...). Also call setDomainEdit(false) when canDomainEdit() is true, so domain membership does not grant access.