Ever had to set a large amount of tabs in a spreadsheet to protected?
It can take a while.
Here’s a simple bit of Apps Script you can use to automate the process:
/**
* Removes Protected from every tab in the sheet and then protects it so only I can edit.
*/
function protectAllSheets() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const me = Session.getEffectiveUser().getEmail();
ss.getSheets().forEach(sheet => {
// Remove any existing protection to avoid duplication
const existingProtection = sheet.getProtections(SpreadsheetApp.ProtectionType.SHEET);
existingProtection.forEach(p => p.remove());
// Create new protection
const protection = sheet.protect().setDescription('Locked by script');
// Allow only the script owner to edit
protection.removeEditors(protection.getEditors());
protection.addEditor(me);
// If the sheet is not yours, this prevents domain users from editing
if (protection.canDomainEdit()) {
protection.setDomainEdit(false);
}
});
}
Hope this made your Google search a bit shorter, leave a comment if this helped or you have any specific problems you need a solution to.
