Birch UI v0.1
Pricing

ProtectionSpreadsheet

Lock cells, hide formulas, protect a sheet with an optional password, choose what users may still do, and protect the workbook structure.

Open .md

Protection works the way it does in Excel. Every cell is locked by default, but locking only takes effect once the sheet is protected. Unlock the input cells first, then protect the sheet.

TypeScript
sheet.setStyle('B2:E13', { locked: false });
sheet.setStyle('F2:F13', { hideFormula: true });
sheet.protectSheet({ password: 'q3-close', sort: true, autoFilter: true });

What protection blocks#

On a protected sheet, users cannot do the following to locked cells: type, edit with F2, paste, cut, fill, clear, replace, or insert the date. A clear message is announced and an error event fires with code: 'protected'.

Users also cannot do the following unless you allow them:

Option Allows
formatCells Styles, number formats, borders, merges and the Format Cells dialog
formatColumns, formatRows Resizing, hiding and grouping columns or rows
insertColumns, insertRows, deleteColumns, deleteRows Structure changes
sort Sorting
autoFilter Turning filters on and off, and filtering
editObjects Adding, editing and deleting notes
selectLockedCells Selecting locked cells (default true)
selectUnlockedCells Selecting unlocked cells (default true)
SheetProtectionOptionsinterface@birchui/spreadsheetFull reference
PropertyTypeDescription
selectLockedCellsoptionalboolean—
selectUnlockedCellsoptionalboolean—
formatCellsoptionalboolean—
formatColumnsoptionalboolean—
formatRowsoptionalboolean—
insertRowsoptionalboolean—
insertColumnsoptionalboolean—
deleteRowsoptionalboolean—
deleteColumnsoptionalboolean—
sortoptionalboolean—
autoFilteroptionalboolean—
editObjectsoptionalboolean—

With selectLockedCells: false, arrow keys skip over locked cells, clicks on them are ignored, and Tab cycles through the unlocked cells in reading order. That is how you build a form inside a sheet.

hideFormula: true on a cell keeps its formula out of the formula bar, the editor and the Evaluate Formula dialog while the sheet is protected. The value still shows.

The API is not restricted by default#

By default protection guards the user interface. Calls such as setValue, setStyle and insertRows still work on a protected sheet, so your code can keep calculated or imported data up to date. If you need the same rule in your own code, check it with isCellLocked(address) and isSheetProtected(), or turn enforcement on.

Enforcing protection in code#

Set enforceProtection: true and the same rules bind the API:

TypeScript
import { createSpreadsheet, ProtectionError } from '@birchui/spreadsheet';

const sheet = createSpreadsheet(container, { data: workbook, enforceProtection: true });

try {
  sheet.setValue('F2', 0);
} catch (error) {
  if (!(error instanceof ProtectionError)) throw error;
}

sheet.setValue('F2', 0, { force: true });

A blocked call throws a ProtectionError, changes nothing, and fires the error event with code: 'protected' and the sheet name. { force: true } as the last argument runs that one call regardless, which is how your own code keeps writing to cells users may not touch.

Calls Blocked on a protected sheet when
setValue, setFormula, setRange, clearRange, autoSum, flashFill, textToColumns, removeDuplicates, subtotal, removeSubtotals, pasteSpecial A cell they would write is locked
setStyle, applyCellStyle, mergeCells, unmergeCells formatCells is not allowed
setColumnWidth, hideColumns, unhideColumns; setRowHeight, hideRows, unhideRows formatColumns or formatRows is not allowed
insertRows, deleteRows, insertColumns, deleteColumns The matching option is not allowed
sortRange, sortRangeBy sort is not allowed, or the range has a locked cell
setAutoFilter autoFilter is not allowed

replaceAll does not throw: it skips locked cells, as it does for users, and returns the number of cells it changed.

What enforcement does not cover:

  • Reads. getValue, getFormula, getRange, toJSON and the rest are never blocked, and hideFormula hides a formula from the user interface only.
  • Methods outside the table: notes, comments, hyperlinks, pictures, charts, shapes, slicers, tables, pivot tables, conditional formats, validation rules, defined names, freeze panes, grouping, fromJSON and the import methods. They work on a protected sheet as before.
  • undo() and redo(). They replay edits that were allowed when they were made, including the step that turned protection on.
  • protectSheet, unprotectSheet, protectWorkbook and unprotectWorkbook themselves.

update({ enforceProtection }) switches at runtime. The option belongs to the component; the headless formula engine keeps protection settings in the workbook JSON and does not apply them.

Passwords#

protectSheet({ password }) stores a salted SHA-512 hash with 100,000 iterations, the format Excel uses, never the password itself. unprotectSheet(password) returns false and fires a protected error when the password is wrong. Hashing takes roughly a quarter of a second. Sheets imported from Excel keep their hash, and the same password unlocks them here. A sheet password is a deterrent, not encryption: anyone with the file can still read the data.

Workbook structure#

protectWorkbook(password?) stops users from adding, deleting, renaming, moving, hiding and unhiding sheets. The tab bar reports a protected error. Because the API cannot carry on silently here, addSheet, removeSheet, renameSheet, moveSheet, setSheetHidden and duplicateSheet throw a ProtectionError while the structure is protected, whatever enforceProtection is set to. With enforceProtection: true the error event fires as well. Pass { force: true } as the last argument to make the change anyway, for example addSheet('Actuals', undefined, { force: true }). unprotectWorkbook(password?) lifts the protection, and isWorkbookProtected() reports the state.

Protection survives toJSON() and round-trips to xlsx.

↑ ↓ to move, Enter to open, Esc to close