Protection
Protection has three levels, and the cell level only works inside a protected sheet:
- 1Workbook
createWorkbook({ protection })locks the structure: no adding, removing or renaming sheets - 2Sheet
.sheet(name, { protection })makes locked cells read-only and hides hidden formulas - 3Cell
style: { protection }marks a cell as editable (locked: false) or its formula as hidden
Choose which cells stay editable
Every cell is locked by default. In a protected sheet, unlock the inputs people should fill in, and hide formulas you don't want shown in the formula bar:
import { createExcelSchema, createWorkbook } from "typed-xlsx";
type Renewal = { account: string; target: number };
const schema = createExcelSchema<Renewal>()
.column("account", { header: "Account", accessor: "account" })
.column("target", {
header: "Target ARR",
accessor: "target",
style: { protection: { locked: false } },
})
.column("uplift", {
header: "Uplift",
formula: ({ refs }) => refs.column("target").mul(0.08),
style: { protection: { hidden: true } },
})
.build();
createWorkbook()
.sheet("Renewals", { protection: { password: "ops", selectLockedCells: false } })
.table("renewals", { schema, rows: [{ account: "Acme", target: 112000 }] });
Here people can only select and edit Target ARR; the Uplift formula is computed but not visible. To make the editable cells stand out, give unlocked cells a look with table defaults or the theme's cellUnlocked slot.
Sheet protection
protection: true protects the sheet with Excel's default permissions. An object sets a password and individual permissions:
import { createWorkbook } from "typed-xlsx";
createWorkbook().sheet("Data", {
protection: {
password: "sheet-secret",
selectUnlockedCells: true,
selectLockedCells: false,
sort: true,
autoFilter: true,
formatCells: false,
insertRows: false,
deleteRows: false,
},
});
The streaming builder's .sheet() takes the same option.
Workbook protection
Workbook protection is about the tabs, not the cells:
import { createWorkbook } from "typed-xlsx";
const workbook = createWorkbook({
protection: { password: "workbook-secret", structure: true },
});
Conditional styles can't change protection: whether a cell is locked or hidden is always set through style.