Sub topic 2
How file access, ownership, and protection editors relate
This chapter assumes you know how to share a spreadsheet with viewers, commenters, and editors, and that you can read A1 notation. A few sections use Apps Script, so basic JavaScript helps. The chapter explains the permission model underneath protection. Later chapters cover the menus and the code.
Two layers of permission
Google Sheets decides whether you can change a cell by checking two separate things.
- File access. This is your role on the whole spreadsheet: owner, editor, commenter, or viewer. It is set with the Share dialog.
- Protection settings. These are rules attached to a range or a sheet. Each rule has its own list of people who may edit that area.
File access is the outer gate. Protection is an inner gate that can only make things stricter. A protection never gives someone more access than they have to the file.
File access levels
The four file roles behave like this with respect to protection.
| File role | Can change cells in unprotected areas | Can change cells in protected areas | Can create or change protections |
|---|---|---|---|
| Owner | Yes | Yes, always | Yes |
| Editor | Yes | Only if listed as a protection editor | Yes, with caveats described below |
| Commenter | No | No | No |
| Viewer | No | No | No |
Commenters and viewers are blocked by the file role alone. Protection matters only for people who already have edit access. Every protection exists to answer one question: of all the people who can edit this file, who can edit this particular area?
The owner
The owner is the account that owns the file. In a shared drive, the file belongs to the drive rather than to a person, and organization managers take on the equivalent role.
The owner has two properties that no protection can take away.
- The owner can always edit a protected range or sheet. Removing the owner from a protection's editor list does not lock them out.
- The owner can always remove or change any protection.
This is why the first Apps Script example in the topic comments that editing is restricted to "the user running the script (and the file owner)". The owner stays in regardless of what the script does.
If the owner is someone who has left your organization, or an account you cannot reach, you may not be able to adjust protections that you need to change. Transfer ownership of important shared files before that happens.
Protection editors
Every protection has its own editor list. This list is separate from the Share dialog. Two files with identical sharing can have very different protection editors on each range.
When you create a protection, the interface and the API offer three kinds of setting.
- Only you. Only the person creating the protection, plus the owner, can edit the area.
- Only domain. Anyone in your organization who has file edit access can edit the area. This option appears only for Google Workspace accounts.
- Custom. You pick a specific list of people. Each person you pick must already be an editor of the file.
A fourth mode, warning-only, replaces blocking with a confirmation prompt. It is covered in a later chapter. For now, note that it does not use an editor list the same way, because nobody is blocked.
Who counts as a protection editor
A person can edit a protected area if any of these is true.
- They are the file owner.
- They are named in the protection's editor list.
- The protection allows domain editing and they belong to that domain with file edit access.
- The protection is warning-only. They can edit after dismissing the warning.
Everyone else with file edit access sees the area as locked.
Who may change a protection
Being able to edit a protected area and being able to change the protection itself are related, but they are not identical. The rules are:
- Editors of a protection can edit the protected cells.
- A person with file edit access can generally open the protection list and see what is protected.
- Changing or removing a protection requires permission to edit that protection, which is the same as being in its editor list or being the owner.
Apps Script exposes this directly. The Protection class has canEdit(), which returns whether the current user may edit the protected area, and canDomainEdit(), which tells you if domain-wide editing is available. Scripts that remove protections should check canEdit() first, as the fifth example in the topic does. Without that check, the call to remove() can throw an exception for protections you cannot change.
The user in a script
A script runs as a particular account. Which account depends on how the script was started.
- A script run manually from the editor runs as the person who started it.
- A script that runs from an installable trigger runs as the person who created the trigger.
- A web app or add-on can be set to run as the visitor or as the developer.
Session.getEffectiveUser() returns the account the script is running as. Session.getActiveUser() returns the person at the keyboard, and in some contexts it returns an empty value. For protection, the effective user is usually the account that matters, because it is the one whose permissions the script exercises.
The header-row example makes this concrete.
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);
}
}Read it in terms of the two layers.
range.protect()creates the protection. Initially the person who runs the script is an editor.removeEditors(protection.getEditors())clears the editor list. The owner is not affected, since the owner is always allowed.addEditor(Session.getEffectiveUser())puts the running account back. This step is needed when the account running the script differs from the one that was removed, or to make the intent explicit.setDomainEdit(false)turns off the organization-wide option, which would otherwise let everyone in the domain edit the header.
The last step is guarded by canDomainEdit() because that option is unavailable for consumer Gmail accounts. Calling setDomainEdit where it does not apply is a mistake the guard avoids.
What happens when the file role changes
Protection editor lists and file roles are stored separately, so changes to one do not always update the other. Several situations follow from this.
- Demoting an editor. If you change a person from editor to viewer, they can no longer edit anything. Their name may remain on protection editor lists, but it has no effect while they are a viewer.
- Promoting a viewer. If you later make them an editor again, any protection that still lists them lets them edit those areas. Review editor lists whenever you promote someone.
- Adding an editor. A new file editor is not added to existing custom protections. They can edit unprotected areas only. This is usually what you want, but it surprises people who expect new collaborators to reach everything.
- Removing a person from the file. They lose access to the file. Stale names in protection lists are harmless but make audits noisier.
How the two layers combine
The following diagram shows the decision Sheets makes when someone tries to edit a cell.
Two details are worth remembering. A cell can sit inside more than one protection, for example a range protection on A1:E1 inside a protected sheet. In that case the person generally needs to be allowed by each protection that covers the cell. Also, a sheet protection with unprotected ranges excludes those ranges from the sheet's rule, which a later chapter covers.
Common errors
The following mistakes come from mixing up the two layers.
- "I added her to the protection but she still cannot edit." Check her file role. If she is a viewer or commenter, protection will not help. Change her role in the Share dialog first.
- "I removed myself from the protection, so I am locked out." If you are the owner, you are not. If you are an editor, you are locked out of the protected area and need the owner or another protection editor to restore you.
- "The script throws when removing a protection." The running account is probably not an editor of that protection. Check
canEdit()before callingremove(). - "New teammates can edit the header." The header protection probably allows domain editing. Turn that off and use a custom list.
- "The script ran as the wrong user." Triggers run as the account that created them. Log
Session.getEffectiveUser().getEmail()to confirm.
A short checklist
Use these questions when you set up protection on a shared file.
- Who owns the file, and is that account reliably reachable?
- Does each person who needs to edit protected cells have edit access to the file?
- Should the protection editor list be only you, the domain, or a custom list?
- Will someone be added or removed from the file soon, and does that affect any protection list?
- If a script manages the protection, which account does it run as?
Exercise
A teammate has Commenter access to a budget spreadsheet. You add her email as a custom editor on a protected range containing the totals. Can she edit the totals? Explain which layer decides, and name the smallest change that would let her edit them.
Show answer
No. File access is checked first, and a commenter cannot edit any cell. Being on the protection's editor list does not raise her file role, because protection can only restrict, never grant. The smallest change is to raise her file role to Editor in the Share dialog. Because she is already on the protection's editor list, she can then edit the totals.
Exercise
Your script calls protection.removeEditors(protection.getEditors()) on a range protection and then runs as an editor who is not the owner. After the script finishes, who can still edit the range?
Show answer
The file owner always can. In the header-row example the script adds the effective user back with addEditor(Session.getEffectiveUser()), so that account can edit as well. If the script does not add anyone back, only the owner can edit, and the account that ran the script is locked out of that range.