Sub topic 9
Creating protections with Apps Script
Assume you already know cells, ranges, sheet tabs, and A1 notation. You should also know basic JavaScript, including functions, constants, arrays, and loops. Apps Script is Google's JavaScript-based automation environment for Google Workspace.
Apps Script can create the same protections that you create through the Google Sheets interface. It can protect a range, protect a whole sheet, leave selected ranges editable, show warnings, and manage existing protections.
How a protection works in a script
A protection is an object that controls editing for a range or sheet. The script first gets a Range or Sheet, then calls protect().
function protectHeaderRow() {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getRange('A1:E1');
const protection = range.protect().setDescription('Header row');
protection.removeEditors(protection.getEditors());
protection.addEditor(Session.getEffectiveUser());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}The script protects A1:E1, gives the protection a description, removes its current editors, and adds the user running the script. The spreadsheet owner may still retain administrative control.
The first time you run a function that changes protections, Apps Script asks for authorization. The account running the script must have enough permission to change the spreadsheet and its protections.
Protecting a sheet with input exceptions
A sheet protection can cover an entire tab while allowing specific ranges to remain editable. This pattern is useful for forms, budgets, and tracking sheets where formulas are locked but input cells remain available.
function protectSheetExceptInputs() {
const sheet = SpreadsheetApp
.getActiveSpreadsheet()
.getSheetByName('Budget');
if (!sheet) {
throw new Error('The Budget sheet does not exist.');
}
const protection = sheet
.protect()
.setDescription('Budget sheet');
const editableInputs = sheet.getRange('B2:B20');
protection.setUnprotectedRanges([editableInputs]);
}setUnprotectedRanges() accepts an array of ranges. You can provide more than one exception:
protection.setUnprotectedRanges([
sheet.getRange('B2:B20'),
sheet.getRange('D2:D20')
]);These ranges are exceptions to the sheet protection. They do not grant access to people who only have viewer or commenter access to the file. A person must already have permission to edit the spreadsheet.
Warning-only protection
Sometimes you want to discourage changes without blocking them. A warning-only protection displays a confirmation prompt when an editor changes the protected range.
function warnOnCalculatedColumn() {
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getRange('D2:D100');
range.protect()
.setDescription('Calculated column')
.setWarningOnly(true);
}Use warning-only protection for areas where accidental edits are likely but legitimate exceptions are common. Use restricted protection when changing the cells would damage formulas, records, or workflow state.
Listing and removing protections
Descriptions make protections easier to audit. This function logs every range and sheet protection on every tab.
function listProtections() {
const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
spreadsheet.getSheets().forEach(sheet => {
const ranges = sheet.getProtections(
SpreadsheetApp.ProtectionType.RANGE
);
const wholeSheet = sheet.getProtections(
SpreadsheetApp.ProtectionType.SHEET
);
ranges.concat(wholeSheet).forEach(protection => {
Logger.log(
'%s | %s | %s | warningOnly=%s',
sheet.getName(),
protection.getRange().getA1Notation(),
protection.getDescription(),
protection.isWarningOnly()
);
});
});
}View the output in the Apps Script execution log. When changing an existing protection, first check that the current user can edit it.
function removeProtectionsByDescription(description) {
const sheet = SpreadsheetApp.getActiveSheet();
sheet.getProtections(SpreadsheetApp.ProtectionType.RANGE)
.forEach(protection => {
if (
protection.getDescription() === description &&
protection.canEdit()
) {
protection.remove();
}
});
}Run management functions carefully. A broad removal loop can delete protections that another person depends on. Use precise descriptions and test on a copy before applying a script to a shared production spreadsheet.
Exercise
Exercise
Write a function that protects C2:C50 on the active sheet, gives the protection the description "Review status", and shows a warning instead of blocking edits.
Show answer
function protectReviewStatus() { const range = SpreadsheetApp.getActiveSheet().getRange('C2:C50'); range.protect() .setDescription('Review status') .setWarningOnly(true); }