Sub topic 10
Listing, Auditing, and Removing Protections
Assume you know basic Google Sheets concepts, A1 notation, and JavaScript. This chapter uses Apps Script to inspect protections, review their settings, and remove protections safely. It does not cover authentication or deploying a web app.
A protection is a rule attached to a range or an entire sheet. An audit checks where those rules exist, who can edit them, whether they only show warnings, and whether exceptions exist. Auditing matters because protections can remain after a spreadsheet’s design or team has changed.
List range and sheet protections
Open the spreadsheet, select Extensions > Apps Script, and run a function from the script editor. The first run may request permission to access the spreadsheet.
getProtections() requires a protection type. Use RANGE for protected cell ranges and SHEET for protected sheet tabs.
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(protection => {
const range = protection.getRange();
Logger.log(
'sheet=%s | range=%s | description=%s | warningOnly=%s',
sheet.getName(),
range.getA1Notation(),
protection.getDescription(),
protection.isWarningOnly()
);
});
});
}After the function finishes, open Executions or choose View > Logs, depending on the Apps Script editor version. Each log entry identifies the sheet, location, description, and warning-only setting.
A sheet protection covers the sheet tab. Its range can therefore represent the sheet rather than a small cell selection. For a more useful audit, also inspect its unprotected ranges.
Inspect editors and exceptions
A protection can restrict editing to selected users, or it can be warning-only. getEditors() returns the users or groups that the protection allows to edit. canDomainEdit() reports whether everyone in the organization’s domain can edit it. These settings describe protection permissions, not the spreadsheet’s general sharing permissions.
For a sheet protection, getUnprotectedRanges() returns the ranges that remain editable under that protection.
function auditProtections() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
ss.getSheets().forEach(sheet => {
const protections = []
.concat(sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE))
.concat(sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET));
protections.forEach(protection => {
const editors = protection.getEditors()
.map(user => user.getEmail())
.join(', ');
const exceptions = protection.getUnprotectedRanges()
.map(range => range.getA1Notation())
.join(', ');
Logger.log(JSON.stringify({
sheet: sheet.getName(),
range: protection.getRange().getA1Notation(),
description: protection.getDescription(),
warningOnly: protection.isWarningOnly(),
editors: editors,
domainEdit: protection.canDomainEdit(),
unprotectedRanges: exceptions,
canCurrentUserEdit: protection.canEdit()
}));
});
});
}canEdit() is important when a script is used by several administrators. A user may be able to view a protection but not change or remove it. Do not treat a successful listing as proof that the current user can administer every protection.
| Audit field | What it tells you |
|---|---|
| `description` | Why the protection exists |
| `warningOnly` | Whether edits are blocked or only confirmed |
| `editors` | Accounts explicitly allowed to edit |
| `domainEdit` | Whether the whole domain can edit |
| `unprotectedRanges` | Input areas excluded from a sheet protection |
| `canCurrentUserEdit` | Whether the running user can change the protection |
Remove a protection safely
Remove a protection only when its description and location identify it clearly. This example removes matching range protections on the active sheet and skips protections that the current user cannot edit.
function removeProtectionsByDescription(description) {
const sheet = SpreadsheetApp.getActiveSheet();
sheet
.getProtections(SpreadsheetApp.ProtectionType.RANGE)
.forEach(protection => {
if (
protection.getDescription() === description &&
protection.canEdit()
) {
protection.remove();
}
});
}Call it with a precise description, such as:
function removeHeaderProtection() {
removeProtectionsByDescription('Header row');
}This function does not remove sheet protections. That distinction is deliberate. If you need to remove a sheet protection, retrieve ProtectionType.SHEET, confirm its description, and call remove() only after checking canEdit().
A safer operational pattern is:
- List and record all protections.
- Identify the exact sheet, range, and description.
- Confirm that the protection is obsolete.
- Remove only the matching protection.
- Run the audit again and review the result.
Exercise
Exercise
Why should a removal script check both the protection description and canEdit() before calling remove()?
Show answer
The description narrows the operation to the intended protection, while canEdit() prevents the script from attempting to change a protection that the current user cannot administer.