ExcelScript.WorkbookProtection interface
Represents the protection of a workbook object.
Methods
get |
Specifies if the workbook is protected. |
protect(password) | Protects the workbook. Fails if the workbook has been protected. |
unprotect(password) | Unprotects the workbook. |
Method Details
getProtected()
Specifies if the workbook is protected.
getProtected(): boolean;
Returns
boolean
Examples
/**
* This script protects the workbook with a default password, if there is not already protection.
*/
function main(workbook: ExcelScript.Workbook) {
// Get the workbook-level protection object.
const protection = workbook.getProtection();
// Check if the workbook is already protected.
if (!protection.getProtected()) {
// Apply a default password.
protection.protect("1234");
}
}
protect(password)
Protects the workbook. Fails if the workbook has been protected.
protect(password?: string): void;
Parameters
- password
-
string
Workbook protection password.
Returns
void
Examples
/**
* This script protects the workbook using a password given in a user prompt.
*/
function main(workbook: ExcelScript.Workbook, password?: string) {
// Get the workbook-level protection object.
const protection = workbook.getProtection();
// Protect the workbook with the given password.
// If the optional password was omitted,
// no password will be needed to unprotect the workbook.
protection.protect(password);
}
unprotect(password)
Unprotects the workbook.
unprotect(password?: string): void;
Parameters
- password
-
string
Workbook protection password.
Returns
void
Examples
/**
* This script removes protection from the workbook using a password given in a user prompt.
*/
function main(workbook: ExcelScript.Workbook, password?: string) {
// Get the workbook-level protection object.
const protection = workbook.getProtection();
// Unprotect the workbook with the given password.
protection.unprotect(password);
}
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.
Office Scripts