Novis\Spreadsheet\Sheet::protect
Locks the sheet, so a person cannot change its cells or its layout in Excel, and returns the sheet.
password is the password a person types in Excel to unlock the sheet. Without it, anyone can unlock the sheet with one click. allow names what a person can still do on a locked sheet, such as "sort", "autoFilter", "formatColumns" or "insertRows". A name that is not on that list throws a LogicError.
Good to know: protection is not a security feature. The file keeps only a short hash of the password, and any tool can remove it. Every program can still read the values of a locked sheet. Use protection to stop mistakes, and never to hide data. A CSV file has no protection, so protect does nothing there.
The examples below lock the layout of a price list, allow sorting and filters, and read the values of a locked sheet.
Parameters
$options?{ password?: string, allow?: array<string> }default null
Return value
Returns Sheet.
Examples
Lock a price list
<?nvs// Shows how to lock a price list, so nobody changes a price by mistake.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Prices');$sheet->addRows([['item' => 'Widget', 'price' => 2.5], ['item' => 'Gadget', 'price' => 4.0]]);
// A person types this password in Excel to unlock the sheet.$sheet->protect({ password: 'shop-prices' });bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the sheet. This line checks that the sheet is locked.var $xml = Core\Zip::read($file, 'xl/worksheets/sheet1.xml') as string;echo Core\Str::contains($xml, '<sheetProtection password=') ? 'the sheet is locked' : 'the sheet is open', "\n";
// The file has a hash of the password. It does not have the password itself.echo Core\Str::contains($xml, 'shop-prices') ? 'the password is in the file' : 'the password is not in the file', "\n";the sheet is lockedthe password is not in the fileAllow sorting and filters
<?nvs// Shows how to lock a sheet and still let a person sort it and use its filters.use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Orders');$sheet->addRows([['order' => 1042, 'total' => 19.9], ['order' => 1043, 'total' => 5.5]]);$sheet->protect({ allow: ['sort', 'autoFilter'] });bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// `Core\Zip::read` returns the XML of the sheet. In it, `sort="0"` means sorting is not locked.var $xml = Core\Zip::read($file, 'xl/worksheets/sheet1.xml') as string;echo Core\Str::contains($xml, 'sort="0"') ? 'a person can sort' : 'sorting is locked', "\n";echo Core\Str::contains($xml, 'autoFilter="0"') ? 'a person can filter' : 'filters are locked', "\n";
// A name that is not on the list throws a `LogicError`.try { $book = Workbook::createInMemory(); $book->addSheet('Orders')->protect({ allow: ['everything'] });} catch (LogicError $e) { echo $e->message, "\n";}a person can sorta person can filter`everything` is not something a protected sheet can allow. The names are objects, scenarios, formatCells, formatColumns, formatRows, insertColumns, insertRows, insertHyperlinks, deleteColumns, deleteRows, selectLockedCells, sort, autoFilter, pivotTables, selectUnlockedCells.A locked sheet can still be read
<?nvs// Shows that a program can read every value of a locked sheet without its password.use Novis\Spreadsheet\Reader;use Novis\Spreadsheet\Workbook;
var $book = Workbook::createInMemory();var $sheet = $book->addSheet('Salaries');$sheet->addRows([['name' => 'Ann', 'salary' => 52000], ['name' => 'Ben', 'salary' => 48000]]);$sheet->protect({ password: 'do-not-open' });bytes $file = $book->finish()->bytes ?? Core\Bytes::fill(0, 0);
// The reader does not need the password. Protection does not hide any value.foreach (Reader::open($file)->rows('Salaries', { header: true }) as var $row) { echo $row->values['name'], ': ', $row->values['salary'], "\n";}Ann: 52000Ben: 48000