List Protections As Rows

listProtectionsAsRows(ss) returns one row per sheet protection and range protection: [sheetName, rangeA1 or "(whole sheet)", description, editors, warningOnly]. Write the result to an audit tab so you can see who can edit what before you hand a spreadsheet to someone else. It returns data rows only, with no header row.

When to use

  • Before a hand-off, to see every protected sheet and range in one list
  • Finding a protection that still lists someone who has left
  • Checking whether a range is protected strictly or only with a warning

How to use this snippet

Pass a Spreadsheet, or call it with no argument to use the active one. editors is a comma-separated list of email addresses. For a warning-only protection (warningOnly is true) the editors cell is left blank: anyone who can edit the file can edit that range after dismissing a warning, so the list would mislead. The helper only reads. It does not change any protection.

/** * Return [sheetName, rangeA1 or "(whole sheet)", description, editors, * warningOnly] for every sheet and range protection in ss (default: the * active spreadsheet). editors is a comma-separated list of emails and is * blank for warning-only protections. No header row is included. * * @param {GoogleAppsScript.Spreadsheet.Spreadsheet} [ss] * @return {Array<Array<*>>} */ function listProtectionsAsRows(ss) { var book = ss || SpreadsheetApp.getActive(); var types = SpreadsheetApp.ProtectionType; var rows = []; var sheets = book.getSheets(); for (var i = 0; i < sheets.length; i++) { var sheet = sheets[i]; var name = sheet.getName(); var whole = sheet.getProtections(types.SHEET); var j, p; for (j = 0; j < whole.length; j++) { rows.push(protectionRow_(name, '(whole sheet)', whole[j])); } var ranges = sheet.getProtections(types.RANGE); for (j = 0; j < ranges.length; j++) { p = ranges[j]; rows.push(protectionRow_(name, p.getRange().getA1Notation(), p)); } } return rows; } function protectionRow_(sheetName, where, protection) { var warningOnly = protection.isWarningOnly(); var editors = ''; if (!warningOnly) { editors = protection .getEditors() .map(function (user) { return user.getEmail(); }) .join(', '); } return [sheetName, where, protection.getDescription() || '', editors, warningOnly]; }

Example

function writeProtectionAudit() { var ss = SpreadsheetApp.getActive(); var rows = listProtectionsAsRows(ss); var tab = ss.getSheetByName('Protection audit') || ss.insertSheet('Protection audit'); tab.clearContents(); tab.getRange(1, 1, 1, 5).setValues([['Sheet', 'Range', 'Description', 'Editors', 'Warning only']]); if (rows.length) tab.getRange(2, 1, rows.length, 5).setValues(rows); }

Notes

  • Protections only limit people who already have edit access to the file. Someone who has left may also still be shared on the file itself, so check the Share dialog as well.
  • A whole-sheet protection can have unprotected ranges inside it. Those ranges are not listed as separate rows.
  • To change a protection, use Data → Protect sheets and ranges, or see Protect Row for Editors for the scripted version.

Happy Coding!

NitroGAS Chrome Extension

Want this snippet handy inside the Apps Script editor? NitroGAS gives you free snippets — plus optional Co-Pilot when you want a boost (Day Pass — $3, or 1-week, 1-month, or yearly passes — no subscription). Happy Coding!

Get the Extension