protect

protect

PREMIUM BASIC / ADVANCED

Password protects the contents of an Excel spreadsheet.

Description
public protect($source, $target, $options = array())

This method allows to password protect an XLSX file. One may protect worksheet or sheet scopes.

Notice that this method does not encrypt the contents of the Excel spreadsheet, only protects them from unauthorized modifications. This method is not secure and can be easily overriden by an experienced user. The encrypt method can be used to encrypt XLSX files.

Parameters

source

Path to the Excel spreadsheet you want to protect.

target

Path to the resulting protected spreadsheet.

options

The possible keys and values are:

Key Type Description
replace bool True or false. If true (default value) replaces the existing protection if it exists.
password string The password should be a string of 15 or less characters. If you introduce a password with more than 15 characters it will be automatically truncated.
scope string 'worksheet' or 'sheets'.
sheetProtections array Available sheet protection types:
  • autoFilter
  • deleteColumns
  • deleteRows
  • formatCells
  • formatColumns
  • formatRows
  • insertColumns
  • insertHyperlinks
  • insertRows
  • pivotTables
  • sort
  • objects
  • selectLockedCells
  • selectUnlockedCells
  • scenarios
  • sheet

Boolean values. Default as true: objects, scenarios, sheet.

sheetsSelection array Sheets to protect: 'names' or 'positions'. If not set, all sheets are protected.
Exceptions

The source file doesn't exist.

A password is not set.

Code samples

Example #1

Example #2

Release notes
  • phpxlsx 1.0:
    • new method.