Template: sheets
book_append_sheet detects a template workbook and adds the sheet to the package.
| Function | Effect |
|---|---|
template_book_append_sheet(wb, ws, name, roll) |
add a worksheet at the end |
template_book_replace_sheet(wb, sheet, ws) |
replace the content of a sheet. The name and position stay, so cross-sheet formulas stay valid |
template_book_delete_sheet(wb, sheet) |
remove a sheet and the defined names that refer to it |
template_book_delete_sheet does not rewrite formulas or other references. Use it only when that is safe.
const XLSX = require("@agent-sheet/wasm");const src = XLSX.utils.book_new();XLSX.utils.book_append_sheet(src, XLSX.utils.aoa_to_sheet([["x"]]), "Sheet1");const wb = XLSX.read(XLSX.write(src, { type: "buffer", bookType: "xlsx" }), { template: true });
const ws = XLSX.utils.aoa_to_sheet([["A", "B"], [1, 2]]);XLSX.utils.sheet_set_range_style(ws, "A1:B1", { bold: true }); // do this BEFORE appendingXLSX.utils.book_append_sheet(wb, ws, "Sheet2");XLSX.utils.template_book_replace_sheet(wb, "Sheet2", XLSX.utils.aoa_to_sheet([["replaced"]]));XLSX.utils.template_book_append_sheet(wb, XLSX.utils.aoa_to_sheet([["tmp"]]), "Temp");XLSX.utils.template_book_delete_sheet(wb, "Temp");const out = XLSX.read(XLSX.write(wb, { type: "buffer", bookType: "xlsx", template: true }));console.log(out.SheetNames, out.Sheets.Sheet2.A1.v);[ 'Sheet1', 'Sheet2' ] replacedSheet visibility
Section titled “Sheet visibility”template_sheet_set_visibility(wb, sheet, vis). 0 is visible, 1 hidden, 2 very hidden.
const XLSX = require("@agent-sheet/wasm");const src = XLSX.utils.book_new();XLSX.utils.book_append_sheet(src, XLSX.utils.aoa_to_sheet([[1]]), "A");XLSX.utils.book_append_sheet(src, XLSX.utils.aoa_to_sheet([[2]]), "B");const wb = XLSX.read(XLSX.write(src, { type: "buffer", bookType: "xlsx" }), { template: true });XLSX.utils.template_sheet_set_visibility(wb, "B", 2); // 0 visible, 1 hidden, 2 very hiddenconst out = XLSX.read(XLSX.write(wb, { type: "buffer", bookType: "xlsx", template: true }));console.log(out.Workbook.Sheets.map((s) => s.Hidden));[ 0, 2 ]