@devmm/puredocs-excel 1.0.5 → 1.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -236,6 +236,8 @@ declare class ExcelBorderEdge {
236
236
  }>;
237
237
  }): ExcelBorderEdge;
238
238
  cacheKey(): string;
239
+ /** An independent copy. ExcelColor is immutable, so it is shared. */
240
+ clone(): ExcelBorderEdge;
239
241
  }
240
242
  declare class ExcelBorder {
241
243
  left: ExcelBorderEdge;
@@ -252,7 +254,21 @@ declare class ExcelBorder {
252
254
  static topOnly(style: ExcelBorderStyle, color?: ExcelColor): ExcelBorder;
253
255
  static leftOnly(style: ExcelBorderStyle, color?: ExcelColor): ExcelBorder;
254
256
  static rightOnly(style: ExcelBorderStyle, color?: ExcelColor): ExcelBorder;
255
- static readonly none: ExcelBorder;
257
+ /**
258
+ * A border with no edges set.
259
+ *
260
+ * A fresh instance per access, not a shared singleton. `ExcelBorder` is mutable
261
+ * (`border.left.style = …` is the normal way to use it), so a shared instance
262
+ * handed to two styles let an edit through one of them reach the other — and,
263
+ * once anything had assigned `none` and then modified it, every later
264
+ * `ExcelBorder.none` came back already carrying that edge.
265
+ */
266
+ static get none(): ExcelBorder;
267
+ /**
268
+ * A deep copy: the edges are new objects, so mutating the copy cannot reach the
269
+ * original. {@link CellStyle.clone} relies on this.
270
+ */
271
+ clone(): ExcelBorder;
256
272
  toXml(): string;
257
273
  static fromXmlChildren(children: Array<{
258
274
  tagName: string;
@@ -449,6 +465,17 @@ declare class StyleManager {
449
465
  * Used by Cell.style getter.
450
466
  */
451
467
  getCellStyle(styleIndex: number): CellStyle;
468
+ /**
469
+ * Whether the cellXf at `styleIndex` formats its value as a date. This is the
470
+ * one style question the value-reading path has to ask, so it is answered
471
+ * without reconstructing a CellStyle: the result is derived from the stored
472
+ * format id/code and memoised per index.
473
+ *
474
+ * Equivalent to `getCellStyle(i).numberFormat` being date-like, by
475
+ * construction — both read the same `applyNumFmt`/`numFmtId` pair through
476
+ * {@link ExcelNumberFormat.fromFormatId}.
477
+ */
478
+ isDateStyle(styleIndex: number): boolean;
452
479
  /** Builds the complete xl/styles.xml string. */
453
480
  buildStylesXml(): string;
454
481
  /** Loads an existing styles.xml, rebuilding all caches. */
@@ -477,11 +504,31 @@ interface CellData {
477
504
  /** Spill range for a dynamic-array formula anchor, e.g. "A1:A3". */
478
505
  arrayRef?: string;
479
506
  styleIndex: number;
507
+ /**
508
+ * Shared-formula group id (`<f t="shared" si="…">`) as read from the file.
509
+ *
510
+ * Only meaningful while a sheet is loading, where it links the cells of one
511
+ * fill-down to the single copy of the formula text Excel stored. Once the group
512
+ * is resolved every cell carries its own `formula`, and this is not written
513
+ * back out — see {@link Worksheet.loadFromXml}.
514
+ */
515
+ sharedIndex?: number;
516
+ }
517
+ /**
518
+ * A mutation counter shared by a worksheet and the {@link Cell} views it hands
519
+ * out. A Cell writes straight into the sheet's stored {@link CellData}, so the
520
+ * sheet cannot otherwise tell that it changed — and both the used-range memo and
521
+ * the saved-XML reuse depend on knowing.
522
+ *
523
+ * @internal
524
+ */
525
+ interface SheetVersion {
526
+ v: number;
480
527
  }
481
528
  declare class Cell {
482
529
  #private;
483
530
  /** @internal */
484
- constructor(data: CellData, sharedStrings: SharedStringManager, styles: StyleManager);
531
+ constructor(data: CellData, sharedStrings: SharedStringManager, styles: StyleManager, version?: SheetVersion);
485
532
  /** Cell reference string e.g. "A1". */
486
533
  get reference(): string;
487
534
  /** 1-based row index. */
@@ -513,11 +560,13 @@ declare class Cell {
513
560
  getText(): string;
514
561
  /**
515
562
  * Returns the display text with the cell's number format applied, e.g. a
516
- * value of 4.5 under `$#,##0.00` renders as `$4.50`.
563
+ * value of 4.5 under `$#,##0.00` renders as `$4.50`, and a date under
564
+ * `dd/mm/yyyy` as `31/01/2025`.
517
565
  *
518
- * Falls back to {@link getText} for non-numeric values and for format codes
519
- * outside the supported subset (fractions, scientific notation, conditional
520
- * sections) — so the result is never a misleading rendering.
566
+ * Falls back to {@link getText} for values with no format and for format
567
+ * codes outside the supported subset (fractions, scientific notation,
568
+ * conditional sections, elapsed time) — so the result is never a misleading
569
+ * rendering.
521
570
  */
522
571
  getFormattedText(): string;
523
572
  /**
@@ -588,6 +637,45 @@ declare class Cell {
588
637
  }, ref: string): CellData;
589
638
  }
590
639
 
640
+ /** Describes one structural edit on a sheet. */
641
+ interface RefShift {
642
+ readonly dim: 'row' | 'col';
643
+ readonly kind: 'insert' | 'delete';
644
+ /** 1-based index of the first inserted/deleted row or column. */
645
+ readonly at: number;
646
+ /** Number of rows/columns inserted or deleted (>= 1). */
647
+ readonly count: number;
648
+ /** Name of the sheet the structural edit happened on. */
649
+ readonly sheetName: string;
650
+ }
651
+ /**
652
+ * Shifts one 1-based index. Returns `null` when the index falls inside a
653
+ * delete band (the row/column it pointed at no longer exists).
654
+ */
655
+ declare function shiftIndex(index: number, shift: RefShift): number | null;
656
+ /**
657
+ * Shifts an inclusive 1-based [start, end] span, clamping it against a delete
658
+ * band the way Excel does (endpoints move independently; a partially deleted
659
+ * span shrinks). Returns `null` when the span is entirely deleted.
660
+ */
661
+ declare function shiftSpan(start: number, end: number, shift: RefShift): {
662
+ start: number;
663
+ end: number;
664
+ } | null;
665
+ /**
666
+ * Shifts a plain "A1" or "A1:B2" reference (no `$` markers — this is for
667
+ * model metadata such as merges, autofilter and validation sqref parts, which
668
+ * never carry them). Returns `null` when the referenced area is entirely
669
+ * deleted.
670
+ */
671
+ declare function shiftRangeRef(ref: string, shift: RefShift): string | null;
672
+ /**
673
+ * Shifts a space-separated list of range references (the OOXML `sqref` shape
674
+ * used by data validations). Parts that are entirely deleted are dropped;
675
+ * returns `null` when nothing survives.
676
+ */
677
+ declare function shiftSqref(sqref: string, shift: RefShift): string | null;
678
+
591
679
  /** A part that a drawing anchor references (e.g. `xl/charts/chart1.xml`). */
592
680
  interface DrawingDependentPart {
593
681
  /** Package-relative path, no leading slash — e.g. `xl/charts/chart1.xml`. */
@@ -701,7 +789,23 @@ declare class Range {
701
789
  getValues(): CellValue[][];
702
790
  /** Clears all cells in the range (values only, preserves style). */
703
791
  clear(): void;
704
- /** Auto-fits all columns in the range. */
792
+ /** Clears values AND styles of all cells in the range. */
793
+ clearAll(): void;
794
+ /**
795
+ * Moves the whole range so its top-left cell lands on `destTopLeft`,
796
+ * carrying values, formulas, styles and spill anchors. The vacated cells
797
+ * are cleared and the destination block is fully replaced (empty source
798
+ * cells clear their destination). Overlapping source/destination is safe.
799
+ * Formula text is NOT adjusted for the new position.
800
+ *
801
+ * @example
802
+ * ws.getRange('A1:B3').moveTo('D1');
803
+ */
804
+ moveTo(destTopLeft: string): void;
805
+ /**
806
+ * Auto-fits all columns in the range. Measures every column in one sparse
807
+ * sweep rather than re-scanning the sheet per column.
808
+ */
705
809
  autoFit(): void;
706
810
  /** Applies a CellStyle to all cells in the range. Returns this for chaining. */
707
811
  setStyle(style: CellStyle): this;
@@ -734,8 +838,63 @@ declare class Range {
734
838
  */
735
839
 
736
840
  type SheetVisibility = 'visible' | 'hidden' | 'veryHidden';
841
+ /**
842
+ * What users may still do on a protected sheet. Named from the user's point of
843
+ * view — the same way Excel's "Protect Sheet" dialog lists its checkboxes —
844
+ * because the underlying OOXML attributes are inverted (`formatCells="1"` means
845
+ * formatting is *blocked*), which is a reliable source of bugs.
846
+ *
847
+ * Defaults match Excel: selecting cells stays allowed, everything else does not.
848
+ */
849
+ interface SheetProtection {
850
+ allowSelectLockedCells: boolean;
851
+ allowSelectUnlockedCells: boolean;
852
+ allowFormatCells: boolean;
853
+ allowFormatColumns: boolean;
854
+ allowFormatRows: boolean;
855
+ allowInsertRows: boolean;
856
+ allowInsertColumns: boolean;
857
+ allowInsertHyperlinks: boolean;
858
+ allowDeleteRows: boolean;
859
+ allowDeleteColumns: boolean;
860
+ allowSort: boolean;
861
+ allowAutoFilter: boolean;
862
+ allowPivotTables: boolean;
863
+ allowEditObjects: boolean;
864
+ allowEditScenarios: boolean;
865
+ }
866
+ /** A hyperlink attached to a cell or range. */
867
+ interface Hyperlink {
868
+ /** The cell or range the link covers, e.g. "A1" or "A1:B2". */
869
+ readonly ref: string;
870
+ /** External target, e.g. "https://example.com" or "mailto:a@b.c". */
871
+ readonly target?: string;
872
+ /** In-workbook destination, e.g. "Sheet2!A1" or a defined name. */
873
+ readonly location?: string;
874
+ /** Text to show instead of the cell's own value. */
875
+ readonly display?: string;
876
+ /** Hover tooltip. */
877
+ readonly tooltip?: string;
878
+ }
879
+ /** A classic cell comment (a "note" in current Excel versions). */
880
+ interface CellComment {
881
+ /** The cell the comment is attached to, e.g. "B4". */
882
+ readonly ref: string;
883
+ readonly text: string;
884
+ readonly author: string;
885
+ }
886
+ /** Outcome of a structural edit (insert/delete rows or columns). */
887
+ interface StructuralEditResult {
888
+ /**
889
+ * References (post-shift, on this sheet) of formula cells that now contain
890
+ * `#REF!` because something they pointed at was deleted.
891
+ */
892
+ readonly refErrors: readonly string[];
893
+ }
737
894
  declare class Worksheet {
738
895
  #private;
896
+ /** @internal Current mutation count — see {@link #version}. */
897
+ get version(): number;
739
898
  /** @internal */
740
899
  constructor(name: string, sharedStrings: SharedStringManager, styles: StyleManager);
741
900
  get name(): string;
@@ -759,14 +918,57 @@ declare class Worksheet {
759
918
  getRange(rangeReference: string): Range;
760
919
  /** Gets the raw value of a cell (for formula evaluation). */
761
920
  getCellValue(reference: string): unknown;
921
+ /**
922
+ * Value of a cell addressed by 1-based row/column. The reference-string
923
+ * overload has to build (and the callee re-parse) a string per cell; a range
924
+ * read walking a rectangle already knows the coordinates, and on a real
925
+ * workbook that is tens of millions of strings per recalculation.
926
+ */
927
+ getCellValueAt(row: number, column: number): unknown;
762
928
  /** Formula text (without leading '=') of a cell, or null if it has none. */
763
929
  getCellFormula(reference: string): string | null;
764
930
  /** Spill range (e.g. "A1:A3") of a dynamic-array anchor, or null. Supports A1#. */
765
931
  getSpillRange(reference: string): string | null;
932
+ /**
933
+ * Iterates the populated rows of the sheet in ascending row order, yielding
934
+ * each row's 1-based index and its cells (left to right). Rows and cells
935
+ * that were never written are skipped, so a sparse sheet iterates in
936
+ * O(populated cells) regardless of its bounds.
937
+ *
938
+ * @example
939
+ * for (const { rowIndex, cells } of ws.rows()) {
940
+ * for (const cell of cells) console.log(rowIndex, cell.reference, cell.getValue());
941
+ * }
942
+ */
943
+ rows(): IterableIterator<{
944
+ rowIndex: number;
945
+ cells: Cell[];
946
+ }>;
947
+ /**
948
+ * Iterates every populated cell of the sheet in row-major order. Sparse
949
+ * counterpart to scanning {@link usedBounds} with {@link tryGetCell}.
950
+ *
951
+ * @example
952
+ * for (const cell of ws.cells()) console.log(cell.reference, cell.getValue());
953
+ */
954
+ cells(): IterableIterator<Cell>;
955
+ /**
956
+ * @internal Visits the populated cells inside a rectangle, passing the
957
+ * coordinates it already knows so the callback never has to parse a
958
+ * reference. Backs the Range operations that must not create cells; probing
959
+ * every coordinate would cost O(area) even on an empty sheet.
960
+ */
961
+ forEachCellIn(startRow: number, startColumn: number, endRow: number, endColumn: number, action: (cell: Cell, row: number, column: number) => void): void;
962
+ /**
963
+ * @internal Moves a cell by coordinates, skipping the reference parsing that
964
+ * the public {@link moveCell} has to do. Used by bulk range moves.
965
+ */
966
+ moveCellRC(fromRow: number, fromColumn: number, toRow: number, toColumn: number): void;
766
967
  /**
767
968
  * Returns the 1-based extent of populated cells (largest row and column that
768
969
  * contain data). Returns zeros for an empty sheet. Useful for snapshotting a
769
- * sheet into a dense 2D array.
970
+ * sheet into a dense 2D array — though {@link rows} and {@link cells} are
971
+ * cheaper when a dense shape is not actually required.
770
972
  */
771
973
  get usedBounds(): {
772
974
  rowCount: number;
@@ -774,6 +976,19 @@ declare class Worksheet {
774
976
  };
775
977
  /** Sets the width of a column (1-based index). */
776
978
  setColumnWidth(columnIndex: number, width: number): void;
979
+ /**
980
+ * Removes the explicit width of a column so it falls back to the sheet
981
+ * default. Counterpart to {@link setColumnWidth}; afterwards
982
+ * {@link getColumnWidth} returns `undefined`. A hidden column stays hidden.
983
+ */
984
+ resetColumnWidth(columnIndex: number): void;
985
+ /**
986
+ * Shows or hides a column (1-based index). Hidden columns round-trip to the
987
+ * file as `<col hidden="1">` and keep any explicit width.
988
+ */
989
+ setColumnHidden(columnIndex: number, hidden?: boolean): void;
990
+ /** True if the given 1-based column is hidden. */
991
+ isColumnHidden(columnIndex: number): boolean;
777
992
  /**
778
993
  * Returns the explicit width of a column (1-based index), or `undefined` when
779
994
  * the column uses the sheet default. Counterpart to {@link setColumnWidth};
@@ -783,6 +998,11 @@ declare class Worksheet {
783
998
  getColumnWidth(columnIndex: number): number | undefined;
784
999
  /** Auto-fits column width based on content (approximate). */
785
1000
  autoFitColumn(columnIndex: number): void;
1001
+ /**
1002
+ * Auto-fits an inclusive span of columns (1-based) in a single pass over the
1003
+ * populated cells, instead of re-scanning the sheet once per column.
1004
+ */
1005
+ autoFitColumns(firstColumn: number, lastColumn: number): void;
786
1006
  /** Sets the height of a row (1-based index). */
787
1007
  setRowHeight(rowIndex: number, height: number): void;
788
1008
  /**
@@ -790,10 +1010,90 @@ declare class Worksheet {
790
1010
  * the row uses the sheet default. Counterpart to {@link setRowHeight}.
791
1011
  */
792
1012
  getRowHeight(rowIndex: number): number | undefined;
1013
+ /**
1014
+ * Removes the explicit height of a row so it falls back to the sheet
1015
+ * default. Counterpart to {@link setRowHeight}; afterwards
1016
+ * {@link getRowHeight} returns `undefined`. A hidden row stays hidden.
1017
+ */
1018
+ resetRowHeight(rowIndex: number): void;
793
1019
  /** Shows or hides a row (1-based index). Hidden rows affect SUBTOTAL(101-111). */
794
1020
  setRowHidden(rowIndex: number, hidden?: boolean): void;
795
1021
  /** True if the given 1-based row is hidden. */
796
1022
  isRowHidden(rowIndex: number): boolean;
1023
+ /**
1024
+ * The 1-based indices of every hidden row, ascending. Sparse counterpart to
1025
+ * probing {@link isRowHidden} across {@link usedBounds}.
1026
+ */
1027
+ get hiddenRows(): readonly number[];
1028
+ /**
1029
+ * The 1-based indices of every hidden column, ascending. Counterpart to
1030
+ * {@link isColumnHidden}, in the same spirit as {@link hiddenRows}.
1031
+ */
1032
+ get hiddenColumns(): readonly number[];
1033
+ /**
1034
+ * Inserts `count` empty rows starting at row `at` (1-based). Everything at
1035
+ * or below `at` moves down: cell values, formulas (references are rewritten
1036
+ * the way Excel does), styles, row heights, hidden flags, merges,
1037
+ * autofilter and data-validation ranges.
1038
+ *
1039
+ * Frozen panes are a view property and stay put. Drawing/chart anchors
1040
+ * preserved from a loaded file are opaque XML and do NOT shift — images
1041
+ * keep their absolute position (known limitation).
1042
+ *
1043
+ * When formulas on OTHER sheets reference this sheet, use the
1044
+ * Workbook-level counterpart so they are rewritten too, or call
1045
+ * `applyExternalShift` on each sheet yourself. If a RecalcEngine is
1046
+ * attached, notify it afterwards (e.g. `engine.onRowsInserted(...)`).
1047
+ *
1048
+ * @example
1049
+ * ws.insertRows(3, 2); // pushes row 3 and below down by two rows
1050
+ */
1051
+ insertRows(at: number, count?: number): StructuralEditResult;
1052
+ /**
1053
+ * Deletes `count` rows starting at row `at` (1-based). Rows below move up;
1054
+ * formulas referencing the deleted band get `#REF!` (fully inside) or
1055
+ * shrink (partially inside), exactly like Excel. See {@link insertRows}
1056
+ * for the shifting rules and known limitations.
1057
+ */
1058
+ deleteRows(at: number, count?: number): StructuralEditResult;
1059
+ /**
1060
+ * Inserts `count` empty columns starting at column `at` (1-based).
1061
+ * Column-level counterpart to {@link insertRows}; widths and hidden flags
1062
+ * shift with the columns.
1063
+ */
1064
+ insertColumns(at: number, count?: number): StructuralEditResult;
1065
+ /**
1066
+ * Deletes `count` columns starting at column `at` (1-based).
1067
+ * Column-level counterpart to {@link deleteRows}.
1068
+ */
1069
+ deleteColumns(at: number, count?: number): StructuralEditResult;
1070
+ /**
1071
+ * @internal Rewrites formulas on THIS sheet after a structural edit on a
1072
+ * DIFFERENT sheet (named by `shift.sheetName`), so qualified references
1073
+ * like `'Data'!A5` stay correct. Cell positions here do not change.
1074
+ */
1075
+ applyExternalShift(shift: RefShift): StructuralEditResult;
1076
+ /**
1077
+ * Moves a cell's full contents — value, formula, style and spill anchor —
1078
+ * to another cell, overwriting the destination and clearing the source.
1079
+ * A missing source clears the destination. This is a literal move: formula
1080
+ * text is NOT adjusted for the new position.
1081
+ *
1082
+ * @example
1083
+ * ws.moveCell('A1', 'C5');
1084
+ */
1085
+ moveCell(from: string, to: string): void;
1086
+ /**
1087
+ * Copies a cell's value, formula and style to another cell, overwriting the
1088
+ * destination and leaving the source untouched. A missing source clears the
1089
+ * destination. This is a literal copy: formula references are NOT adjusted,
1090
+ * and the spill anchor (`arrayRef`) is not copied — a recalc engine
1091
+ * re-establishes it.
1092
+ *
1093
+ * @example
1094
+ * ws.copyCell('A1', 'C5');
1095
+ */
1096
+ copyCell(from: string, to: string): void;
797
1097
  /** Merges cells in the given range (e.g. "A1:B2"). */
798
1098
  mergeCells(rangeReference: string): void;
799
1099
  /** Removes a merge for the given range reference. */
@@ -821,6 +1121,82 @@ declare class Worksheet {
821
1121
  /** Sets the visibility state of this worksheet. */
822
1122
  setVisibility(state: SheetVisibility): void;
823
1123
  get visibility(): SheetVisibility;
1124
+ /**
1125
+ * Turns on sheet protection, optionally relaxing individual permissions.
1126
+ * Cells are locked by default in Excel, so protecting a sheet makes all of
1127
+ * them read-only; clear a cell style's `locked` flag to leave a cell editable.
1128
+ *
1129
+ * Excel's sheet protection is a UI guard, **not** security: the contents stay
1130
+ * readable to anything that opens the file. This library therefore never
1131
+ * creates a protection password. A password read from an existing file is
1132
+ * preserved so the sheet round-trips unchanged.
1133
+ *
1134
+ * @example
1135
+ * ws.protect(); // lock everything
1136
+ * ws.protect({ allowSort: true, allowAutoFilter: true });
1137
+ */
1138
+ protect(options?: Partial<SheetProtection>): void;
1139
+ /** Turns off sheet protection, discarding any password read from a file. */
1140
+ unprotect(): void;
1141
+ /** True when this sheet is protected. */
1142
+ get isProtected(): boolean;
1143
+ /** The active permissions, or `undefined` when the sheet is not protected. */
1144
+ get protection(): Readonly<SheetProtection> | undefined;
1145
+ /**
1146
+ * Attaches a hyperlink to a cell or range. Give `target` for an external
1147
+ * destination or `location` for one inside the workbook; passing both keeps
1148
+ * the external target and uses the location as its fragment, which is how
1149
+ * Excel links to an anchor within a document.
1150
+ *
1151
+ * A cell's displayed text still comes from its own value — set that
1152
+ * separately; `display` only overrides what Excel shows in the edit bar.
1153
+ *
1154
+ * @example
1155
+ * ws.getCell('A1').setValue('Anthropic');
1156
+ * ws.setHyperlink('A1', { target: 'https://anthropic.com', tooltip: 'Open site' });
1157
+ * ws.setHyperlink('B1', { location: "'Sheet2'!A1" });
1158
+ */
1159
+ setHyperlink(ref: string, link: {
1160
+ target?: string;
1161
+ location?: string;
1162
+ display?: string;
1163
+ tooltip?: string;
1164
+ }): void;
1165
+ /** The hyperlink covering exactly this ref, or undefined. */
1166
+ getHyperlink(ref: string): Hyperlink | undefined;
1167
+ /** Removes the hyperlink on this ref. Returns true if one was there. */
1168
+ removeHyperlink(ref: string): boolean;
1169
+ /** Every hyperlink on the sheet, including ones read from a file. */
1170
+ get hyperlinks(): readonly Hyperlink[];
1171
+ /**
1172
+ * Attaches a comment (a "note" in current Excel versions) to a cell,
1173
+ * replacing any comment already there.
1174
+ *
1175
+ * Note that writing comments makes this sheet regenerate its comment and
1176
+ * legacy-drawing parts on save. If the file was opened with other legacy
1177
+ * drawing content on the same sheet — form controls, for instance — that
1178
+ * content is not reproduced. Reading comments has no such effect.
1179
+ *
1180
+ * @example
1181
+ * ws.setComment('B4', 'Check this figure', { author: 'Bill' });
1182
+ */
1183
+ setComment(ref: string, text: string, options?: {
1184
+ author?: string;
1185
+ }): void;
1186
+ /** The comment on a cell, or undefined. */
1187
+ getComment(ref: string): CellComment | undefined;
1188
+ /** Removes the comment on a cell. Returns true if one was there. */
1189
+ removeComment(ref: string): boolean;
1190
+ /** Every comment on the sheet, including ones read from a file. */
1191
+ get comments(): readonly CellComment[];
1192
+ /** @internal True when this sheet must write its own comment parts. */
1193
+ get ownsCommentParts(): boolean;
1194
+ /**
1195
+ * @internal True when the sheet took over its comment parts and any preserved
1196
+ * originals must be dropped — including the case where every comment was
1197
+ * deleted and nothing replaces them.
1198
+ */
1199
+ get commentPartsReplaced(): boolean;
824
1200
  /**
825
1201
  * Attaches a drawing provider (e.g. a chart) to this worksheet.
826
1202
  * Satellite packages such as `@devmm/puredocs-excel-charts` call this; the core
@@ -858,12 +1234,47 @@ declare class Worksheet {
858
1234
  * clear every preserved id so the two sets cannot collide.
859
1235
  */
860
1236
  get drawingRelId(): string;
1237
+ /**
1238
+ * @internal Relationship ids for the parts this sheet generates itself.
1239
+ *
1240
+ * Derived purely from the model so the worksheet XML and the sheet's rels part
1241
+ * — which are written by different code — cannot disagree about an id. Order is
1242
+ * fixed: drawing, then external hyperlinks in insertion order, then the
1243
+ * comments part and its legacy VML drawing.
1244
+ */
1245
+ generatedRels(): {
1246
+ drawing?: string;
1247
+ hyperlinks: Map<string, string>;
1248
+ comments?: string;
1249
+ vmlDrawing?: string;
1250
+ };
861
1251
  /** @internal Captures round-trip state from the source package. */
862
1252
  loadPreservedState(worksheetXml: string, relationships: readonly Relationship[]): void;
1253
+ /**
1254
+ * @internal Package paths of the comment parts this sheet read at load time.
1255
+ * The save pipeline drops them from the preserved set once the sheet takes
1256
+ * ownership, so they are not written twice.
1257
+ */
1258
+ commentPartPaths(): string[];
1259
+ /**
1260
+ * @internal The relationship pointing at this sheet's comments part, if the
1261
+ * sheet was loaded from a file that had one.
1262
+ */
1263
+ commentsPartPath(): string | undefined;
863
1264
  /** Builds the xl/worksheets/sheetN.xml string. */
864
1265
  buildXml(): string;
865
1266
  /** Parses xl/worksheets/sheetN.xml and loads cells into memory. */
866
1267
  loadFromXml(xml: string): void;
1268
+ /** @internal Parses an `xl/comments*.xml` part into this sheet's comments. */
1269
+ loadCommentsXml(xml: string): void;
1270
+ /** @internal The comments part content, when this sheet owns it. */
1271
+ buildCommentsXml(): string;
1272
+ /**
1273
+ * @internal The legacy VML drawing that positions the comment boxes. Excel
1274
+ * needs it to draw the note indicator and popup; the comments part alone
1275
+ * carries only the text.
1276
+ */
1277
+ buildCommentsVml(): string;
867
1278
  }
868
1279
 
869
1280
  /**
@@ -959,6 +1370,23 @@ declare class Workbook {
959
1370
  * Returns a worksheet by name. Throws if not found.
960
1371
  */
961
1372
  getWorksheet(name: string): Worksheet;
1373
+ /**
1374
+ * Inserts rows on one sheet and keeps the whole workbook consistent:
1375
+ * formulas on OTHER sheets that reference the edited sheet, and defined
1376
+ * names, are rewritten too. Prefer this over `Worksheet.insertRows` in a
1377
+ * multi-sheet workbook.
1378
+ *
1379
+ * @returns The edited sheet's result; `refErrors` additionally includes
1380
+ * sheet-qualified refs (e.g. `"Other!B2"`) for affected formula
1381
+ * cells on other sheets.
1382
+ */
1383
+ insertRows(sheetName: string, at: number, count?: number): StructuralEditResult;
1384
+ /** Workbook-wide counterpart to `Worksheet.deleteRows`. See {@link insertRows}. */
1385
+ deleteRows(sheetName: string, at: number, count?: number): StructuralEditResult;
1386
+ /** Workbook-wide counterpart to `Worksheet.insertColumns`. See {@link insertRows}. */
1387
+ insertColumns(sheetName: string, at: number, count?: number): StructuralEditResult;
1388
+ /** Workbook-wide counterpart to `Worksheet.deleteColumns`. See {@link insertRows}. */
1389
+ deleteColumns(sheetName: string, at: number, count?: number): StructuralEditResult;
962
1390
  /**
963
1391
  * Defines (or replaces) a named range.
964
1392
  *
@@ -989,6 +1417,16 @@ declare class Workbook {
989
1417
  * Works in browser and Node.js.
990
1418
  */
991
1419
  saveAsBuffer(): Uint8Array;
1420
+ /**
1421
+ * Same output as {@link saveAsBuffer}, without blocking the calling thread on
1422
+ * compression — for an editor that autosaves, this is the difference between a
1423
+ * visible stall on every save and none.
1424
+ *
1425
+ * The XML for sheets that have not changed since the last save is reused, so a
1426
+ * save after a one-cell edit re-serialises one sheet rather than all of them.
1427
+ * That reuse applies to the synchronous path too; only the compression differs.
1428
+ */
1429
+ saveAsBufferAsync(): Promise<Uint8Array>;
992
1430
  /**
993
1431
  * Saves the workbook to a file (Node.js only).
994
1432
  * Mirrors workbook.SaveAs(filePath) in C#.
@@ -998,6 +1436,20 @@ declare class Workbook {
998
1436
  close(): void;
999
1437
  /** Symbol.dispose support for `using` keyword (TypeScript 5.2+). */
1000
1438
  [Symbol.dispose](): void;
1439
+ /**
1440
+ * The calculation order Excel recorded for this workbook, oldest-first as
1441
+ * stored in `xl/calcChain.xml`, or an empty array when the source file had
1442
+ * none (a freshly created workbook, or one saved by a tool that omits it).
1443
+ *
1444
+ * Excel has already done the topological sort that produced this order, so
1445
+ * evaluating in it avoids repeating that work. Treat it as a hint, not a
1446
+ * guarantee: it reflects the file as opened, and edits made since are not
1447
+ * reflected here.
1448
+ */
1449
+ get calcChain(): readonly {
1450
+ readonly sheet: string;
1451
+ readonly ref: string;
1452
+ }[];
1001
1453
  }
1002
1454
 
1003
1455
  /**
@@ -1006,6 +1458,10 @@ declare class Workbook {
1006
1458
  *
1007
1459
  * Port of TVE.PureDocs.Excel.CellReference (C#)
1008
1460
  */
1461
+ /** Largest 1-based row index a worksheet can have (Excel limit). */
1462
+ declare const EXCEL_MAX_ROWS = 1048576;
1463
+ /** Largest 1-based column index a worksheet can have (column XFD). */
1464
+ declare const EXCEL_MAX_COLUMNS = 16384;
1009
1465
  interface ParsedCellRef {
1010
1466
  readonly row: number;
1011
1467
  readonly column: number;
@@ -1047,6 +1503,15 @@ declare function columnLetter(column: number): string;
1047
1503
  * columnNumber("AA") // 27
1048
1504
  */
1049
1505
  declare function columnNumber(letters: string): number;
1506
+ /**
1507
+ * Formats start/end coordinates as a range reference string. Collapses to a
1508
+ * single cell reference when start and end are the same cell.
1509
+ *
1510
+ * @example
1511
+ * formatRangeRef(1, 1, 10, 2) // "A1:B10"
1512
+ * formatRangeRef(1, 1, 1, 1) // "A1"
1513
+ */
1514
+ declare function formatRangeRef(startRow: number, startColumn: number, endRow: number, endColumn: number): string;
1050
1515
  /**
1051
1516
  * Parses a range reference string like "A1:B10" into start/end coordinates.
1052
1517
  */
@@ -1057,6 +1522,24 @@ declare function parseRangeRef(rangeReference: string): {
1057
1522
  endColumn: number;
1058
1523
  };
1059
1524
 
1525
+ interface FormulaRewriteResult {
1526
+ /** The rewritten formula text (without leading '='). */
1527
+ readonly text: string;
1528
+ /** True when any reference was actually rewritten. */
1529
+ readonly changed: boolean;
1530
+ /** True when at least one reference was replaced by `#REF!`. */
1531
+ readonly hasRefError: boolean;
1532
+ }
1533
+ /**
1534
+ * Rewrites the references in one formula for a structural edit.
1535
+ *
1536
+ * @param formula Formula text without the leading '='.
1537
+ * @param shift The structural edit that happened.
1538
+ * @param formulaSheet Name of the sheet this formula lives on — unqualified
1539
+ * references resolve against it.
1540
+ */
1541
+ declare function shiftFormulaRefs(formula: string, shift: RefShift, formulaSheet: string): FormulaRewriteResult;
1542
+
1060
1543
  /**
1061
1544
  * OLE Automation Date (OADate) helpers.
1062
1545
  *
@@ -1102,8 +1585,8 @@ declare function isDateFormatCode(formatCode: string): boolean;
1102
1585
  * raw value rather than display something wrong.
1103
1586
  *
1104
1587
  * Not handled (returns null): fractions (`# ?/?`), scientific (`0.00E+00`),
1105
- * date/time codes (handled by the date path in Cell), and conditional sections
1106
- * such as `[<=9999999]###-####`.
1588
+ * date/time codes (see date-format-renderer.ts, which Cell routes Date values
1589
+ * to), and conditional sections such as `[<=9999999]###-####`.
1107
1590
  */
1108
1591
  /**
1109
1592
  * Applies `formatCode` to `value`.
@@ -1113,6 +1596,30 @@ declare function isDateFormatCode(formatCode: string): boolean;
1113
1596
  */
1114
1597
  declare function formatNumberWithCode(value: number, formatCode: string): string | null;
1115
1598
 
1599
+ /**
1600
+ * Renders a Date through an OOXML date/time format code.
1601
+ *
1602
+ * Deliberately a *subset* of the full spec, mirroring the philosophy of
1603
+ * number-format-renderer: it covers the codes an editor actually round-trips
1604
+ * (`yyyy`, `yy`, `m`→`mmmm`, `d`→`dddd`, `h`/`hh`, `s`/`ss`, `AM/PM`, quoted
1605
+ * literals, separators) and returns `null` for anything it does not
1606
+ * understand — elapsed-time brackets (`[h]`), fractional seconds (`ss.00`),
1607
+ * era codes — so callers can fall back rather than display something wrong.
1608
+ *
1609
+ * The `m` month-vs-minute ambiguity uses the standard OOXML rule: `m`/`mm`
1610
+ * means minutes when the previous time token is hours or the next one is
1611
+ * seconds; otherwise it means month.
1612
+ */
1613
+ /**
1614
+ * Formats a Date with an OOXML date/time format code, e.g. a value of
1615
+ * 2025-01-31 under `dd/mm/yyyy` renders as `31/01/2025`.
1616
+ *
1617
+ * Returns `null` when the code uses constructs outside the supported subset
1618
+ * (elapsed time `[h]`, fractional seconds, era codes) — callers should fall
1619
+ * back to a locale default rather than render something misleading.
1620
+ */
1621
+ declare function formatDateCode(date: Date, formatCode: string): string | null;
1622
+
1116
1623
  /**
1117
1624
  * Cross-platform XML parser.
1118
1625
  *
@@ -1131,6 +1638,12 @@ type XmlElement = {
1131
1638
  /**
1132
1639
  * Parses an XML string into a simple XmlElement tree.
1133
1640
  */
1641
+ /**
1642
+ * Decodes the five predefined XML entities. `&amp;` is expanded last so that an
1643
+ * escaped entity such as `&amp;lt;` decodes to the literal text `&lt;` rather
1644
+ * than being expanded twice.
1645
+ */
1646
+ declare function decodeXmlEntities(value: string): string;
1134
1647
  declare function parseXml(xmlString: string): XmlElement;
1135
1648
  /**
1136
1649
  * Decodes a Uint8Array to a UTF-8 string.
@@ -1196,6 +1709,9 @@ declare const REL_TYPES: {
1196
1709
  readonly chart: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/chart";
1197
1710
  readonly drawing: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
1198
1711
  readonly hyperlink: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
1712
+ readonly comments: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments";
1713
+ readonly vmlDrawing: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing";
1714
+ readonly calcChain: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/calcChain";
1199
1715
  };
1200
1716
  declare const CONTENT_TYPES: {
1201
1717
  readonly workbook: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml";
@@ -1206,6 +1722,9 @@ declare const CONTENT_TYPES: {
1206
1722
  readonly chart: "application/vnd.openxmlformats-officedocument.drawingml.chart+xml";
1207
1723
  readonly relationships: "application/vnd.openxmlformats-package.relationships+xml";
1208
1724
  readonly coreProperties: "application/vnd.openxmlformats-package.core-properties+xml";
1725
+ readonly comments: "application/vnd.openxmlformats-officedocument.spreadsheetml.comments+xml";
1726
+ readonly vmlDrawing: "application/vnd.openxmlformats-officedocument.vmlDrawing";
1727
+ readonly calcChain: "application/vnd.openxmlformats-officedocument.spreadsheetml.calcChain+xml";
1209
1728
  };
1210
1729
 
1211
1730
  /**
@@ -1229,4 +1748,4 @@ declare function setXmlEntry(entries: ZipInput, path: string, xml: string): void
1229
1748
  */
1230
1749
  declare function setBinaryEntry(entries: ZipInput, path: string, bytes: Uint8Array): void;
1231
1750
 
1232
- export { CONTENT_TYPES, Cell, type CellData, CellStyle, type CellValue, type ColorXmlAttrs, type DefinedName, DefinedNameManager, type DrawingDependentPart, type DrawingProvider, ExcelAlignment, ExcelBorder, ExcelBorderEdge, ExcelBorderStyle, ExcelColor, ExcelFill, ExcelFont, type ExcelFontOptions, ExcelHorizontalAlignment, ExcelNumberFormat, ExcelPatternType, ExcelReadingOrder, ExcelUnderline, ExcelVerticalAlignRun, ExcelVerticalAlignment, NS, type ParsedCellRef, REL_TYPES, Range, type SheetVisibility, StyleManager, WORKSHEET_DRAWING_REL_ID, Workbook, Worksheet, type XmlAttrs, XmlBuilder, type XmlElement, type ZipEntries, type ZipInput, buildDrawingXml, buildRelsXml, cellRefFromRowCol, columnLetter, columnNumber, decodeUtf8, encodeUtf8, escapeXml, formatNumberWithCode, fromOADate, getAttr, getElementsByTagName, isDateFormatCode, isDateFormatId, listEntries, parseCellRef, parseRangeRef, parseXml, readXmlEntry, requireXmlEntry, setBinaryEntry, setXmlEntry, toOADate, unzipXlsx, xml, zipXlsx };
1751
+ export { CONTENT_TYPES, Cell, type CellComment, type CellData, CellStyle, type CellValue, type ColorXmlAttrs, type DefinedName, DefinedNameManager, type DrawingDependentPart, type DrawingProvider, EXCEL_MAX_COLUMNS, EXCEL_MAX_ROWS, ExcelAlignment, ExcelBorder, ExcelBorderEdge, ExcelBorderStyle, ExcelColor, ExcelFill, ExcelFont, type ExcelFontOptions, ExcelHorizontalAlignment, ExcelNumberFormat, ExcelPatternType, ExcelReadingOrder, ExcelUnderline, ExcelVerticalAlignRun, ExcelVerticalAlignment, type FormulaRewriteResult, type Hyperlink, NS, type ParsedCellRef, REL_TYPES, Range, type RefShift, type SheetProtection, type SheetVisibility, type StructuralEditResult, StyleManager, WORKSHEET_DRAWING_REL_ID, Workbook, Worksheet, type XmlAttrs, XmlBuilder, type XmlElement, type ZipEntries, type ZipInput, buildDrawingXml, buildRelsXml, cellRefFromRowCol, columnLetter, columnNumber, decodeUtf8, decodeXmlEntities, encodeUtf8, escapeXml, formatDateCode, formatNumberWithCode, formatRangeRef, fromOADate, getAttr, getElementsByTagName, isDateFormatCode, isDateFormatId, listEntries, parseCellRef, parseRangeRef, parseXml, readXmlEntry, requireXmlEntry, setBinaryEntry, setXmlEntry, shiftFormulaRefs, shiftIndex, shiftRangeRef, shiftSpan, shiftSqref, toOADate, unzipXlsx, xml, zipXlsx };