Skip to content
Office Open

XLSX

Patching

Modify an existing .xlsx through canonical WorkbookOptions commands

patchWorkbook parses an existing .xlsx into WorkbookOptions, applies ordered commands, and regenerates the package. Worksheet order, shared strings, and workbook sheet definitions are reconciled after worksheet commands.

ts
import { patchWorkbook } from "@office-open/xlsx";

const result = await patchWorkbook({
  data: workbook,
  commands: [
    {
      type: "replace",
      at: { field: "worksheets", route: [{ name: "Invoice" }], text: { placeholder: "customer" } },
      value: "Acme Corporation",
    },
    {
      type: "update",
      at: { field: "worksheets", route: [{ name: "Invoice" }] },
      set: { state: "hidden" },
    },
    {
      type: "insert",
      at: { field: "worksheets", route: [{ name: "Invoice" }, { collection: "comments" }] },
      value: [{ cell: "A1", author: "Jane Doe", text: "Check this value" }],
    },
  ],
});

Worksheet routes accept name or sheetId; cells and rows are addressed through canonical reference, address, index, and property routes. Set packageVariant for macro and template workbooks.

#Options

OptionTypeDefault
dataDataTyperequired
commandsreadonly WorkbookPatchCommand[]required
outputTypeOutputType"uint8array"
packageVariant"standard" | "macro" | "template" | "macroTemplate""standard"
placeholderDelimiters{ start, end }{ start: "{{", end: "}}" }