protect method
Protect workbook using the password from moving, hiding, adding and renaming the worksheet.
// Create a new Excel Document.
final Workbook workbook = Workbook(1);
// Accessing sheet via index.
final Worksheet sheet = workbook.worksheets[0];
sheet.getRangeByIndex(1, 1).text = 'Workbook is protected';
// Protect Workbook.
workbook.protect(true, true, 'Syncfusion');
// Save and dispose workbook.
final List<int> bytes = workbook.saveAsStream();
saveAsExcel(bytes, 'ExcelWorkbookProtection2.xlsx');
workbook.dispose();
Implementation
void protect(bool isProtectWindow, bool isProtectContent,
[String? sPassword]) {
if (!isProtectWindow && !isProtectContent) {
throw Exception('One of params must be TRUE.');
}
if (bCellProtect || bWindowProtect) {
throw Exception(
'Workbook is already protected. Use Unprotect before calling method.');
}
bCellProtect = isProtectContent;
bWindowProtect = isProtectWindow;
if (sPassword != null) {
password = sPassword;
final int value =
(password!.isNotEmpty) ? Worksheet.getPasswordHash(password!) : 0;
isPassword = value;
}
}