@niicojs/excel 0.2.6 → 0.3.0

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.cts CHANGED
@@ -13,6 +13,10 @@ type ErrorType = '#NULL!' | '#DIV/0!' | '#VALUE!' | '#REF!' | '#NAME?' | '#NUM!'
13
13
  * Discriminator for cell content type
14
14
  */
15
15
  type CellType = 'number' | 'string' | 'boolean' | 'date' | 'error' | 'empty';
16
+ /**
17
+ * Date handling strategy when serializing cell values.
18
+ */
19
+ type DateHandling = 'jsDate' | 'excelSerial' | 'isoString';
16
20
  /**
17
21
  * Style definition for cells
18
22
  */
@@ -24,7 +28,17 @@ interface CellStyle {
24
28
  fontSize?: number;
25
29
  fontName?: string;
26
30
  fontColor?: string;
31
+ fontColorTheme?: number;
32
+ fontColorTint?: number;
33
+ fontColorIndexed?: number;
27
34
  fill?: string;
35
+ fillTheme?: number;
36
+ fillTint?: number;
37
+ fillIndexed?: number;
38
+ fillBgColor?: string;
39
+ fillBgTheme?: number;
40
+ fillBgTint?: number;
41
+ fillBgIndexed?: number;
28
42
  border?: BorderStyle;
29
43
  alignment?: Alignment;
30
44
  numberFormat?: string;
@@ -83,16 +97,29 @@ interface CellData {
83
97
  * Pivot table aggregation functions
84
98
  */
85
99
  type AggregationType = 'sum' | 'count' | 'average' | 'min' | 'max';
100
+ /**
101
+ * Sort order for pivot fields.
102
+ */
103
+ type PivotSortOrder = 'asc' | 'desc';
104
+ /**
105
+ * Filter configuration for pivot fields.
106
+ */
107
+ interface PivotFieldFilter {
108
+ include?: string[];
109
+ exclude?: string[];
110
+ }
86
111
  /**
87
112
  * Configuration for a value field in a pivot table
88
113
  */
89
114
  interface PivotValueConfig {
90
115
  /** Source field name (column header) */
91
116
  field: string;
92
- /** Aggregation function */
93
- aggregation: AggregationType;
117
+ /** Aggregation function (default: 'sum') */
118
+ aggregation?: AggregationType;
94
119
  /** Display name (e.g., "Sum of Sales") */
95
120
  name?: string;
121
+ /** Number format (e.g., '$#,##0.00', '0.00%') */
122
+ numberFormat?: string;
96
123
  }
97
124
  /**
98
125
  * Configuration for creating a pivot table
@@ -168,6 +195,40 @@ interface RichCellValue {
168
195
  /** Cell style */
169
196
  style?: CellStyle;
170
197
  }
198
+ /**
199
+ * Configuration for creating an Excel Table (ListObject)
200
+ */
201
+ interface TableConfig {
202
+ /** Table name (must be unique within the workbook) */
203
+ name: string;
204
+ /** Data range including headers (e.g., "A1:D10") */
205
+ range: string;
206
+ /** First row contains headers (default: true) */
207
+ headerRow?: boolean;
208
+ /** Show total row at the bottom (default: false) */
209
+ totalRow?: boolean;
210
+ /** Table style configuration */
211
+ style?: TableStyleConfig;
212
+ }
213
+ /**
214
+ * Table style configuration options
215
+ */
216
+ interface TableStyleConfig {
217
+ /** Built-in table style name (e.g., "TableStyleMedium2", "TableStyleLight1") */
218
+ name?: string;
219
+ /** Show banded/alternating row colors (default: true) */
220
+ showRowStripes?: boolean;
221
+ /** Show banded/alternating column colors (default: false) */
222
+ showColumnStripes?: boolean;
223
+ /** Highlight first column with special formatting (default: false) */
224
+ showFirstColumn?: boolean;
225
+ /** Highlight last column with special formatting (default: false) */
226
+ showLastColumn?: boolean;
227
+ }
228
+ /**
229
+ * Aggregation functions available for table total row
230
+ */
231
+ type TableTotalFunction = 'sum' | 'count' | 'average' | 'min' | 'max' | 'stdDev' | 'var' | 'countNums' | 'none';
171
232
  /**
172
233
  * Configuration for converting a sheet to JSON objects.
173
234
  */
@@ -200,6 +261,10 @@ interface SheetToJsonConfig {
200
261
  * If true, stop reading when an empty row is encountered. Defaults to true.
201
262
  */
202
263
  stopOnEmptyRow?: boolean;
264
+ /**
265
+ * How to serialize Date values. Defaults to 'jsDate'.
266
+ */
267
+ dateHandling?: DateHandling;
203
268
  }
204
269
 
205
270
  /**
@@ -315,6 +380,12 @@ declare class Range {
315
380
  * Get all values in the range as a 2D array
316
381
  */
317
382
  get values(): CellValue[][];
383
+ /**
384
+ * Get all values in the range as a 2D array with options
385
+ */
386
+ getValues(options?: {
387
+ createMissing?: boolean;
388
+ }): CellValue[][];
318
389
  /**
319
390
  * Set values in the range from a 2D array
320
391
  */
@@ -345,6 +416,122 @@ declare class Range {
345
416
  rows(): Generator<Cell[], void, unknown>;
346
417
  }
347
418
 
419
+ /**
420
+ * Represents an Excel Table (ListObject) with auto-filter, banded styling, and total row.
421
+ */
422
+ declare class Table {
423
+ private _name;
424
+ private _displayName;
425
+ private _worksheet;
426
+ private _range;
427
+ private _baseRange;
428
+ private _totalRow;
429
+ private _autoFilter;
430
+ private _style;
431
+ private _columns;
432
+ private _id;
433
+ private _dirty;
434
+ private _headerRow;
435
+ constructor(worksheet: Worksheet, config: TableConfig, tableId: number);
436
+ /**
437
+ * Get the table name
438
+ */
439
+ get name(): string;
440
+ /**
441
+ * Get the table display name
442
+ */
443
+ get displayName(): string;
444
+ /**
445
+ * Get the table ID
446
+ */
447
+ get id(): number;
448
+ /**
449
+ * Get the worksheet this table belongs to
450
+ */
451
+ get worksheet(): Worksheet;
452
+ /**
453
+ * Get the table range address string
454
+ */
455
+ get range(): string;
456
+ /**
457
+ * Get the base range excluding total row
458
+ */
459
+ get baseRange(): string;
460
+ /**
461
+ * Get the table range as RangeAddress
462
+ */
463
+ get rangeAddress(): RangeAddress;
464
+ /**
465
+ * Get column names
466
+ */
467
+ get columns(): string[];
468
+ /**
469
+ * Check if table has a total row
470
+ */
471
+ get hasTotalRow(): boolean;
472
+ /**
473
+ * Check if table has a header row
474
+ */
475
+ get hasHeaderRow(): boolean;
476
+ /**
477
+ * Check if table has auto-filter enabled
478
+ */
479
+ get hasAutoFilter(): boolean;
480
+ /**
481
+ * Get the current style configuration
482
+ */
483
+ get style(): TableStyleConfig;
484
+ /**
485
+ * Check if the table has been modified
486
+ */
487
+ get dirty(): boolean;
488
+ /**
489
+ * Set a total function for a column
490
+ * @param columnName - Name of the column (header text)
491
+ * @param fn - Aggregation function to use
492
+ * @returns this for method chaining
493
+ */
494
+ setTotalFunction(columnName: string, fn: TableTotalFunction): this;
495
+ /**
496
+ * Get total function for a column if set
497
+ */
498
+ getTotalFunction(columnName: string): TableTotalFunction | undefined;
499
+ /**
500
+ * Enable or disable auto-filter
501
+ * @param enabled - Whether auto-filter should be enabled
502
+ * @returns this for method chaining
503
+ */
504
+ setAutoFilter(enabled: boolean): this;
505
+ /**
506
+ * Update table style configuration
507
+ * @param style - Style options to apply
508
+ * @returns this for method chaining
509
+ */
510
+ setStyle(style: Partial<TableStyleConfig>): this;
511
+ /**
512
+ * Enable or disable the total row
513
+ * @param enabled - Whether total row should be shown
514
+ * @returns this for method chaining
515
+ */
516
+ setTotalRow(enabled: boolean): this;
517
+ /**
518
+ * Extract column names from the header row of the worksheet
519
+ */
520
+ private _extractColumns;
521
+ /**
522
+ * Write the SUBTOTAL formula to a total row cell
523
+ */
524
+ private _writeTotalRowFormula;
525
+ /**
526
+ * Get the auto-filter range (excludes total row if present)
527
+ */
528
+ private _getAutoFilterRange;
529
+ /**
530
+ * Generate the table definition XML
531
+ */
532
+ toXml(): string;
533
+ }
534
+
348
535
  /**
349
536
  * Represents a worksheet in a workbook
350
537
  */
@@ -356,6 +543,17 @@ declare class Worksheet {
356
543
  private _dirty;
357
544
  private _mergedCells;
358
545
  private _sheetData;
546
+ private _columnWidths;
547
+ private _rowHeights;
548
+ private _frozenPane;
549
+ private _dataBoundsCache;
550
+ private _boundsDirty;
551
+ private _tables;
552
+ private _preserveXml;
553
+ private _tableRelIds;
554
+ private _sheetViewsDirty;
555
+ private _colsDirty;
556
+ private _tablePartsDirty;
359
557
  constructor(workbook: Workbook, name: string);
360
558
  /**
361
559
  * Get the workbook this sheet belongs to
@@ -385,6 +583,10 @@ declare class Worksheet {
385
583
  * Get a cell by address or row/col
386
584
  */
387
585
  cell(rowOrAddress: number | string, col?: number): Cell;
586
+ /**
587
+ * Get an existing cell without creating it.
588
+ */
589
+ getCellIfExists(rowOrAddress: number | string, col?: number): Cell | undefined;
388
590
  /**
389
591
  * Get a range of cells
390
592
  */
@@ -410,6 +612,81 @@ declare class Worksheet {
410
612
  * Get all cells in the worksheet
411
613
  */
412
614
  get cells(): Map<string, Cell>;
615
+ /**
616
+ * Set a column width (0-based index or column letter)
617
+ */
618
+ setColumnWidth(col: number | string, width: number): void;
619
+ /**
620
+ * Get a column width if set
621
+ */
622
+ getColumnWidth(col: number | string): number | undefined;
623
+ /**
624
+ * Set a row height (0-based index)
625
+ */
626
+ setRowHeight(row: number, height: number): void;
627
+ /**
628
+ * Get a row height if set
629
+ */
630
+ getRowHeight(row: number): number | undefined;
631
+ /**
632
+ * Freeze panes at a given row/column split (counts from top-left)
633
+ */
634
+ freezePane(rowSplit: number, colSplit: number): void;
635
+ /**
636
+ * Get current frozen pane configuration
637
+ */
638
+ getFrozenPane(): {
639
+ row: number;
640
+ col: number;
641
+ } | null;
642
+ /**
643
+ * Get all tables in the worksheet
644
+ */
645
+ get tables(): Table[];
646
+ /**
647
+ * Get column width entries
648
+ * @internal
649
+ */
650
+ getColumnWidths(): Map<number, number>;
651
+ /**
652
+ * Get row height entries
653
+ * @internal
654
+ */
655
+ getRowHeights(): Map<number, number>;
656
+ /**
657
+ * Set table relationship IDs for tableParts generation.
658
+ * @internal
659
+ */
660
+ setTableRelIds(ids: string[] | null): void;
661
+ /**
662
+ * Create an Excel Table (ListObject) from a data range.
663
+ *
664
+ * Tables provide structured data features like auto-filter, banded styling,
665
+ * and total row with aggregation functions.
666
+ *
667
+ * @param config - Table configuration
668
+ * @returns Table instance for method chaining
669
+ *
670
+ * @example
671
+ * ```typescript
672
+ * // Create a table with default styling
673
+ * const table = sheet.createTable({
674
+ * name: 'SalesData',
675
+ * range: 'A1:D10',
676
+ * });
677
+ *
678
+ * // Create a table with total row
679
+ * const table = sheet.createTable({
680
+ * name: 'SalesData',
681
+ * range: 'A1:D10',
682
+ * totalRow: true,
683
+ * style: { name: 'TableStyleMedium2' }
684
+ * });
685
+ *
686
+ * table.setTotalFunction('Sales', 'sum');
687
+ * ```
688
+ */
689
+ createTable(config: TableConfig): Table;
413
690
  /**
414
691
  * Convert sheet data to an array of JSON objects.
415
692
  *
@@ -429,6 +706,7 @@ declare class Worksheet {
429
706
  * ```
430
707
  */
431
708
  toJson<T = Record<string, CellValue>>(config?: SheetToJsonConfig): T[];
709
+ private _serializeDate;
432
710
  /**
433
711
  * Get the bounds of data in the sheet (min/max row and column with data)
434
712
  */
@@ -437,6 +715,12 @@ declare class Worksheet {
437
715
  * Generate XML for this worksheet
438
716
  */
439
717
  toXml(): string;
718
+ private _buildSheetDataNode;
719
+ private _buildSheetViewsNode;
720
+ private _buildColsNode;
721
+ private _buildMergeCellsNode;
722
+ private _buildTablePartsNode;
723
+ private _buildPreservedWorksheet;
440
724
  /**
441
725
  * Build a cell XML node from a Cell object
442
726
  */
@@ -448,9 +732,10 @@ declare class Worksheet {
448
732
  * Excel stores strings in a shared table to reduce file size
449
733
  */
450
734
  declare class SharedStrings {
451
- private strings;
735
+ private entries;
452
736
  private stringToIndex;
453
737
  private _dirty;
738
+ private _totalCount;
454
739
  /**
455
740
  * Parse shared strings from XML content
456
741
  */
@@ -477,6 +762,10 @@ declare class SharedStrings {
477
762
  * Get the count of strings
478
763
  */
479
764
  get count(): number;
765
+ /**
766
+ * Get total usage count of shared strings
767
+ */
768
+ get totalCount(): number;
480
769
  /**
481
770
  * Generate XML for the shared strings table
482
771
  */
@@ -495,6 +784,14 @@ declare class Styles {
495
784
  private _xmlNodes;
496
785
  private _dirty;
497
786
  private _styleCache;
787
+ private _styleObjectCache;
788
+ /**
789
+ * Generate a deterministic cache key for a style object.
790
+ * More efficient than JSON.stringify as it avoids the overhead of
791
+ * full JSON serialization and produces a consistent key regardless
792
+ * of property order.
793
+ */
794
+ private _getStyleKey;
498
795
  /**
499
796
  * Parse styles from XML content
500
797
  */
@@ -517,6 +814,10 @@ declare class Styles {
517
814
  * Uses caching to deduplicate identical styles
518
815
  */
519
816
  createStyle(style: CellStyle): number;
817
+ /**
818
+ * Clone an existing style by index, optionally overriding fields.
819
+ */
820
+ cloneStyle(index: number, overrides?: Partial<CellStyle>): number;
520
821
  private _findOrCreateFont;
521
822
  private _findOrCreateFill;
522
823
  private _findOrCreateBorder;
@@ -537,6 +838,8 @@ declare class Styles {
537
838
  toXml(): string;
538
839
  private _buildFontNode;
539
840
  private _buildFillNode;
841
+ private _toStyleColor;
842
+ private _colorsEqual;
540
843
  private _buildBorderNode;
541
844
  private _buildXfNode;
542
845
  }
@@ -547,6 +850,7 @@ declare class Styles {
547
850
  */
548
851
  declare class PivotCache {
549
852
  private _cacheId;
853
+ private _fileIndex;
550
854
  private _sourceSheet;
551
855
  private _sourceRange;
552
856
  private _fields;
@@ -554,11 +858,15 @@ declare class PivotCache {
554
858
  private _recordCount;
555
859
  private _refreshOnLoad;
556
860
  private _sharedItemsIndexMap;
557
- constructor(cacheId: number, sourceSheet: string, sourceRange: string);
861
+ constructor(cacheId: number, sourceSheet: string, sourceRange: string, fileIndex: number);
558
862
  /**
559
863
  * Get the cache ID
560
864
  */
561
865
  get cacheId(): number;
866
+ /**
867
+ * Get the file index for this cache (used for file naming).
868
+ */
869
+ get fileIndex(): number;
562
870
  /**
563
871
  * Set refreshOnLoad option
564
872
  */
@@ -625,9 +933,11 @@ declare class PivotTable {
625
933
  private _columnFields;
626
934
  private _valueFields;
627
935
  private _filterFields;
936
+ private _fieldAssignments;
628
937
  private _pivotTableIndex;
938
+ private _cacheFileIndex;
629
939
  private _styles;
630
- constructor(name: string, cache: PivotCache, targetSheet: string, targetCell: string, targetRow: number, targetCol: number, pivotTableIndex: number);
940
+ constructor(name: string, cache: PivotCache, targetSheet: string, targetCell: string, targetRow: number, targetCol: number, pivotTableIndex: number, cacheFileIndex: number);
631
941
  /**
632
942
  * Get the pivot table name
633
943
  */
@@ -648,6 +958,11 @@ declare class PivotTable {
648
958
  * Get the pivot table index (for file naming)
649
959
  */
650
960
  get index(): number;
961
+ /**
962
+ * Get the pivot cache file index used for rels.
963
+ * @internal
964
+ */
965
+ get cacheFileIndex(): number;
651
966
  /**
652
967
  * Set the styles reference for number format resolution
653
968
  * @internal
@@ -664,18 +979,38 @@ declare class PivotTable {
664
979
  */
665
980
  addColumnField(fieldName: string): this;
666
981
  /**
667
- * Add a field to the values area with aggregation
668
- * @param fieldName - Name of the source field (column header)
669
- * @param aggregation - Aggregation function (sum, count, average, min, max)
670
- * @param displayName - Optional display name (defaults to "Sum of FieldName")
671
- * @param numberFormat - Optional number format (e.g., '$#,##0.00', '0.00%')
982
+ * Add a field to the values area with aggregation.
983
+ *
984
+ * Supports two call signatures:
985
+ * - Positional: `addValueField(fieldName, aggregation?, displayName?, numberFormat?)`
986
+ * - Object: `addValueField({ field, aggregation?, name?, numberFormat? })`
987
+ *
988
+ * @example
989
+ * // Positional arguments
990
+ * pivot.addValueField('Sales', 'sum', 'Total Sales', '$#,##0.00');
991
+ *
992
+ * // Object form
993
+ * pivot.addValueField({ field: 'Sales', aggregation: 'sum', name: 'Total Sales', numberFormat: '$#,##0.00' });
672
994
  */
995
+ addValueField(config: PivotValueConfig): this;
673
996
  addValueField(fieldName: string, aggregation?: AggregationType, displayName?: string, numberFormat?: string): this;
674
997
  /**
675
998
  * Add a field to the filter (page) area
676
999
  * @param fieldName - Name of the source field (column header)
677
1000
  */
678
1001
  addFilterField(fieldName: string): this;
1002
+ /**
1003
+ * Set a sort order for a row or column field
1004
+ * @param fieldName - Name of the field to sort
1005
+ * @param order - Sort order ('asc' or 'desc')
1006
+ */
1007
+ sortField(fieldName: string, order: PivotSortOrder): this;
1008
+ /**
1009
+ * Filter items for a row, column, or filter field
1010
+ * @param fieldName - Name of the field to filter
1011
+ * @param filter - Filter configuration with include or exclude list
1012
+ */
1013
+ filterField(fieldName: string, filter: PivotFieldFilter): this;
679
1014
  /**
680
1015
  * Generate the pivotTableDefinition XML
681
1016
  */
@@ -684,6 +1019,10 @@ declare class PivotTable {
684
1019
  * Build a pivotField node for a given field index
685
1020
  */
686
1021
  private _buildPivotFieldNode;
1022
+ /**
1023
+ * Build item nodes for a pivot field, with optional filtering
1024
+ */
1025
+ private _buildItemNodes;
687
1026
  /**
688
1027
  * Build row items based on unique values in row fields
689
1028
  */
@@ -724,6 +1063,9 @@ declare class Workbook {
724
1063
  private _pivotTables;
725
1064
  private _pivotCaches;
726
1065
  private _nextCacheId;
1066
+ private _nextCacheFileIndex;
1067
+ private _nextTableId;
1068
+ private _dateHandling;
727
1069
  private constructor();
728
1070
  /**
729
1071
  * Load a workbook from a file path
@@ -753,6 +1095,20 @@ declare class Workbook {
753
1095
  * Get styles
754
1096
  */
755
1097
  get styles(): Styles;
1098
+ /**
1099
+ * Get the workbook date handling strategy.
1100
+ */
1101
+ get dateHandling(): DateHandling;
1102
+ /**
1103
+ * Set the workbook date handling strategy.
1104
+ */
1105
+ set dateHandling(value: DateHandling);
1106
+ /**
1107
+ * Get the next unique table ID for this workbook.
1108
+ * Table IDs must be unique across all worksheets.
1109
+ * @internal
1110
+ */
1111
+ getNextTableId(): number;
756
1112
  /**
757
1113
  * Get a worksheet by name or index
758
1114
  */
@@ -773,6 +1129,9 @@ declare class Workbook {
773
1129
  * Copy a worksheet
774
1130
  */
775
1131
  copySheet(sourceName: string, newName: string): Worksheet;
1132
+ private _createUniqueTableName;
1133
+ private _sanitizeTableName;
1134
+ private _hasTableName;
776
1135
  /**
777
1136
  * Create a new worksheet from an array of objects.
778
1137
  *
@@ -872,11 +1231,16 @@ declare class Workbook {
872
1231
  private _updateFiles;
873
1232
  private _updateWorkbookXml;
874
1233
  private _updateRelationshipsXml;
1234
+ private _buildRelationshipInfo;
875
1235
  private _updateContentTypes;
876
1236
  /**
877
1237
  * Generate all pivot table related files
878
1238
  */
879
1239
  private _updatePivotTableFiles;
1240
+ /**
1241
+ * Generate all table related files
1242
+ */
1243
+ private _updateTableFiles;
880
1244
  }
881
1245
 
882
1246
  /**
@@ -905,6 +1269,6 @@ declare const parseRange: (range: string) => RangeAddress;
905
1269
  */
906
1270
  declare const toRange: (range: RangeAddress) => string;
907
1271
 
908
- export { Cell, PivotCache, PivotTable, Range, SharedStrings, Styles, Workbook, Worksheet, parseAddress, parseRange, toAddress, toRange };
909
- export type { AggregationType, Alignment, BorderStyle, BorderType, CellAddress, CellError, CellStyle, CellType, CellValue, ColumnConfig, ErrorType, PivotFieldAxis, PivotTableConfig, PivotValueConfig, RangeAddress, RichCellValue, SheetFromDataConfig, SheetToJsonConfig };
1272
+ export { Cell, PivotCache, PivotTable, Range, SharedStrings, Styles, Table, Workbook, Worksheet, parseAddress, parseRange, toAddress, toRange };
1273
+ export type { AggregationType, Alignment, BorderStyle, BorderType, CellAddress, CellError, CellStyle, CellType, CellValue, ColumnConfig, DateHandling, ErrorType, PivotFieldAxis, PivotFieldFilter, PivotSortOrder, PivotTableConfig, PivotValueConfig, RangeAddress, RichCellValue, SheetFromDataConfig, SheetToJsonConfig, TableConfig, TableStyleConfig, TableTotalFunction };
910
1274
  //# sourceMappingURL=index.d.cts.map