Skip to content

Protection

const XLSX = require("@agent-sheet/wasm");
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, XLSX.utils.aoa_to_sheet([[1]]), "S");
wb.Custprops = { _MarkAsFinal: true };
const back = XLSX.read(XLSX.write(wb, { type: "buffer", bookType: "xlsx" }));
console.log(!!(back.Custprops && back.Custprops._MarkAsFinal));
Output
true

wb.Workbook.Protection = { lockStructure: true, lockWindows: false } stops users from adding, moving, hiding and renaming sheets (lockStructure). It also fixes the window size and position (lockWindows). The XLSX reader and writer support these flags. A password property on this workbook object is not written; worksheet passwords are separate. Use template editing when you also need to preserve unrelated file content.

ws["!protect"] turns on sheet protection. An empty object protects the sheet with the defaults below. Each key is one permission. The value is inverted: true means the action is disabled (locked), and false means it is enabled (allowed). This matches the sheetProtection attributes in the file.

Key Permission Default when protected
selectLockedCells, selectUnlockedCells select cells enabled
formatCells, formatColumns, formatRows format disabled
insertColumns, insertRows, insertHyperlinks insert disabled
deleteColumns, deleteRows delete disabled
sort, autoFilter, pivotTables sort, filter, use PivotTables disabled
objects, scenarios edit objects and scenarios enabled
const XLSX = require("@agent-sheet/wasm");
const ws = XLSX.utils.aoa_to_sheet([[1, 2]]);
ws["!protect"] = { formatCells: false, selectLockedCells: true }; // allow formatting, block selecting locked cells
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, "S");
const p = XLSX.read(XLSX.write(wb, { type: "buffer", bookType: "xlsx" })).Sheets.S["!protect"];
console.log(p.formatCells, p.selectLockedCells, p.insertRows); // false true true
Output
false true true

The password property can set a sheet password on this path. For an existing template workbook, use utils.template_sheet_set_password. See Template: protection.

Per-cell hidden and editable flags are described in Cell styles.