Sub topic 7
Leaving input cells editable on a protected sheet
This chapter assumes you can protect a whole sheet from the interface and understand the difference between file access levels and protection editors. It also assumes you can read basic JavaScript for the Apps Script section. Here you will learn how to protect a sheet and still leave chosen cells open to everyone who has edit access to the file.
Why use exceptions
A common design is a sheet that is mostly formulas, labels, and headers, with a few cells where people type values. You could protect each formula region as a separate range. That approach is fragile, because every new region you add is unprotected by default.
The better approach is to invert the design. Protect the whole sheet, then carve out the cells people may edit. A new row of formulas or a new label then starts out protected, and you open up only the cells you name.
In Google Sheets this carve-out is called an unprotected range (the interface labels it "Except certain cells"). It is an exception list attached to a sheet protection.
How the exception rule works
Three rules explain nearly all the behavior.
- The protection applies to every cell on the sheet except the ranges on the exception list.
- A cell on the exception list can be edited by anyone who has edit access to the spreadsheet file. The protection's editor list does not apply to it.
- Cells that are not on the list can be edited only by the protection's editors (or the file owner), or they trigger a warning if the protection is warning-only.
The diagram shows how Sheets decides whether an edit goes through.
The first decision matters. An exception never grants file access. A viewer or commenter cannot edit an unprotected cell, because they cannot edit anything.
Setting exceptions in the interface
- Open the spreadsheet and select the sheet tab you want to protect.
- Choose Data > Protect sheets and ranges. The side panel opens.
- Click Add a sheet or range, then select the Sheet tab in the panel.
- Pick the sheet from the drop-down list.
- Select Except certain cells.
- Click Add another range and type the range in A1 notation, for example
B2:B20. You can also click the grid icon and select cells with the mouse. - Repeat for each input region.
- Click Set permissions, choose who can edit, and click Done.
You can edit the list later. Open the same panel, click the protection, and change the ranges under Except certain cells.
Worked example: a budget sheet
Suppose a sheet named Budget has this layout.
| Range | Content | Who should edit it |
|---|---|---|
| A1:D1 | Column headers | Only you |
| A2:A20 | Category names | Only you |
| B2:B20 | Planned amounts (input) | Any editor |
| C2:C20 | Actual amounts (input) | Any editor |
| D2:D20 | Difference formulas | Only you |
| A22:D22 | Totals | Only you |
You protect the whole Budget sheet and add B2:C20 as the exception. Set the permission to Only you. Now every collaborator with edit access can type in the two input columns. Only you can change the headers, categories, formulas, and totals.
If a collaborator inserts a row inside the exception range, the new row usually becomes part of the range, because the range expands. If they insert a row just outside it, for example below row 20, the new row is protected and they cannot type in it. Plan for this when you expect the table to grow (see the edge cases below).
Creating exceptions with Apps Script
The method setUnprotectedRanges() on a Protection object takes an array of Range objects. This is the same behavior as the interface option.
function protectSheetExceptInputs() {
const sheet = SpreadsheetApp.getActive().getSheetByName('Budget');
const protection = sheet.protect().setDescription('Budget sheet');
protection.setUnprotectedRanges([sheet.getRange('B2:B20')]);
}Note that this script calls sheet.protect() and nothing else about editors. A new protection created by a script starts with the script user and the file owner as editors. Other collaborators cannot edit protected cells, but they can edit B2:B20.
To open several input regions, pass several ranges.
function protectBudgetWithTwoInputs() {
const sheet = SpreadsheetApp.getActive().getSheetByName('Budget');
const protection = sheet.protect().setDescription('Budget sheet');
protection.setUnprotectedRanges([
sheet.getRange('B2:B20'),
sheet.getRange('C2:C20')
]);
// Make sure only the script user and owner can edit the rest.
protection.removeEditors(protection.getEditors());
protection.addEditor(Session.getEffectiveUser());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}Two details are easy to miss.
setUnprotectedRanges()replaces the whole list. It does not append. To add one range, read the current list withgetUnprotectedRanges(), add to it, and pass the combined array back.- The ranges you pass must be on the same sheet as the protection. A range from another sheet is not accepted.
The following function appends one range safely.
function addInputRange(sheetName, a1Notation) {
const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
const protections = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET);
if (protections.length === 0) {
throw new Error('No sheet protection found on ' + sheetName);
}
const protection = protections[0];
if (!protection.canEdit()) {
throw new Error('You cannot edit this protection.');
}
const current = protection.getUnprotectedRanges();
current.push(sheet.getRange(a1Notation));
protection.setUnprotectedRanges(current);
}A sheet has at most one sheet-level protection, so taking the first item of the result is safe here.
Reading exceptions back
When you audit a spreadsheet, list the exceptions along with each sheet protection. This shows you which cells are open.
function listUnprotectedRanges() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getSheets().forEach(sheet => {
sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET).forEach(p => {
const open = p.getUnprotectedRanges()
.map(r => r.getA1Notation())
.join(', ');
Logger.log('%s | %s | open: %s',
sheet.getName(),
p.getDescription(),
open || '(none)');
});
});
}Edge cases and common errors
Exceptions are fixed A1 ranges
An unprotected range is tied to cell positions, not to the meaning of the data. Several situations follow from that.
- Rows added at the end of a table. Inserting a row below the last row of the exception does not extend it. People will see a "You are trying to edit a protected cell" message. Either leave a spare buffer of unprotected rows, or use an open-ended range such as
B2:B1000. - Sorting. Sorting a range that includes both protected and unprotected cells is blocked for non-editors. Ask people to sort only inside an exception range, or give them a filter view, which does not change the data order for others.
- Deleting rows. A user who can edit only some cells in a row cannot delete the whole row, because the delete would touch protected cells. If you want people to remove input rows, they must be protection editors.
- Formulas that reference input cells. Formulas in protected cells still recalculate when an input changes. Protection restricts who types, not what calculates.
A user sees both a lock and a working cell
The lock icon appears on the sheet tab even when most cells are editable. Collaborators may think the whole sheet is locked. Add a short note on the sheet, or color the input cells, so people know where to type.
Editors and exceptions do not combine the way people expect
Exceptions grant edit rights to everyone with file edit access. You cannot say "this exception is only for the finance group". If different people need different input cells, use a range protection per region, each with its own editor list, instead of one sheet protection with exceptions.
Overlapping protections
If a range protection covers cells that are also in the exception list of a sheet protection, the range protection still applies. Both protections must allow the edit. Use this when most of the exception is open but one cell inside it should be restricted.
Script changes need the right user
setUnprotectedRanges() fails if the user running the script is not allowed to edit the protection. Use canEdit() before you change a protection, as the addInputRange function above does. This is especially important in a time-driven trigger or a shared add-on, where the running user may not be the owner.
Equivalent Sheets API request
In the Sheets API, a sheet protection is a protectedRange with a range that has only a sheetId, and the exceptions are listed in unprotectedRanges. Indexes are zero-based and end indexes are exclusive, so B2:B20 is row indexes 1 to 20 and column indexes 1 to 2.
{
"requests": [
{
"addProtectedRange": {
"protectedRange": {
"range": {
"sheetId": 0
},
"description": "Budget sheet",
"warningOnly": false,
"unprotectedRanges": [
{
"sheetId": 0,
"startRowIndex": 1,
"endRowIndex": 20,
"startColumnIndex": 1,
"endColumnIndex": 2
}
],
"editors": {
"users": ["[email protected]"]
}
}
}
}
]
}Leaving the row and column indexes out of range means the whole sheet is covered. This is the API form of "protect the sheet".
Summary of the design
- Protect the sheet, then list the input cells as exceptions. New content is then protected by default.
- Make the exception ranges generous enough to absorb added rows, or document how to extend them.
- Remember that exceptions open cells to all file editors. For per-person control, use range protections.
- Audit exceptions with a script, and check them whenever the layout changes.
Exercise
A sheet named Orders is protected with the exception B2:B50, and the protection editors are only you. A collaborator with edit access to the file inserts a new row at row 51 and tries to type a quantity in B51. What happens, and what are two ways to prevent it?
Show answer
The collaborator is blocked from editing B51, because the exception ends at row 50 and the new row is part of the protected area. One fix is to widen the exception to a larger range such as B2:B1000 so added rows fall inside it. Another fix is to ask a protection editor (you) to update the exception list after the table grows, for example with the addInputRange function.
Exercise
Write an Apps Script function openColumn(sheetName, a1Notation) that replaces the exception list of the sheet protection on the named sheet with exactly one range. Explain why setUnprotectedRanges is not an "add" method.
Show answer
Example:
function openColumn(sheetName, a1Notation) {
const sheet = SpreadsheetApp.getActive().getSheetByName(sheetName);
const protection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET)[0];
protection.setUnprotectedRanges([sheet.getRange(a1Notation)]);
}
setUnprotectedRanges sets the whole list to the array you pass. Any range that was previously on the list but is not in the new array becomes protected again, so to add a range you must read the current ranges with getUnprotectedRanges() and include them in the new array.
Exercise
Two teams share one sheet. Team A must type only in column B and Team B only in column C. Can one sheet protection with exceptions B2:C20 do this? What should you use instead?
Show answer
No. An exception opens cells to everyone with file edit access, so both teams could edit both columns. Use two range protections instead, one on B2:B20 with Team A as editors and one on C2:C20 with Team B as editors, and protect the rest of the sheet with a separate sheet protection or additional range protections.