Sub topic 3
What protection does not do
This chapter assumes you can share a Google Sheet, read A1 notation, and read basic JavaScript. You should also know that a protection applies to a range or a whole sheet, and that each protection has a list of editors.
What protection does not do
Protection limits who can change cells. It does not hide data, encrypt it, or stop viewers from reading or copying values. Anyone who can edit a protection can usually remove it. The file owner can always edit protected cells. Treat protection as a guard against accidental edits, not as a security boundary.
Creating protections with Apps Script
Apps Script can create range protections with range.protect(). The following function protects row 1 and limits editing to the user who runs the script.
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.
protection.removeEditors(protection.getEditors());
protection.addEditor(Session.getEffectiveUser());
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
}Listing, auditing, and removing protections
sheet.getProtections() returns every protection on a sheet. The following function logs each one so you can review them.
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());
});
});
}To remove a protection, call p.remove(). Check p.canEdit() first, because the call fails for protections you cannot edit.
Designing a protection scheme for a shared spreadsheet
Build the scheme in this order:
- List the ranges that hold formulas, headers, and reference data.
- Protect those ranges, and set the editors to the people who maintain them.
- Use warning-only protection for ranges that editors may need to change sometimes.
- Leave input cells editable by protecting the whole sheet and excluding those cells.
- Run
listProtectionseach quarter to remove stale entries.
Protected ranges in the Sheets API
The Sheets API expresses the same protection as an addProtectedRange request inside spreadsheets.batchUpdate. Indexes are zero-based, and endRowIndex and endColumnIndex are exclusive. The request below protects A1:E1 on the sheet with ID 0.
{
"requests": [
{
"addProtectedRange": {
"protectedRange": {
"range": {
"sheetId": 0,
"startRowIndex": 0,
"endRowIndex": 1,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"description": "Header row",
"warningOnly": false,
"editors": {
"users": ["[email protected]"]
}
}
}
}
]
}Exercise
Exercise
A teammate says the protected salary column is safe because viewers cannot see it. What is wrong with this claim, and what should you do instead?
Show answer
Protection only restricts editing, so viewers can still read the values. Remove sensitive data from the sheet, or share it through a tool that controls access to the data itself.