protect method

void protect(
  1. bool isProtectWindow,
  2. bool isProtectContent, [
  3. String? password
])

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? password]) {
  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 (password != null) {
    _password = password;
    final int value =
        (_password!.isNotEmpty) ? Worksheet._getPasswordHash(_password!) : 0;
    _isPassword = value;
  }
}