Sub topic 10
Listing, auditing, and removing protections
Protections build up over time. Someone protects a header row, someone else locks a whole sheet for a month-end close, and a year later nobody remembers why a range refuses edits. This chapter covers how to find every protection in a spreadsheet, review each one against a checklist, and remove the ones that no longer belong.
It assumes you can create protections in the interface and with range.protect() and sheet.protect() in Apps Script. It also assumes you know the difference between a protection's editors and the file's access levels. You need basic JavaScript for the code sections.
Why audit protections
A protection is a rule attached to a range or sheet. Nothing in the grid shows how many rules exist or whether they still make sense. Three problems come up in practice:
- Stale rules. A protection on a range that no longer holds anything important still blocks collaborators.
- Overlapping rules. Two protections can cover the same cells with different editor lists. Someone must satisfy every protection covering a cell to edit it, so the strictest list wins. This is hard to see from the grid.
- Unknown owners. A protection created by a person who has left the team may list editors who no longer need access.
An audit answers four questions for each protection: what does it cover, who can edit it, is it enforced or warning-only, and is it still needed.
Listing protections in the interface
Open Data > Protect sheets and ranges. The sidebar lists every protection you can see, with its description and range. It has these parts:
- Click an entry to see its range, editor settings, and any unprotected exceptions.
- The Sheet and Range groupings in the list tell you which kind of protection each entry is.
- The trash icon on an entry's detail view removes that protection.
The sidebar is fine for a handful of rules. It becomes slow with dozens of protections across many tabs, because it shows one sheet at a time and does not show editor lists in the overview. For anything larger, use a script.
Listing protections with Apps Script
Protections belong to sheets, not to the spreadsheet as a whole. Spreadsheet has no getProtections() method, so you loop over the sheets and call sheet.getProtections(type) once for each protection type. SpreadsheetApp.ProtectionType.RANGE returns range protections, and SpreadsheetApp.ProtectionType.SHEET returns sheet protections.
This is the basic listing function:
function listProtections() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getSheets().forEach(sheet => {
const rangeProtections = sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE);
const sheetProtections = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET);
rangeProtections.concat(sheetProtections).forEach(p => {
Logger.log('%s | %s | %s | warningOnly=%s',
sheet.getName(),
p.getRange().getA1Notation(),
p.getDescription(),
p.isWarningOnly());
});
});
}For a sheet protection, getRange() returns the range covering the whole sheet. You cannot tell a sheet protection from a range protection that happens to cover everything unless you remember which loop produced it. The next section tracks the type explicitly.
Methods worth auditing
| Method | What it tells you |
|---|---|
| `getProtectionType()` | `RANGE` or `SHEET` |
| `getRange()` | The protected cells |
| `getRangeName()` | The named range the protection is tied to, or `null` |
| `getDescription()` | The label someone gave it, often empty |
| `isWarningOnly()` | Whether it warns instead of blocking |
| `getEditors()` | Users allowed to edit (meaningful only when not warning-only) |
| `canDomainEdit()` | Whether the whole domain could be given edit rights |
| `getUnprotectedRanges()` | Exceptions on a sheet protection |
| `canEdit()` | Whether the user running the script may change or remove it |
Building an audit report
Logs disappear. For a review that several people can read, write the results into a dedicated sheet. This function builds one row per protection and writes the rows in a single call:
function auditProtections() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const rows = [[
'Sheet', 'Type', 'Range', 'Description', 'Mode',
'Editors', 'Unprotected ranges', 'I can edit'
]];
ss.getSheets().forEach(sheet => {
[SpreadsheetApp.ProtectionType.RANGE, SpreadsheetApp.ProtectionType.SHEET]
.forEach(type => {
sheet.getProtections(type).forEach(p => {
const isSheet = type === SpreadsheetApp.ProtectionType.SHEET;
const warningOnly = p.isWarningOnly();
rows.push([
sheet.getName(),
isSheet ? 'Sheet' : 'Range',
isSheet ? '(entire sheet)' : p.getRange().getA1Notation(),
p.getDescription() || '(no description)',
warningOnly ? 'Warning only' : 'Restricted',
warningOnly ? '' : p.getEditors().map(u => u.getEmail()).join(', '),
isSheet ? p.getUnprotectedRanges().map(r => r.getA1Notation()).join(', ') : '',
p.canEdit()
]);
});
});
});
const name = 'Protection audit';
const report = ss.getSheetByName(name) || ss.insertSheet(name);
report.clearContents();
report.getRange(1, 1, rows.length, rows[0].length).setValues(rows);
report.setFrozenRows(1);
}Some details in this function matter:
- Every row has the same number of columns.
setValuesthrows if the rows are uneven. getUnprotectedRanges()is called only for sheet protections, which is where exceptions live.- The editor list is blank for warning-only protections. A warning-only protection does not restrict anyone, so an editor list would suggest control that does not exist.
- The report is itself a sheet. If you later protect it, it appears in the next run of the audit.
What to look for in the report
Sort the report by range and read it against this checklist:
- Empty descriptions. A protection without a description is hard to justify later. Add one before you decide anything else.
- Overlaps. Two rows on the same sheet with intersecting ranges need a deliberate reason. Merge them or remove the weaker one.
- Warning-only rules on critical cells. A warning prompt is easy to click through. If the cells matter, switch to a restricted protection.
- Editor lists with departed people. Compare the email column against the current team.
- Sheet protections with a long exception list. If the exceptions are most of the sheet, you probably want range protections on the few locked cells instead.
- Domain editing. Check
canDomainEdit()on any protection that should stay with named individuals.
Removing protections
Calling protection.remove() deletes a protection. For a sheet protection, this also drops its unprotected exceptions. The cells and their values are untouched, and only the rule disappears.
remove() throws an exception if you lack permission. You can edit a protection if you are the file owner or if you are on the protection's editor list, so check canEdit() first. This function removes range protections on the active sheet whose description matches:
function removeProtectionsByDescription(description) {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE).forEach(p => {
if (p.getDescription() === description && p.canEdit()) {
p.remove();
}
});
}The canEdit() check silently skips protections you cannot remove, which can hide a problem. The next version covers every sheet, supports a dry run, and counts what it skipped:
function removeProtections(description, dryRun) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
let removed = 0;
let skipped = 0;
ss.getSheets().forEach(sheet => {
[SpreadsheetApp.ProtectionType.RANGE, SpreadsheetApp.ProtectionType.SHEET]
.forEach(type => {
sheet.getProtections(type).forEach(p => {
if (p.getDescription() !== description) return;
if (!p.canEdit()) {
skipped++;
return;
}
Logger.log('%s %s on "%s"',
dryRun ? 'Would remove' : 'Removing', type, sheet.getName());
if (!dryRun) p.remove();
removed++;
});
});
});
Logger.log('%s: %s, skipped (no permission): %s',
dryRun ? 'Dry run' : 'Done', removed, skipped);
}
function previewRemoval() {
removeProtections('Header row', true);
}Apps Script's editor can only run functions without arguments, which is why previewRemoval() exists as a wrapper. Run the preview first, read the log, then call removeProtections('Header row', false) from a wrapper of your own.
Removing a protection by editing its scope
Sometimes you do not want to remove a protection, only narrow it. Use setRange() on a range protection, or setUnprotectedRanges() on a sheet protection, instead of deleting and recreating it. This keeps the description and editor list intact.
Cleanup that happens automatically
Deleting a sheet deletes its protections. Deleting every row or column covered by a range protection removes that protection too. Deleting only some of them shrinks the protected range. This means an old protection can quietly cover fewer cells than when you made it. This is another reason to audit the actual range, not only the description.
Doing the same through the Sheets API
If you manage protections from outside Apps Script, the Sheets API exposes the same data. Reading uses spreadsheets.get with a field mask, so you receive only the protection data:
GET https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID?fields=sheets(properties(sheetId,title),protectedRanges)Each entry in protectedRanges has a protectedRangeId, a range in zero-based grid coordinates, a description, a warningOnly flag, and an editors object. A sheet protection appears with a range that has only a sheetId, and its exceptions appear in unprotectedRanges. Removal is a batchUpdate request using the ID:
{
"requests": [
{
"deleteProtectedRange": {
"protectedRangeId": 123456789
}
}
]
}The ID is the stable handle. In the API, you identify a protection by its ID rather than by its description, which makes it safer than the description match used in the script above. A sensible workflow is to read and review with the API or the audit sheet, collect the IDs of the protections to remove, then send one batchUpdate with one deleteProtectedRange per ID.
Scheduling the audit
A one-time audit goes stale. In Apps Script, open Triggers and add a time-driven trigger for auditProtections, for example monthly. Running it as a trigger means it runs as the user who created the trigger, so create it as the owner. You can extend the function to send the report's link with MailApp.sendEmail to the people who maintain the file. Keep the schedule modest, because each run reads every sheet.
Common errors
- "You do not have permission" on
remove(). The user running the script is not an editor of that protection. CheckcanEdit()or run as the owner. - Audit shows fewer protections than the sidebar suggests. Hidden sheets are still returned by
getSheets(), but protections you cannot see are not listed to you. Run as the owner. - Editor column looks populated for a warning-only protection. The list exists but is not enforced. Rely on
isWarningOnly()first. setValuesfails on dimension mismatch. Every row array must have the same length as the header.
Exercise
You run an audit and find three protections on the "Budget" sheet, all with the description "Locked". You want to remove only the one covering A1:E1. Why is removeProtections('Locked', false) a bad choice, and what should you do instead?
Show answer
It matches by description, and all three protections share it, so it would remove all three. Instead, do a dry run to see what matches, then select the protection by a more specific test, such as comparing p.getRange().getA1Notation() === 'A1:E1' together with canEdit(). Alternatively, collect the protectedRangeId for that one protection and send a deleteProtectedRange request through the Sheets API.
Exercise
A colleague runs your audit function on a shared file and the "I can edit" column shows false for most rows. What does this tell you, and does it affect the accuracy of the other columns?
Show answer
It tells you the colleague is neither the owner nor on the editor lists of those protections, so they can read them but not change or remove them. The other columns, such as range, description, and mode, still describe the protections correctly. The editor list is also shown. They should ask the owner or a listed protection editor to remove or change those rules.