/** * Public Workbook façade over internal {@link OpcPackage}. * * Async `load` / `save` align with package I/O. Sync `create()` seeds * an in-memory package via {@link OpcPackage.createFromParts} (Transitional * SpreadsheetML only). * * Public sheet surface (API-RULES R1.5 — one name per concept): * `load`, `worksheets`, `sheet` / `firstSheet` / `lastSheet` / `activeSheet`, * `addSheet` / `removeSheet` / `moveSheet`, `names`. * * Citations: `docs/specs/workbook-model.md`, transitional `sml.xsd` CT_Workbook. */ import { Font } from '../cell/rich-text'; import { OpcPackage } from '../core/package/opc-package'; import { SharedStringTable } from '../shared-strings/shared-string-table'; import { WorkbookStyles } from '../styles/workbook-styles'; import { DefinedNames } from './defined-names'; import { BuiltInDocumentProperties } from './document-properties'; import { WorkbookProtectionInfo, WorkbookProtectionOptions } from './workbook-protection'; import { Worksheet, WorksheetCellHost } from './worksheet'; /** * Represents an Excel workbook containing worksheets, document settings, and * workbook-level data, letting you open, edit, and save spreadsheet files * programmatically. */ export declare class Workbook { private readonly pkg; private readonly workbookPartName; private readonly sheetsCollection; private readonly definedNamesCollection; private readonly sst; private readonly stylesTable; private readonly personsStore; private readonly documentPropertiesImpl; private readonly _protection; private _date1904; /** True when original workbook.xml had a workbookPr element (even empty). */ private hasWorkbookPr; private workbookPrExtraAttrsXml; private preserveChildrenXml; private rootExtraxmlnsXml; private dirty; private maxSheetId; private maxRelNum; /** Highest rIdN among package-level relationships (`/_rels/.rels`). */ private maxPackageRelNum; private nextSheetPartIndex; private nextTablePartIndex; private maxTableId; /** * Zero-based active sheet index for this session and the saved workbook view. */ private _activeSheetIndex; private constructor(); /** * Open an existing workbook from the bytes of an `.xlsx` file. * * Formulas are stored as written and never evaluated; external links are * not resolved. * * @param data - The bytes of the workbook file to open. * @returns the opened workbook, ready to read or edit. */ static open(data: Uint8Array | ArrayBuffer): Promise; /** * Open an existing workbook from a filesystem path (Node.js). * * Formulas are stored as written and never evaluated; external links are * not resolved. Path open requires the Node.js `fs` module and is not * available in browsers — read the file yourself and pass * `Uint8Array` / `ArrayBuffer` there. * * @param filePath - Absolute or relative path to an `.xlsx` file (for * example `D:\\Input.xlsx` or `./data/report.xlsx`). * @returns the opened workbook, ready to read or edit. * @throws InvalidArgumentError when `filePath` is missing or empty * @throws IoError when the file cannot be read */ static open(filePath: string): Promise; /** * Create a new, empty workbook with one visible sheet named `Sheet1`. * * @returns the new workbook. */ static create(): Workbook; /** * The worksheets in the workbook, in tab order. Each read returns a fresh * snapshot; add sheets with {@link addSheet} and remove them with * {@link removeSheet}. */ readonly worksheets: ReadonlyArray; /** * The workbook's defined names (named ranges). * * Add a name with {@link DefinedNames.add}, look one up with * {@link DefinedNames.get}. */ readonly names: DefinedNames; /** * Get a sheet by its zero-based position or its name (not case * sensitive). * * @param nameOrIndex - The sheet's name, like `'Sales'`, or its * zero-based position. * @returns the matching sheet. * @throws RangeError when a numeric index is out of bounds * @throws InvalidArgumentError when name is not found */ sheet(nameOrIndex: string | number): Worksheet; /** @private * First sheet in workbook order. * @throws RangeError when the workbook has no sheets */ readonly firstSheet: Worksheet; /** @private * Last sheet in workbook order. * @throws RangeError when the workbook has no sheets */ readonly lastSheet: Worksheet; /** * The sheet that is currently active — the one Excel shows when the * workbook opens. Assign a sheet to make it active. * * @throws RangeError when setting a sheet that is not part of this * workbook */ /** * Set the sheet shown as active when the workbook opens. * @throws RangeError when the sheet is not part of this workbook */ activeSheet: Worksheet; /** * Add a new, visible worksheet at the end of the workbook. * * @param name - The new sheet's name. Omit it to use the next available * `Sheet2`, `Sheet3`, … name. * @returns the newly created sheet. */ addSheet(name?: string): Worksheet; /** * Remove a sheet from the workbook. The last remaining sheet cannot be * removed — a workbook must always have at least one. * * @param sheet - The sheet to remove. * @throws LastSheetError when this is the only sheet * @throws RangeError when the sheet is not part of this workbook */ removeSheet(sheet: Worksheet): void; /** * Move a sheet to a different position, changing its place in the tab * order. * * @param fromIndex - The sheet's current zero-based position. * @param toIndex - The zero-based position to move it to. * @throws RangeError when either index is out of range */ moveSheet(fromIndex: number, toIndex: number): void; /** * Whether the workbook uses the 1904 date system (as Excel on Mac * historically did) instead of the usual 1900 system. Affects how dates * are stored as serial numbers. Default `false`. */ date1904: boolean; /** * The workbook's built-in document properties — title, subject, author, * and other file details shown in Excel's file info. */ readonly builtInDocumentProperties: BuiltInDocumentProperties; /** * The workbook's author. * * A shortcut for `{@link builtInDocumentProperties}.author`. Assign * `undefined` to clear it. */ author: string | undefined | null; /** * The workbook's styles table — its fonts, fills, borders, and number * formats. Style authoring is normally done per cell via * `cell.style` / `cell.numberFormat`; this handle is for advanced work. * * @private Entire `WorkbookStyles` surface is internal (stripped from the * shipped package `.d.ts`). Prefer `cell.style` / `cell.numberFormat`. */ readonly styles: WorkbookStyles; /** * Create a font you can apply to part of a cell’s rich text. * * Set properties on the returned font, then assign it with * `cell.richText.characters(start, end).font = font`. * This does not create a shared style-table font. */ createFont(): Font; /** * `true` when workbook structure and/or windows are protected. */ readonly isProtected: boolean; /** * Current workbook protection settings, or `undefined` when not protected. */ readonly workbookProtection: WorkbookProtectionInfo | undefined; /** * Protect the workbook structure and/or windows (not file encryption). * * @param password - Optional password required to unprotect later. * @param options - What to lock; defaults lock structure only. */ protect(password?: string, options?: WorkbookProtectionOptions): void; /** * Turn off workbook structure/windows protection. * * @param password - Required when the workbook was protected with a password. */ unprotect(password?: string): void; /** * Internal shared string table (cells wire in TASK-1105). * @private */ readonly sharedStrings: SharedStringTable; /** * Write the workbook back into `.xlsx` file bytes, applying every change * made to its sheets, tables, styles, names, and properties. * * @returns the bytes of the saved workbook file. */ save(): Promise; /** * Save the workbook to a filesystem path (Node.js), applying every change * made to its sheets, tables, styles, names, and properties. * * The parent directory must already exist. Path save requires the Node.js * `fs` module and is not available in browsers — use parameterless * {@link Workbook.save} and write the returned bytes yourself there. * * @param filePath - Absolute or relative destination path (for example * `D:\\Output.xlsx` or `./out/report.xlsx`). * @throws InvalidArgumentError when `filePath` is missing or empty * @throws IoError when the file cannot be written */ save(filePath: string): Promise; /** * Sheet removed at zero-based index — drop local names and reindex higher ids. * @private */ onSheetRemoved(deletedIndex: number): void; /** * Sheet reordered — remap defined-name sheetIndex values. * @private */ onSheetMoved(fromIndex: number, toIndex: number): void; /** @private */ markDirty(): void; /** * Allocate the next package-level relationship id (`/_rels/.rels`). * @private */ allocatePackageRelationshipId(): string; /** @private */ allocateSheetId(): number; /** @private */ allocateRelationshipId(): string; /** @private */ nextWorksheetPartName(): string; /** @private */ readonly package: OpcPackage; /** * Cell host shared by worksheets (SST + package access). * @private */ createWorksheetCellHost(): WorksheetCellHost; /** @private */ private nextTablePartNameInternal; private static fromPackage; private flushWorksheets; /** * Keep session active index inside `[0, length)`. * Empty workbooks are not expected after open/create; clamp still avoids OOB. */ private clampActiveSheetIndex; private flushWorkbookXml; /** * Write workbook persons part used by threaded-comment authors. * Materializes after worksheet flush so person rows allocated during sheet * mutations are included (API-RULES R5.1). */ private flushPersons; private flushSharedStrings; private flushStyles; /** * Write dirty core/app property parts and ensure package-level relationships. * Untouched parts keep original bytes (open → save lossless). * Passes current worksheet names so dirty app.xml can emit Excel inventory. */ private flushDocumentProperties; }