@niicojs/excel 0.2.2 → 0.2.4

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
  */
@@ -83,6 +87,17 @@ interface CellData {
83
87
  * Pivot table aggregation functions
84
88
  */
85
89
  type AggregationType = 'sum' | 'count' | 'average' | 'min' | 'max';
90
+ /**
91
+ * Sort order for pivot fields.
92
+ */
93
+ type PivotSortOrder = 'asc' | 'desc';
94
+ /**
95
+ * Filter configuration for pivot fields.
96
+ */
97
+ interface PivotFieldFilter {
98
+ include?: string[];
99
+ exclude?: string[];
100
+ }
86
101
  /**
87
102
  * Configuration for a value field in a pivot table
88
103
  */
@@ -168,6 +183,43 @@ interface RichCellValue {
168
183
  /** Cell style */
169
184
  style?: CellStyle;
170
185
  }
186
+ /**
187
+ * Configuration for converting a sheet to JSON objects.
188
+ */
189
+ interface SheetToJsonConfig {
190
+ /**
191
+ * Field names to use for each column.
192
+ * If provided, the first row of data starts at row 1 (or startRow).
193
+ * If not provided, the first row is used as field names.
194
+ */
195
+ fields?: string[];
196
+ /**
197
+ * Starting row (0-based). Defaults to 0.
198
+ * If fields are not provided, this row contains the headers.
199
+ * If fields are provided, this is the first data row.
200
+ */
201
+ startRow?: number;
202
+ /**
203
+ * Starting column (0-based). Defaults to 0.
204
+ */
205
+ startCol?: number;
206
+ /**
207
+ * Ending row (0-based, inclusive). Defaults to the last row with data.
208
+ */
209
+ endRow?: number;
210
+ /**
211
+ * Ending column (0-based, inclusive). Defaults to the last column with data.
212
+ */
213
+ endCol?: number;
214
+ /**
215
+ * If true, stop reading when an empty row is encountered. Defaults to true.
216
+ */
217
+ stopOnEmptyRow?: boolean;
218
+ /**
219
+ * How to serialize Date values. Defaults to 'jsDate'.
220
+ */
221
+ dateHandling?: DateHandling;
222
+ }
171
223
 
172
224
  /**
173
225
  * Represents a single cell in a worksheet
@@ -282,6 +334,12 @@ declare class Range {
282
334
  * Get all values in the range as a 2D array
283
335
  */
284
336
  get values(): CellValue[][];
337
+ /**
338
+ * Get all values in the range as a 2D array with options
339
+ */
340
+ getValues(options?: {
341
+ createMissing?: boolean;
342
+ }): CellValue[][];
285
343
  /**
286
344
  * Set values in the range from a 2D array
287
345
  */
@@ -323,6 +381,11 @@ declare class Worksheet {
323
381
  private _dirty;
324
382
  private _mergedCells;
325
383
  private _sheetData;
384
+ private _columnWidths;
385
+ private _rowHeights;
386
+ private _frozenPane;
387
+ private _dataBoundsCache;
388
+ private _boundsDirty;
326
389
  constructor(workbook: Workbook, name: string);
327
390
  /**
328
391
  * Get the workbook this sheet belongs to
@@ -352,6 +415,10 @@ declare class Worksheet {
352
415
  * Get a cell by address or row/col
353
416
  */
354
417
  cell(rowOrAddress: number | string, col?: number): Cell;
418
+ /**
419
+ * Get an existing cell without creating it.
420
+ */
421
+ getCellIfExists(rowOrAddress: number | string, col?: number): Cell | undefined;
355
422
  /**
356
423
  * Get a range of cells
357
424
  */
@@ -377,6 +444,57 @@ declare class Worksheet {
377
444
  * Get all cells in the worksheet
378
445
  */
379
446
  get cells(): Map<string, Cell>;
447
+ /**
448
+ * Set a column width (0-based index or column letter)
449
+ */
450
+ setColumnWidth(col: number | string, width: number): void;
451
+ /**
452
+ * Get a column width if set
453
+ */
454
+ getColumnWidth(col: number | string): number | undefined;
455
+ /**
456
+ * Set a row height (0-based index)
457
+ */
458
+ setRowHeight(row: number, height: number): void;
459
+ /**
460
+ * Get a row height if set
461
+ */
462
+ getRowHeight(row: number): number | undefined;
463
+ /**
464
+ * Freeze panes at a given row/column split (counts from top-left)
465
+ */
466
+ freezePane(rowSplit: number, colSplit: number): void;
467
+ /**
468
+ * Get current frozen pane configuration
469
+ */
470
+ getFrozenPane(): {
471
+ row: number;
472
+ col: number;
473
+ } | null;
474
+ /**
475
+ * Convert sheet data to an array of JSON objects.
476
+ *
477
+ * @param config - Configuration options
478
+ * @returns Array of objects where keys are field names and values are cell values
479
+ *
480
+ * @example
481
+ * ```typescript
482
+ * // Using first row as headers
483
+ * const data = sheet.toJson();
484
+ *
485
+ * // Using custom field names
486
+ * const data = sheet.toJson({ fields: ['name', 'age', 'city'] });
487
+ *
488
+ * // Starting from a specific row/column
489
+ * const data = sheet.toJson({ startRow: 2, startCol: 1 });
490
+ * ```
491
+ */
492
+ toJson<T = Record<string, CellValue>>(config?: SheetToJsonConfig): T[];
493
+ private _serializeDate;
494
+ /**
495
+ * Get the bounds of data in the sheet (min/max row and column with data)
496
+ */
497
+ private _getDataBounds;
380
498
  /**
381
499
  * Generate XML for this worksheet
382
500
  */
@@ -439,6 +557,14 @@ declare class Styles {
439
557
  private _xmlNodes;
440
558
  private _dirty;
441
559
  private _styleCache;
560
+ private _styleObjectCache;
561
+ /**
562
+ * Generate a deterministic cache key for a style object.
563
+ * More efficient than JSON.stringify as it avoids the overhead of
564
+ * full JSON serialization and produces a consistent key regardless
565
+ * of property order.
566
+ */
567
+ private _getStyleKey;
442
568
  /**
443
569
  * Parse styles from XML content
444
570
  */
@@ -461,6 +587,10 @@ declare class Styles {
461
587
  * Uses caching to deduplicate identical styles
462
588
  */
463
589
  createStyle(style: CellStyle): number;
590
+ /**
591
+ * Clone an existing style by index, optionally overriding fields.
592
+ */
593
+ cloneStyle(index: number, overrides?: Partial<CellStyle>): number;
464
594
  private _findOrCreateFont;
465
595
  private _findOrCreateFill;
466
596
  private _findOrCreateBorder;
@@ -491,6 +621,7 @@ declare class PivotCache {
491
621
  private _records;
492
622
  private _recordCount;
493
623
  private _refreshOnLoad;
624
+ private _dateGrouping;
494
625
  constructor(cacheId: number, sourceSheet: string, sourceRange: string);
495
626
  /**
496
627
  * Get the cache ID
@@ -562,6 +693,7 @@ declare class PivotTable {
562
693
  private _columnFields;
563
694
  private _valueFields;
564
695
  private _filterFields;
696
+ private _fieldAssignments;
565
697
  private _pivotTableIndex;
566
698
  constructor(name: string, cache: PivotCache, targetSheet: string, targetCell: string, targetRow: number, targetCol: number, pivotTableIndex: number);
567
699
  /**
@@ -606,6 +738,14 @@ declare class PivotTable {
606
738
  * @param fieldName - Name of the source field (column header)
607
739
  */
608
740
  addFilterField(fieldName: string): this;
741
+ /**
742
+ * Set a sort order for a row/column field
743
+ */
744
+ sortField(fieldName: string, order: PivotSortOrder): this;
745
+ /**
746
+ * Filter items for a field (include or exclude list)
747
+ */
748
+ filterField(fieldName: string, filter: PivotFieldFilter): this;
609
749
  /**
610
750
  * Generate the pivotTableDefinition XML
611
751
  */
@@ -614,6 +754,7 @@ declare class PivotTable {
614
754
  * Build a pivotField node for a given field index
615
755
  */
616
756
  private _buildPivotFieldNode;
757
+ private _resolveItemFilter;
617
758
  /**
618
759
  * Build row items based on unique values in row fields
619
760
  */
@@ -654,6 +795,7 @@ declare class Workbook {
654
795
  private _pivotTables;
655
796
  private _pivotCaches;
656
797
  private _nextCacheId;
798
+ private _dateHandling;
657
799
  private constructor();
658
800
  /**
659
801
  * Load a workbook from a file path
@@ -683,6 +825,14 @@ declare class Workbook {
683
825
  * Get styles
684
826
  */
685
827
  get styles(): Styles;
828
+ /**
829
+ * Get the workbook date handling strategy.
830
+ */
831
+ get dateHandling(): DateHandling;
832
+ /**
833
+ * Set the workbook date handling strategy.
834
+ */
835
+ set dateHandling(value: DateHandling);
686
836
  /**
687
837
  * Get a worksheet by name or index
688
838
  */
@@ -809,6 +959,18 @@ declare class Workbook {
809
959
  private _updatePivotTableFiles;
810
960
  }
811
961
 
962
+ /**
963
+ * Converts a column index (0-based) to Excel column letters (A, B, ..., Z, AA, AB, ...)
964
+ * @param col - 0-based column index
965
+ * @returns Column letter(s)
966
+ */
967
+ declare const colToLetter: (col: number) => string;
968
+ /**
969
+ * Converts Excel column letters to a 0-based column index
970
+ * @param letters - Column letter(s) like 'A', 'B', 'AA'
971
+ * @returns 0-based column index
972
+ */
973
+ declare const letterToCol: (letters: string) => number;
812
974
  /**
813
975
  * Parses an Excel cell address (e.g., 'A1', '$B$2') to row/col indices
814
976
  * @param address - Cell address string
@@ -834,7 +996,15 @@ declare const parseRange: (range: string) => RangeAddress;
834
996
  * @returns Range string like 'A1:B10'
835
997
  */
836
998
  declare const toRange: (range: RangeAddress) => string;
999
+ /**
1000
+ * Normalizes a range so start is always top-left and end is bottom-right
1001
+ */
1002
+ declare const normalizeRange: (range: RangeAddress) => RangeAddress;
1003
+ /**
1004
+ * Checks if an address is within a range
1005
+ */
1006
+ declare const isInRange: (addr: CellAddress, range: RangeAddress) => boolean;
837
1007
 
838
- export { Cell, PivotCache, PivotTable, Range, SharedStrings, Styles, Workbook, Worksheet, parseAddress, parseRange, toAddress, toRange };
839
- export type { AggregationType, Alignment, BorderStyle, BorderType, CellAddress, CellError, CellStyle, CellType, CellValue, ColumnConfig, ErrorType, PivotFieldAxis, PivotTableConfig, PivotValueConfig, RangeAddress, RichCellValue, SheetFromDataConfig };
1008
+ export { Cell, PivotCache, PivotTable, Range, SharedStrings, Styles, Workbook, Worksheet, colToLetter, isInRange, letterToCol, normalizeRange, parseAddress, parseRange, toAddress, toRange };
1009
+ export type { AggregationType, Alignment, BorderStyle, BorderType, CellAddress, CellError, CellStyle, CellType, CellValue, ColumnConfig, DateHandling, ErrorType, PivotFieldAxis, PivotFieldFilter, PivotSortOrder, PivotTableConfig, PivotValueConfig, RangeAddress, RichCellValue, SheetFromDataConfig, SheetToJsonConfig };
840
1010
  //# sourceMappingURL=index.d.cts.map