@niicojs/excel 0.1.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.
@@ -0,0 +1,745 @@
1
+ /**
2
+ * Cell value types - what a cell can contain
3
+ */
4
+ type CellValue = number | string | boolean | Date | null | CellError;
5
+ /**
6
+ * Represents an Excel error value
7
+ */
8
+ interface CellError {
9
+ error: ErrorType;
10
+ }
11
+ type ErrorType = '#NULL!' | '#DIV/0!' | '#VALUE!' | '#REF!' | '#NAME?' | '#NUM!' | '#N/A' | '#GETTING_DATA';
12
+ /**
13
+ * Discriminator for cell content type
14
+ */
15
+ type CellType = 'number' | 'string' | 'boolean' | 'date' | 'error' | 'empty';
16
+ /**
17
+ * Style definition for cells
18
+ */
19
+ interface CellStyle {
20
+ bold?: boolean;
21
+ italic?: boolean;
22
+ underline?: boolean | 'single' | 'double';
23
+ strike?: boolean;
24
+ fontSize?: number;
25
+ fontName?: string;
26
+ fontColor?: string;
27
+ fill?: string;
28
+ border?: BorderStyle;
29
+ alignment?: Alignment;
30
+ numberFormat?: string;
31
+ }
32
+ interface BorderStyle {
33
+ top?: BorderType;
34
+ bottom?: BorderType;
35
+ left?: BorderType;
36
+ right?: BorderType;
37
+ }
38
+ type BorderType = 'thin' | 'medium' | 'thick' | 'double' | 'dotted' | 'dashed';
39
+ interface Alignment {
40
+ horizontal?: 'left' | 'center' | 'right' | 'justify';
41
+ vertical?: 'top' | 'middle' | 'bottom';
42
+ wrapText?: boolean;
43
+ textRotation?: number;
44
+ }
45
+ /**
46
+ * Cell address with 0-indexed row and column
47
+ */
48
+ interface CellAddress {
49
+ row: number;
50
+ col: number;
51
+ }
52
+ /**
53
+ * Range address with start and end cells
54
+ */
55
+ interface RangeAddress {
56
+ start: CellAddress;
57
+ end: CellAddress;
58
+ }
59
+ /**
60
+ * Internal cell data representation
61
+ */
62
+ interface CellData {
63
+ /** Cell type: n=number, s=string (shared), str=inline string, b=boolean, e=error, d=date */
64
+ t?: 'n' | 's' | 'str' | 'b' | 'e' | 'd';
65
+ /** Raw value */
66
+ v?: number | string | boolean;
67
+ /** Formula (without leading =) */
68
+ f?: string;
69
+ /** Style index */
70
+ s?: number;
71
+ /** Formatted text (cached) */
72
+ w?: string;
73
+ /** Number format */
74
+ z?: string;
75
+ /** Array formula range */
76
+ F?: string;
77
+ /** Dynamic array formula flag */
78
+ D?: boolean;
79
+ /** Shared formula index */
80
+ si?: number;
81
+ }
82
+ /**
83
+ * Pivot table aggregation functions
84
+ */
85
+ type AggregationType = 'sum' | 'count' | 'average' | 'min' | 'max';
86
+ /**
87
+ * Configuration for a value field in a pivot table
88
+ */
89
+ interface PivotValueConfig {
90
+ /** Source field name (column header) */
91
+ field: string;
92
+ /** Aggregation function */
93
+ aggregation: AggregationType;
94
+ /** Display name (e.g., "Sum of Sales") */
95
+ name?: string;
96
+ }
97
+ /**
98
+ * Configuration for creating a pivot table
99
+ */
100
+ interface PivotTableConfig {
101
+ /** Name of the pivot table */
102
+ name: string;
103
+ /** Source data range with sheet name (e.g., "Sheet1!A1:D100") */
104
+ source: string;
105
+ /** Target cell where pivot table will be placed (e.g., "Sheet2!A3") */
106
+ target: string;
107
+ /** Refresh the pivot table data when the file is opened (default: true) */
108
+ refreshOnLoad?: boolean;
109
+ }
110
+ /**
111
+ * Internal representation of a pivot cache field
112
+ */
113
+ interface PivotCacheField {
114
+ /** Field name (from header row) */
115
+ name: string;
116
+ /** Field index (0-based) */
117
+ index: number;
118
+ /** Whether this field contains numbers */
119
+ isNumeric: boolean;
120
+ /** Whether this field contains dates */
121
+ isDate: boolean;
122
+ /** Unique string values (for shared items) */
123
+ sharedItems: string[];
124
+ /** Min numeric value */
125
+ minValue?: number;
126
+ /** Max numeric value */
127
+ maxValue?: number;
128
+ }
129
+ /**
130
+ * Pivot field axis assignment
131
+ */
132
+ type PivotFieldAxis = 'row' | 'column' | 'filter' | 'value';
133
+
134
+ /**
135
+ * Represents a single cell in a worksheet
136
+ */
137
+ declare class Cell {
138
+ private _row;
139
+ private _col;
140
+ private _data;
141
+ private _worksheet;
142
+ private _dirty;
143
+ constructor(worksheet: Worksheet, row: number, col: number, data?: CellData);
144
+ /**
145
+ * Get the cell address (e.g., 'A1')
146
+ */
147
+ get address(): string;
148
+ /**
149
+ * Get the 0-based row index
150
+ */
151
+ get row(): number;
152
+ /**
153
+ * Get the 0-based column index
154
+ */
155
+ get col(): number;
156
+ /**
157
+ * Get the cell type
158
+ */
159
+ get type(): CellType;
160
+ /**
161
+ * Get the cell value
162
+ */
163
+ get value(): CellValue;
164
+ /**
165
+ * Set the cell value
166
+ */
167
+ set value(val: CellValue);
168
+ /**
169
+ * Write a 2D array of values starting at this cell
170
+ */
171
+ set values(data: CellValue[][]);
172
+ /**
173
+ * Get the formula (without leading '=')
174
+ */
175
+ get formula(): string | undefined;
176
+ /**
177
+ * Set the formula (without leading '=')
178
+ */
179
+ set formula(f: string | undefined);
180
+ /**
181
+ * Get the formatted text (as displayed in Excel)
182
+ */
183
+ get text(): string;
184
+ /**
185
+ * Get the style index
186
+ */
187
+ get styleIndex(): number | undefined;
188
+ /**
189
+ * Set the style index
190
+ */
191
+ set styleIndex(index: number | undefined);
192
+ /**
193
+ * Get the cell style
194
+ */
195
+ get style(): CellStyle;
196
+ /**
197
+ * Set the cell style (merges with existing)
198
+ */
199
+ set style(style: CellStyle);
200
+ /**
201
+ * Check if cell has been modified
202
+ */
203
+ get dirty(): boolean;
204
+ /**
205
+ * Get internal cell data
206
+ */
207
+ get data(): CellData;
208
+ /**
209
+ * Check if this cell has a date number format
210
+ */
211
+ private _isDateFormat;
212
+ /**
213
+ * Convert Excel serial date to JavaScript Date
214
+ * Used when reading dates stored as numbers with date formats
215
+ */
216
+ _excelDateToJs(serial: number): Date;
217
+ /**
218
+ * Convert JavaScript Date to Excel serial date
219
+ * Used when writing dates as numbers for Excel compatibility
220
+ */
221
+ _jsDateToExcel(date: Date): number;
222
+ }
223
+
224
+ /**
225
+ * Represents a range of cells in a worksheet
226
+ */
227
+ declare class Range {
228
+ private _worksheet;
229
+ private _range;
230
+ constructor(worksheet: Worksheet, range: RangeAddress);
231
+ /**
232
+ * Get the range address as a string
233
+ */
234
+ get address(): string;
235
+ /**
236
+ * Get the number of rows in the range
237
+ */
238
+ get rowCount(): number;
239
+ /**
240
+ * Get the number of columns in the range
241
+ */
242
+ get colCount(): number;
243
+ /**
244
+ * Get all values in the range as a 2D array
245
+ */
246
+ get values(): CellValue[][];
247
+ /**
248
+ * Set values in the range from a 2D array
249
+ */
250
+ set values(data: CellValue[][]);
251
+ /**
252
+ * Get all formulas in the range as a 2D array
253
+ */
254
+ get formulas(): (string | undefined)[][];
255
+ /**
256
+ * Set formulas in the range from a 2D array
257
+ */
258
+ set formulas(data: (string | undefined)[][]);
259
+ /**
260
+ * Get the style of the top-left cell
261
+ */
262
+ get style(): CellStyle;
263
+ /**
264
+ * Set style for all cells in the range
265
+ */
266
+ set style(style: CellStyle);
267
+ /**
268
+ * Iterate over all cells in the range
269
+ */
270
+ [Symbol.iterator](): Generator<Cell, void, unknown>;
271
+ /**
272
+ * Iterate over cells row by row
273
+ */
274
+ rows(): Generator<Cell[], void, unknown>;
275
+ }
276
+
277
+ /**
278
+ * Represents a worksheet in a workbook
279
+ */
280
+ declare class Worksheet {
281
+ private _name;
282
+ private _workbook;
283
+ private _cells;
284
+ private _xmlNodes;
285
+ private _dirty;
286
+ private _mergedCells;
287
+ private _sheetData;
288
+ constructor(workbook: Workbook, name: string);
289
+ /**
290
+ * Get the workbook this sheet belongs to
291
+ */
292
+ get workbook(): Workbook;
293
+ /**
294
+ * Get the sheet name
295
+ */
296
+ get name(): string;
297
+ /**
298
+ * Set the sheet name
299
+ */
300
+ set name(value: string);
301
+ /**
302
+ * Parse worksheet XML content
303
+ */
304
+ parse(xml: string): void;
305
+ /**
306
+ * Parse the sheetData element to extract cells
307
+ */
308
+ private _parseSheetData;
309
+ /**
310
+ * Parse a cell XML node to CellData
311
+ */
312
+ private _parseCellNode;
313
+ /**
314
+ * Get a cell by address or row/col
315
+ */
316
+ cell(rowOrAddress: number | string, col?: number): Cell;
317
+ /**
318
+ * Get a range of cells
319
+ */
320
+ range(rangeStr: string): Range;
321
+ range(startRow: number, startCol: number, endRow: number, endCol: number): Range;
322
+ /**
323
+ * Merge cells in the given range
324
+ */
325
+ mergeCells(rangeOrStart: string, end?: string): void;
326
+ /**
327
+ * Unmerge cells in the given range
328
+ */
329
+ unmergeCells(rangeStr: string): void;
330
+ /**
331
+ * Get all merged cell ranges
332
+ */
333
+ get mergedCells(): string[];
334
+ /**
335
+ * Check if the worksheet has been modified
336
+ */
337
+ get dirty(): boolean;
338
+ /**
339
+ * Get all cells in the worksheet
340
+ */
341
+ get cells(): Map<string, Cell>;
342
+ /**
343
+ * Generate XML for this worksheet
344
+ */
345
+ toXml(): string;
346
+ /**
347
+ * Build a cell XML node from a Cell object
348
+ */
349
+ private _buildCellNode;
350
+ }
351
+
352
+ /**
353
+ * Manages the shared strings table from xl/sharedStrings.xml
354
+ * Excel stores strings in a shared table to reduce file size
355
+ */
356
+ declare class SharedStrings {
357
+ private strings;
358
+ private stringToIndex;
359
+ private _dirty;
360
+ /**
361
+ * Parse shared strings from XML content
362
+ */
363
+ static parse(xml: string): SharedStrings;
364
+ /**
365
+ * Extract text from a string item (si element)
366
+ * Handles both simple <t> elements and rich text <r> elements
367
+ */
368
+ private extractText;
369
+ /**
370
+ * Get a string by index
371
+ */
372
+ getString(index: number): string | undefined;
373
+ /**
374
+ * Add a string and return its index
375
+ * If the string already exists, returns the existing index
376
+ */
377
+ addString(str: string): number;
378
+ /**
379
+ * Check if the shared strings table has been modified
380
+ */
381
+ get dirty(): boolean;
382
+ /**
383
+ * Get the count of strings
384
+ */
385
+ get count(): number;
386
+ /**
387
+ * Generate XML for the shared strings table
388
+ */
389
+ toXml(): string;
390
+ }
391
+
392
+ /**
393
+ * Manages the styles (xl/styles.xml)
394
+ */
395
+ declare class Styles {
396
+ private _numFmts;
397
+ private _fonts;
398
+ private _fills;
399
+ private _borders;
400
+ private _cellXfs;
401
+ private _xmlNodes;
402
+ private _dirty;
403
+ private _styleCache;
404
+ /**
405
+ * Parse styles from XML content
406
+ */
407
+ static parse(xml: string): Styles;
408
+ /**
409
+ * Create an empty styles object with defaults
410
+ */
411
+ static createDefault(): Styles;
412
+ private _parseFont;
413
+ private _parseFill;
414
+ private _parseBorder;
415
+ private _parseCellXf;
416
+ private _parseAlignment;
417
+ /**
418
+ * Get a style by index
419
+ */
420
+ getStyle(index: number): CellStyle;
421
+ /**
422
+ * Create a style and return its index
423
+ * Uses caching to deduplicate identical styles
424
+ */
425
+ createStyle(style: CellStyle): number;
426
+ private _findOrCreateFont;
427
+ private _findOrCreateFill;
428
+ private _findOrCreateBorder;
429
+ private _findOrCreateNumFmt;
430
+ /**
431
+ * Check if styles have been modified
432
+ */
433
+ get dirty(): boolean;
434
+ /**
435
+ * Generate XML for styles
436
+ */
437
+ toXml(): string;
438
+ private _buildFontNode;
439
+ private _buildFillNode;
440
+ private _buildBorderNode;
441
+ private _buildXfNode;
442
+ }
443
+
444
+ /**
445
+ * Manages the pivot cache (definition and records) for a pivot table.
446
+ * The cache stores source data metadata and cached values.
447
+ */
448
+ declare class PivotCache {
449
+ private _cacheId;
450
+ private _sourceSheet;
451
+ private _sourceRange;
452
+ private _fields;
453
+ private _records;
454
+ private _recordCount;
455
+ private _refreshOnLoad;
456
+ constructor(cacheId: number, sourceSheet: string, sourceRange: string);
457
+ /**
458
+ * Get the cache ID
459
+ */
460
+ get cacheId(): number;
461
+ /**
462
+ * Set refreshOnLoad option
463
+ */
464
+ set refreshOnLoad(value: boolean);
465
+ /**
466
+ * Get refreshOnLoad option
467
+ */
468
+ get refreshOnLoad(): boolean;
469
+ /**
470
+ * Get the source sheet name
471
+ */
472
+ get sourceSheet(): string;
473
+ /**
474
+ * Get the source range
475
+ */
476
+ get sourceRange(): string;
477
+ /**
478
+ * Get the full source reference (Sheet!Range)
479
+ */
480
+ get sourceRef(): string;
481
+ /**
482
+ * Get the fields in this cache
483
+ */
484
+ get fields(): PivotCacheField[];
485
+ /**
486
+ * Get the number of data records
487
+ */
488
+ get recordCount(): number;
489
+ /**
490
+ * Build the cache from source data.
491
+ * @param headers - Array of column header names
492
+ * @param data - 2D array of data rows (excluding headers)
493
+ */
494
+ buildFromData(headers: string[], data: CellValue[][]): void;
495
+ /**
496
+ * Get field by name
497
+ */
498
+ getField(name: string): PivotCacheField | undefined;
499
+ /**
500
+ * Get field index by name
501
+ */
502
+ getFieldIndex(name: string): number;
503
+ /**
504
+ * Generate the pivotCacheDefinition XML
505
+ */
506
+ toDefinitionXml(recordsRelId: string): string;
507
+ /**
508
+ * Generate the pivotCacheRecords XML
509
+ */
510
+ toRecordsXml(): string;
511
+ }
512
+
513
+ /**
514
+ * Represents an Excel pivot table with a fluent API for configuration.
515
+ */
516
+ declare class PivotTable {
517
+ private _name;
518
+ private _cache;
519
+ private _targetSheet;
520
+ private _targetCell;
521
+ private _targetRow;
522
+ private _targetCol;
523
+ private _rowFields;
524
+ private _columnFields;
525
+ private _valueFields;
526
+ private _filterFields;
527
+ private _pivotTableIndex;
528
+ constructor(name: string, cache: PivotCache, targetSheet: string, targetCell: string, targetRow: number, targetCol: number, pivotTableIndex: number);
529
+ /**
530
+ * Get the pivot table name
531
+ */
532
+ get name(): string;
533
+ /**
534
+ * Get the target sheet name
535
+ */
536
+ get targetSheet(): string;
537
+ /**
538
+ * Get the target cell address
539
+ */
540
+ get targetCell(): string;
541
+ /**
542
+ * Get the pivot cache
543
+ */
544
+ get cache(): PivotCache;
545
+ /**
546
+ * Get the pivot table index (for file naming)
547
+ */
548
+ get index(): number;
549
+ /**
550
+ * Add a field to the row area
551
+ * @param fieldName - Name of the source field (column header)
552
+ */
553
+ addRowField(fieldName: string): this;
554
+ /**
555
+ * Add a field to the column area
556
+ * @param fieldName - Name of the source field (column header)
557
+ */
558
+ addColumnField(fieldName: string): this;
559
+ /**
560
+ * Add a field to the values area with aggregation
561
+ * @param fieldName - Name of the source field (column header)
562
+ * @param aggregation - Aggregation function (sum, count, average, min, max)
563
+ * @param displayName - Optional display name (defaults to "Sum of FieldName")
564
+ */
565
+ addValueField(fieldName: string, aggregation?: AggregationType, displayName?: string): this;
566
+ /**
567
+ * Add a field to the filter (page) area
568
+ * @param fieldName - Name of the source field (column header)
569
+ */
570
+ addFilterField(fieldName: string): this;
571
+ /**
572
+ * Generate the pivotTableDefinition XML
573
+ */
574
+ toXml(): string;
575
+ /**
576
+ * Build a pivotField node for a given field index
577
+ */
578
+ private _buildPivotFieldNode;
579
+ /**
580
+ * Build row items based on unique values in row fields
581
+ */
582
+ private _buildRowItems;
583
+ /**
584
+ * Build column items based on unique values in column fields
585
+ */
586
+ private _buildColItems;
587
+ /**
588
+ * Calculate the location reference for the pivot table output
589
+ */
590
+ private _calculateLocationRef;
591
+ /**
592
+ * Estimate number of rows in pivot table output
593
+ */
594
+ private _estimateRowCount;
595
+ /**
596
+ * Estimate number of columns in pivot table output
597
+ */
598
+ private _estimateColCount;
599
+ /**
600
+ * Convert 0-based column index to letter (A, B, ..., Z, AA, etc.)
601
+ */
602
+ private _colToLetter;
603
+ }
604
+
605
+ /**
606
+ * Represents an Excel workbook (.xlsx file)
607
+ */
608
+ declare class Workbook {
609
+ private _files;
610
+ private _sheets;
611
+ private _sheetDefs;
612
+ private _relationships;
613
+ private _sharedStrings;
614
+ private _styles;
615
+ private _dirty;
616
+ private _pivotTables;
617
+ private _pivotCaches;
618
+ private _nextCacheId;
619
+ private constructor();
620
+ /**
621
+ * Load a workbook from a file path
622
+ */
623
+ static fromFile(path: string): Promise<Workbook>;
624
+ /**
625
+ * Load a workbook from a buffer
626
+ */
627
+ static fromBuffer(data: Uint8Array): Promise<Workbook>;
628
+ /**
629
+ * Create a new empty workbook
630
+ */
631
+ static create(): Workbook;
632
+ /**
633
+ * Get sheet names
634
+ */
635
+ get sheetNames(): string[];
636
+ /**
637
+ * Get number of sheets
638
+ */
639
+ get sheetCount(): number;
640
+ /**
641
+ * Get shared strings table
642
+ */
643
+ get sharedStrings(): SharedStrings;
644
+ /**
645
+ * Get styles
646
+ */
647
+ get styles(): Styles;
648
+ /**
649
+ * Get a worksheet by name or index
650
+ */
651
+ sheet(nameOrIndex: string | number): Worksheet;
652
+ /**
653
+ * Add a new worksheet
654
+ */
655
+ addSheet(name: string, index?: number): Worksheet;
656
+ /**
657
+ * Delete a worksheet by name or index
658
+ */
659
+ deleteSheet(nameOrIndex: string | number): void;
660
+ /**
661
+ * Rename a worksheet
662
+ */
663
+ renameSheet(oldName: string, newName: string): void;
664
+ /**
665
+ * Copy a worksheet
666
+ */
667
+ copySheet(sourceName: string, newName: string): Worksheet;
668
+ /**
669
+ * Create a pivot table from source data.
670
+ *
671
+ * @param config - Pivot table configuration
672
+ * @returns PivotTable instance for fluent configuration
673
+ *
674
+ * @example
675
+ * ```typescript
676
+ * const pivot = wb.createPivotTable({
677
+ * name: 'SalesPivot',
678
+ * source: 'DataSheet!A1:D100',
679
+ * target: 'PivotSheet!A3',
680
+ * });
681
+ *
682
+ * pivot
683
+ * .addRowField('Region')
684
+ * .addColumnField('Product')
685
+ * .addValueField('Sales', 'sum', 'Total Sales');
686
+ * ```
687
+ */
688
+ createPivotTable(config: PivotTableConfig): PivotTable;
689
+ /**
690
+ * Parse a sheet reference like "Sheet1!A1:D100" into sheet name and range
691
+ */
692
+ private _parseSheetRef;
693
+ /**
694
+ * Extract headers and data from a source range
695
+ */
696
+ private _extractSourceData;
697
+ /**
698
+ * Save the workbook to a file
699
+ */
700
+ toFile(path: string): Promise<void>;
701
+ /**
702
+ * Save the workbook to a buffer
703
+ */
704
+ toBuffer(): Promise<Uint8Array>;
705
+ private _parseWorkbook;
706
+ private _parseRelationships;
707
+ private _updateFiles;
708
+ private _updateWorkbookXml;
709
+ private _updateRelationshipsXml;
710
+ private _updateContentTypes;
711
+ /**
712
+ * Generate all pivot table related files
713
+ */
714
+ private _updatePivotTableFiles;
715
+ }
716
+
717
+ /**
718
+ * Parses an Excel cell address (e.g., 'A1', '$B$2') to row/col indices
719
+ * @param address - Cell address string
720
+ * @returns CellAddress with 0-based row and col
721
+ */
722
+ declare const parseAddress: (address: string) => CellAddress;
723
+ /**
724
+ * Converts row/col indices to an Excel cell address
725
+ * @param row - 0-based row index
726
+ * @param col - 0-based column index
727
+ * @returns Cell address string like 'A1'
728
+ */
729
+ declare const toAddress: (row: number, col: number) => string;
730
+ /**
731
+ * Parses an Excel range (e.g., 'A1:B10') to start/end addresses
732
+ * @param range - Range string
733
+ * @returns RangeAddress with start and end
734
+ */
735
+ declare const parseRange: (range: string) => RangeAddress;
736
+ /**
737
+ * Converts a RangeAddress to a range string
738
+ * @param range - RangeAddress object
739
+ * @returns Range string like 'A1:B10'
740
+ */
741
+ declare const toRange: (range: RangeAddress) => string;
742
+
743
+ export { Cell, PivotCache, PivotTable, Range, SharedStrings, Styles, Workbook, Worksheet, parseAddress, parseRange, toAddress, toRange };
744
+ export type { AggregationType, Alignment, BorderStyle, BorderType, CellAddress, CellError, CellStyle, CellType, CellValue, ErrorType, PivotFieldAxis, PivotTableConfig, PivotValueConfig, RangeAddress };
745
+ //# sourceMappingURL=index.d.ts.map