Sub topic 12
Protected ranges in the Sheets API
This chapter assumes you know cells, A1 notation, sheet tabs, and the difference between viewers, commenters, and editors. The examples use basic JSON and HTTP concepts. You do not need Apps Script to use the API.
The API protection model
The Google Sheets API manages protection through the spreadsheets.batchUpdate method. A protected range is represented by a ProtectedRange object. It can describe a rectangular range, an entire sheet, or a range identified by a named range.
The API uses zero-based indexes:
startRowIndexandstartColumnIndexinclude the starting row or column.endRowIndexandendColumnIndexexclude the ending row or column.- Row 0 and column 0 mean row 1 and column A in the spreadsheet.
For example, A1:E1 is row index 0 through 1 and column index 0 through 5.
Add a protected range
Send an addProtectedRange request inside the requests array. The following request protects A1:E1 on the sheet whose ID is 0. The ID is not necessarily the visible sheet position, so obtain it from the sheet's properties.sheetId.
{
"requests": [
{
"addProtectedRange": {
"protectedRange": {
"range": {
"sheetId": 0,
"startRowIndex": 0,
"endRowIndex": 1,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"description": "Header row",
"warningOnly": false,
"editors": {
"users": ["[email protected]"]
}
}
}
}
]
}Call the endpoint with an OAuth access token that has spreadsheet write access:
export ACCESS_TOKEN="your-oauth-token"
export SPREADSHEET_ID="your-spreadsheet-id"
curl -X POST \
"https://sheets.googleapis.com/v4/spreadsheets/${SPREADSHEET_ID}:batchUpdate" \
-H "Authorization: Bearer ${ACCESS_TOKEN}" \
-H "Content-Type: application/json" \
--data '{
"requests": [
{
"addProtectedRange": {
"protectedRange": {
"range": {
"sheetId": 0,
"startRowIndex": 0,
"endRowIndex": 1,
"startColumnIndex": 0,
"endColumnIndex": 5
},
"description": "Header row",
"warningOnly": false,
"editors": {
"users": ["[email protected]"]
}
}
}
}
]
}'The response includes the new protected range and its generated protectedRangeId. Save this ID if later code must update or delete the protection.
Protect a sheet with editable exceptions
To protect an entire sheet, provide a GridRange containing only sheetId. Add unprotectedRanges for cells that users may edit. For example, this protects the Budget sheet while leaving B2:B20 editable:
{
"requests": [
{
"addProtectedRange": {
"protectedRange": {
"range": {
"sheetId": 123456789
},
"description": "Budget calculations",
"warningOnly": false,
"unprotectedRanges": [
{
"sheetId": 123456789,
"startRowIndex": 1,
"endRowIndex": 20,
"startColumnIndex": 1,
"endColumnIndex": 2
}
]
}
}
}
]
}The exception uses B2:B20 because row 1 is zero-based index 1, column B is index 1, and the exclusive ends are row 20 and column 2.
| Protection setting | API property | Result |
|---|---|---|
| Block edits | `"warningOnly": false` | Only permitted editors can change the protected cells |
| Show a warning | `"warningOnly": true` | Editors receive a confirmation prompt but can continue |
| Leave input cells open | `unprotectedRanges` | Those ranges remain editable inside a protected sheet |
Read, change, and remove protections
Use spreadsheets.get to inspect protections. Requesting only the relevant fields makes the response smaller:
GET https://sheets.googleapis.com/v4/spreadsheets/SPREADSHEET_ID?fields=sheets(properties(sheetId,title),protectedRanges)A protection can be changed with updateProtectedRange. Include its protectedRangeId and list the properties being changed in fields:
{
"requests": [
{
"updateProtectedRange": {
"protectedRange": {
"protectedRangeId": 987654321,
"description": "Reviewed header",
"warningOnly": true
},
"fields": "description,warningOnly"
}
}
]
}To remove it, use deleteProtectedRange:
{
"requests": [
{
"deleteProtectedRange": {
"protectedRangeId": 987654321
}
}
]
}A caller still needs sufficient access to manage the protection. An invalid sheet ID, range, protection ID, or editor configuration causes the batch request to fail. Validate IDs and ranges before sending changes.
Design choices and common errors
Use a restricted protection when accidental or unauthorized edits must be blocked. Use warning-only protection when collaboration is more important than enforcement and you want to make risky edits visible.
Keep protections narrow and descriptive. A single whole-sheet protection with clearly documented input exceptions is often easier to audit than many overlapping ranges. Avoid assuming that sheet position equals sheetId, and remember that an A1 range must be converted to zero-based indexes for GridRange.
Exercise
You need to protect the whole sheet with ID 42 but keep C3:C50 editable. Which indexes describe the exception?
Show answer
Use startRowIndex 2, endRowIndex 50, startColumnIndex 2, and endColumnIndex 3, with sheetId 42. The end indexes are exclusive.