Google Apps Script

Class PageProtection

Access and modify the state of protection permissions on a sheet. This class allows users to access and modify sheet protection permissions on a sheet.

Methods

MethodReturn typeBrief description
addUser(email)voidAdds a user to the list of users who can edit the sheet, if it is protected.
getUsers()String[]Returns a list of the email addresses of the users who can edit this sheet.
isProtected()BooleanIndicates whether the sheet has sheet protection enabled or not.
removeUser(user)voidRemoves a user from the list of users who can edit the sheet.
setProtected(protection)voidSets the protection status for the sheet.

Detailed documentation

addUser(email)

Adds a user to the list of users who can edit the sheet, if it is protected.

 
// Add the "user@example.com" user to the list of users who can edit this sheet
 var sheet = SpreadsheetApp.getActiveSheet();
 var permissions = sheet.getSheetProtection();
 permissions.addUser('user@example.com');
 permissions.setProtected(true);
 sheet.setSheetProtection(permissions);
 

Parameters

NameTypeDescription
emailStringthe email of the user to add

See also


getUsers()

Returns a list of the email addresses of the users who can edit this sheet. If sheet protection is disabled, the value returned by this call is meaningless.

Return

String[] — an array of email addresses of users who can edit this sheet


isProtected()

Indicates whether the sheet has sheet protection enabled or not.

 
// Determine whether or not sheet protection is enabled
 var sheet = SpreadsheetApp.getActiveSheet();
 var permissions = sheet.getSheetProtection();
 var isProtected = permissions.isProtected();
 

Return

Boolean — whether the sheet has sheet protection enabled or not

See also


removeUser(user)

Removes a user from the list of users who can edit the sheet.

 
// Remove the "user@example.com" user to the list of users who can edit this sheet
 var sheet = SpreadsheetApp.getActiveSheet();
 var permissions = sheet.getSheetProtection();
 permissions.removeUser('user@example.com');
 permissions.setProtected(true);
 sheet.setSheetProtection(permissions);
 

Parameters

NameTypeDescription
userStringthe email address of the user to remove

See also


setProtected(protection)

Sets the protection status for the sheet.

 
// Enables sheet protection for  this sheet
 var sheet = SpreadsheetApp.getActiveSheet();
 var permissions = sheet.getSheetProtection();
 permissions.setProtected(true);
 sheet.setSheetProtection(permissions);
 

Parameters

NameTypeDescription
protectionBooleantrue to enable sheet protection, false to disable sheet protection

See also

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.