@niicojs/excel 0.3.4 → 0.3.5

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
@@ -93,82 +93,6 @@ interface CellData {
93
93
  /** Shared formula index */
94
94
  si?: number;
95
95
  }
96
- /**
97
- * Pivot table aggregation functions
98
- */
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
- }
111
- /**
112
- * Configuration for a value field in a pivot table
113
- */
114
- interface PivotValueConfig {
115
- /** Source field name (column header) */
116
- field: string;
117
- /** Aggregation function (default: 'sum') */
118
- aggregation?: AggregationType;
119
- /** Display name (e.g., "Sum of Sales") */
120
- name?: string;
121
- /** Number format (e.g., '$#,##0.00', '0.00%') */
122
- numberFormat?: string;
123
- }
124
- /**
125
- * Configuration for creating a pivot table
126
- */
127
- interface PivotTableConfig {
128
- /** Name of the pivot table */
129
- name: string;
130
- /** Source data range with sheet name (e.g., "Sheet1!A1:D100") */
131
- source: string;
132
- /** Target cell where pivot table will be placed (e.g., "Sheet2!A3") */
133
- target: string;
134
- /** Refresh the pivot table data when the file is opened (default: true) */
135
- refreshOnLoad?: boolean;
136
- /** Save pivot cache data in the file (default: true) */
137
- saveData?: boolean;
138
- }
139
- /**
140
- * Internal representation of a pivot cache field
141
- */
142
- interface PivotCacheField {
143
- /** Field name (from header row) */
144
- name: string;
145
- /** Field index (0-based) */
146
- index: number;
147
- /** Whether this field contains numbers */
148
- isNumeric: boolean;
149
- /** Whether this field contains dates */
150
- isDate: boolean;
151
- /** Whether this field contains boolean values */
152
- hasBoolean: boolean;
153
- /** Whether this field contains blank (null/undefined) values */
154
- hasBlank: boolean;
155
- /** Number format ID for this field (cache field numFmtId) */
156
- numFmtId?: number;
157
- /** Unique string values (for shared items) */
158
- sharedItems: string[];
159
- /** Min numeric value */
160
- minValue?: number;
161
- /** Max numeric value */
162
- maxValue?: number;
163
- /** Min date value (for date fields) */
164
- minDate?: Date;
165
- /** Max date value (for date fields) */
166
- maxDate?: Date;
167
- }
168
- /**
169
- * Pivot field axis assignment
170
- */
171
- type PivotFieldAxis = 'row' | 'column' | 'filter' | 'value';
172
96
  /**
173
97
  * Configuration for creating a sheet from an array of objects
174
98
  */
@@ -237,6 +161,49 @@ interface TableStyleConfig {
237
161
  /** Highlight last column with special formatting (default: false) */
238
162
  showLastColumn?: boolean;
239
163
  }
164
+ /**
165
+ * Pivot table aggregation functions.
166
+ */
167
+ type PivotAggregationType = 'sum' | 'count' | 'average' | 'min' | 'max';
168
+ /**
169
+ * Pivot field sort order.
170
+ */
171
+ type PivotSortOrder = 'asc' | 'desc';
172
+ /**
173
+ * Filter definition for pivot fields.
174
+ * Use either include or exclude, not both.
175
+ */
176
+ type PivotFieldFilter = {
177
+ include: string[];
178
+ } | {
179
+ exclude: string[];
180
+ };
181
+ /**
182
+ * Value field configuration for pivot tables.
183
+ */
184
+ interface PivotValueConfig {
185
+ /** Source field name */
186
+ field: string;
187
+ /** Aggregation type (default: 'sum') */
188
+ aggregation?: PivotAggregationType;
189
+ /** Display name for the value field */
190
+ name?: string;
191
+ /** Number format to apply to values */
192
+ numberFormat?: string;
193
+ }
194
+ /**
195
+ * Configuration for creating a PivotTable.
196
+ */
197
+ interface PivotTableConfig {
198
+ /** Pivot table name */
199
+ name: string;
200
+ /** Source data range with sheet name, e.g. "Data!A1:E100" */
201
+ source: string;
202
+ /** Target cell with sheet name, e.g. "Summary!A3" */
203
+ target: string;
204
+ /** Refresh pivot when opening workbook (default: true) */
205
+ refreshOnLoad?: boolean;
206
+ }
240
207
  /**
241
208
  * Aggregation functions available for table total row
242
209
  */
@@ -288,6 +255,16 @@ interface SheetToJsonConfig {
288
255
  */
289
256
  locale?: string;
290
257
  }
258
+ /**
259
+ * Options for reading a workbook from file/buffer.
260
+ */
261
+ interface WorkbookReadOptions {
262
+ /**
263
+ * Enable lazy parsing of ZIP entries and XML parts.
264
+ * Defaults to true.
265
+ */
266
+ lazy?: boolean;
267
+ }
291
268
 
292
269
  /**
293
270
  * Represents a single cell in a worksheet
@@ -568,7 +545,6 @@ declare class Worksheet {
568
545
  private _xmlNodes;
569
546
  private _dirty;
570
547
  private _mergedCells;
571
- private _sheetData;
572
548
  private _columnWidths;
573
549
  private _rowHeights;
574
550
  private _frozenPane;
@@ -576,10 +552,14 @@ declare class Worksheet {
576
552
  private _boundsDirty;
577
553
  private _tables;
578
554
  private _preserveXml;
555
+ private _rawXml;
556
+ private _lazyParse;
579
557
  private _tableRelIds;
558
+ private _pivotTableRelIds;
580
559
  private _sheetViewsDirty;
581
560
  private _colsDirty;
582
561
  private _tablePartsDirty;
562
+ private _pivotTablePartsDirty;
583
563
  constructor(workbook: Workbook, name: string);
584
564
  /**
585
565
  * Get the workbook this sheet belongs to
@@ -596,7 +576,10 @@ declare class Worksheet {
596
576
  /**
597
577
  * Parse worksheet XML content
598
578
  */
599
- parse(xml: string): void;
579
+ parse(xml: string, options?: {
580
+ lazy?: boolean;
581
+ }): void;
582
+ private _ensureParsed;
600
583
  /**
601
584
  * Parse the sheetData element to extract cells
602
585
  */
@@ -684,6 +667,11 @@ declare class Worksheet {
684
667
  * @internal
685
668
  */
686
669
  setTableRelIds(ids: string[] | null): void;
670
+ /**
671
+ * Set pivot table relationship IDs for pivotTableParts generation.
672
+ * @internal
673
+ */
674
+ setPivotTableRelIds(ids: string[] | null): void;
687
675
  /**
688
676
  * Create an Excel Table (ListObject) from a data range.
689
677
  *
@@ -746,6 +734,7 @@ declare class Worksheet {
746
734
  private _buildColsNode;
747
735
  private _buildMergeCellsNode;
748
736
  private _buildTablePartsNode;
737
+ private _buildPivotTablePartsNode;
749
738
  private _buildPreservedWorksheet;
750
739
  /**
751
740
  * Build a cell XML node from a Cell object
@@ -762,10 +751,13 @@ declare class SharedStrings {
762
751
  private stringToIndex;
763
752
  private _dirty;
764
753
  private _totalCount;
754
+ private _rawXml;
755
+ private _parsed;
765
756
  /**
766
757
  * Parse shared strings from XML content
767
758
  */
768
759
  static parse(xml: string): SharedStrings;
760
+ private _parse;
769
761
  /**
770
762
  * Extract text from a string item (si element)
771
763
  * Handles both simple <t> elements and rich text <r> elements
@@ -812,6 +804,8 @@ declare class Styles {
812
804
  private _borders;
813
805
  private _cellXfs;
814
806
  private _xmlNodes;
807
+ private _rawXml;
808
+ private _parsed;
815
809
  private _dirty;
816
810
  private _styleCache;
817
811
  private _styleObjectCache;
@@ -826,6 +820,7 @@ declare class Styles {
826
820
  * Parse styles from XML content
827
821
  */
828
822
  static parse(xml: string): Styles;
823
+ private _parse;
829
824
  /**
830
825
  * Create an empty styles object with defaults
831
826
  */
@@ -874,228 +869,81 @@ declare class Styles {
874
869
  private _buildXfNode;
875
870
  }
876
871
 
877
- /**
878
- * Manages the pivot cache (definition and records) for a pivot table.
879
- * The cache stores source data metadata and cached values.
880
- */
881
- declare class PivotCache {
882
- private _cacheId;
883
- private _fileIndex;
884
- private _sourceSheet;
885
- private _sourceRange;
886
- private _fields;
887
- private _records;
888
- private _recordCount;
889
- private _saveData;
890
- private _refreshOnLoad;
891
- private _sharedItemsIndexMap;
892
- private _blankItemIndexMap;
893
- private _styles;
894
- constructor(cacheId: number, sourceSheet: string, sourceRange: string, fileIndex: number);
895
- /**
896
- * Set styles reference for number format resolution.
897
- * @internal
898
- */
899
- setStyles(styles: Styles): void;
900
- /**
901
- * Get the cache ID
902
- */
903
- get cacheId(): number;
904
- /**
905
- * Get the file index for this cache (used for file naming).
906
- */
907
- get fileIndex(): number;
908
- /**
909
- * Set refreshOnLoad option
910
- */
911
- set refreshOnLoad(value: boolean);
912
- /**
913
- * Set saveData option
914
- */
915
- set saveData(value: boolean);
916
- /**
917
- * Get refreshOnLoad option
918
- */
919
- get refreshOnLoad(): boolean;
920
- /**
921
- * Get saveData option
922
- */
923
- get saveData(): boolean;
924
- /**
925
- * Get the source sheet name
926
- */
927
- get sourceSheet(): string;
928
- /**
929
- * Get the source range
930
- */
931
- get sourceRange(): string;
932
- /**
933
- * Get the full source reference (Sheet!Range)
934
- */
935
- get sourceRef(): string;
936
- /**
937
- * Get the fields in this cache
938
- */
939
- get fields(): PivotCacheField[];
940
- /**
941
- * Get the number of data records
942
- */
943
- get recordCount(): number;
944
- /**
945
- * Build the cache from source data.
946
- * @param headers - Array of column header names
947
- * @param data - 2D array of data rows (excluding headers)
948
- */
949
- buildFromData(headers: string[], data: CellValue[][]): void;
950
- /**
951
- * Get field by name
952
- */
953
- getField(name: string): PivotCacheField | undefined;
954
- /**
955
- * Get field index by name
956
- */
957
- getFieldIndex(name: string): number;
958
- /**
959
- * Generate the pivotCacheDefinition XML
960
- */
961
- toDefinitionXml(recordsRelId: string): string;
962
- /**
963
- * Generate the pivotCacheRecords XML
964
- */
965
- toRecordsXml(): string;
966
- private _formatDate;
967
- private _formatNumber;
968
- private _excelSerialToDate;
872
+ interface PivotFieldMeta {
873
+ name: string;
874
+ sourceCol: number;
969
875
  }
970
-
971
876
  /**
972
- * Represents an Excel pivot table with a fluent API for configuration.
877
+ * Represents an Excel PivotTable with a fluent configuration API.
973
878
  */
974
879
  declare class PivotTable {
880
+ private _workbook;
975
881
  private _name;
976
- private _cache;
977
- private _targetSheet;
882
+ private _sourceSheetName;
883
+ private _sourceSheet;
884
+ private _sourceRange;
885
+ private _targetSheetName;
978
886
  private _targetCell;
979
- private _targetRow;
980
- private _targetCol;
887
+ private _refreshOnLoad;
888
+ private _cacheId;
889
+ private _pivotId;
890
+ private _cachePartIndex;
891
+ private _fields;
981
892
  private _rowFields;
982
893
  private _columnFields;
983
- private _valueFields;
984
894
  private _filterFields;
985
- private _fieldAssignments;
986
- private _pivotTableIndex;
987
- private _cacheFileIndex;
988
- private _styles;
989
- constructor(name: string, cache: PivotCache, targetSheet: string, targetCell: string, targetRow: number, targetCol: number, pivotTableIndex: number, cacheFileIndex: number);
990
- /**
991
- * Get the pivot table name
992
- */
895
+ private _valueFields;
896
+ private _sortOrders;
897
+ private _filters;
898
+ constructor(workbook: Workbook, config: PivotTableConfig, sourceSheetName: string, sourceSheet: Worksheet, sourceRange: RangeAddress, targetSheetName: string, targetCell: {
899
+ row: number;
900
+ col: number;
901
+ }, cacheId: number, pivotId: number, cachePartIndex: number, fields: PivotFieldMeta[]);
993
902
  get name(): string;
994
- /**
995
- * Get the target sheet name
996
- */
997
- get targetSheet(): string;
998
- /**
999
- * Get the target cell address
1000
- */
1001
- get targetCell(): string;
1002
- /**
1003
- * Get the pivot cache
1004
- */
1005
- get cache(): PivotCache;
1006
- /**
1007
- * Get the pivot table index (for file naming)
1008
- */
1009
- get index(): number;
1010
- /**
1011
- * Get the pivot cache file index used for rels.
1012
- * @internal
1013
- */
1014
- get cacheFileIndex(): number;
1015
- /**
1016
- * Set the styles reference for number format resolution
1017
- * @internal
1018
- */
1019
- setStyles(styles: Styles): this;
1020
- /**
1021
- * Add a field to the row area
1022
- * @param fieldName - Name of the source field (column header)
1023
- */
903
+ get sourceSheetName(): string;
904
+ get sourceRange(): RangeAddress;
905
+ get targetSheetName(): string;
906
+ get targetCell(): {
907
+ row: number;
908
+ col: number;
909
+ };
910
+ get refreshOnLoad(): boolean;
911
+ get cacheId(): number;
912
+ get pivotId(): number;
913
+ get cachePartIndex(): number;
1024
914
  addRowField(fieldName: string): this;
1025
- /**
1026
- * Add a field to the column area
1027
- * @param fieldName - Name of the source field (column header)
1028
- */
1029
915
  addColumnField(fieldName: string): this;
1030
- /**
1031
- * Add a field to the values area with aggregation.
1032
- *
1033
- * Supports two call signatures:
1034
- * - Positional: `addValueField(fieldName, aggregation?, displayName?, numberFormat?)`
1035
- * - Object: `addValueField({ field, aggregation?, name?, numberFormat? })`
1036
- *
1037
- * @example
1038
- * // Positional arguments
1039
- * pivot.addValueField('Sales', 'sum', 'Total Sales', '$#,##0.00');
1040
- *
1041
- * // Object form
1042
- * pivot.addValueField({ field: 'Sales', aggregation: 'sum', name: 'Total Sales', numberFormat: '$#,##0.00' });
1043
- */
1044
- addValueField(config: PivotValueConfig): this;
1045
- addValueField(fieldName: string, aggregation?: AggregationType, displayName?: string, numberFormat?: string): this;
1046
- /**
1047
- * Add a field to the filter (page) area
1048
- * @param fieldName - Name of the source field (column header)
1049
- */
1050
916
  addFilterField(fieldName: string): this;
1051
- /**
1052
- * Set a sort order for a row or column field
1053
- * @param fieldName - Name of the field to sort
1054
- * @param order - Sort order ('asc' or 'desc')
1055
- */
917
+ addValueField(fieldName: string, aggregation?: PivotAggregationType, displayName?: string, numberFormat?: string): this;
918
+ addValueField(config: PivotValueConfig): this;
1056
919
  sortField(fieldName: string, order: PivotSortOrder): this;
1057
- /**
1058
- * Filter items for a row, column, or filter field
1059
- * @param fieldName - Name of the field to filter
1060
- * @param filter - Filter configuration with include or exclude list
1061
- */
1062
920
  filterField(fieldName: string, filter: PivotFieldFilter): this;
921
+ toPivotCacheDefinitionXml(): string;
922
+ toPivotCacheRecordsXml(): string;
923
+ toPivotCacheDefinitionRelsXml(): string;
1063
924
  /**
1064
- * Generate the pivotTableDefinition XML
1065
- */
1066
- toXml(): string;
1067
- /**
1068
- * Build a pivotField node for a given field index
1069
- */
1070
- private _buildPivotFieldNode;
1071
- /**
1072
- * Build item nodes for a pivot field, with optional filtering
1073
- */
1074
- private _buildItemNodes;
1075
- /**
1076
- * Build row items based on unique values in row fields
1077
- */
1078
- private _buildRowItems;
1079
- /**
1080
- * Build column items based on unique values in column fields
1081
- */
1082
- private _buildColItems;
1083
- /**
1084
- * Calculate the location reference for the pivot table output
1085
- */
1086
- private _calculateLocationRef;
1087
- /**
1088
- * Estimate number of rows in pivot table output
1089
- */
1090
- private _estimateRowCount;
1091
- /**
1092
- * Estimate number of columns in pivot table output
1093
- */
1094
- private _estimateColCount;
1095
- /**
1096
- * Convert 0-based column index to letter (A, B, ..., Z, AA, etc.)
925
+ * @internal
1097
926
  */
1098
- private _colToLetter;
927
+ buildPivotPartsXml(): {
928
+ cacheDefinitionXml: string;
929
+ cacheRecordsXml: string;
930
+ cacheRelsXml: string;
931
+ pivotTableXml: string;
932
+ };
933
+ toPivotTableDefinitionXml(): string;
934
+ private _buildPivotCacheDefinitionXml;
935
+ private _buildPivotCacheRecordsXml;
936
+ private _buildPivotTableDefinitionXml;
937
+ private _buildCacheFieldNode;
938
+ private _buildTargetAreaRef;
939
+ private _estimateOutputRows;
940
+ private _buildPivotCacheData;
941
+ private _buildSharedItemNode;
942
+ private _buildRawCacheValueNode;
943
+ private _assertFieldExists;
944
+ private _fieldIndex;
945
+ private _aggregationLabel;
946
+ private _distinctKey;
1099
947
  }
1100
948
 
1101
949
  /**
@@ -1108,23 +956,25 @@ declare class Workbook {
1108
956
  private _relationships;
1109
957
  private _sharedStrings;
1110
958
  private _styles;
959
+ private _sharedStringsXml;
960
+ private _stylesXml;
961
+ private _lazy;
1111
962
  private _dirty;
1112
- private _pivotTables;
1113
- private _pivotCaches;
1114
- private _nextCacheId;
1115
- private _nextCacheFileIndex;
1116
963
  private _nextTableId;
964
+ private _pivotTables;
965
+ private _nextPivotTableId;
966
+ private _nextPivotCacheId;
1117
967
  private _dateHandling;
1118
968
  private _locale;
1119
969
  private constructor();
1120
970
  /**
1121
971
  * Load a workbook from a file path
1122
972
  */
1123
- static fromFile(path: string): Promise<Workbook>;
973
+ static fromFile(path: string, options?: WorkbookReadOptions): Promise<Workbook>;
1124
974
  /**
1125
975
  * Load a workbook from a buffer
1126
976
  */
1127
- static fromBuffer(data: Uint8Array): Promise<Workbook>;
977
+ static fromBuffer(data: Uint8Array, options?: WorkbookReadOptions): Promise<Workbook>;
1128
978
  /**
1129
979
  * Create a new empty workbook
1130
980
  */
@@ -1167,6 +1017,16 @@ declare class Workbook {
1167
1017
  * @internal
1168
1018
  */
1169
1019
  getNextTableId(): number;
1020
+ /**
1021
+ * Get all pivot tables in the workbook.
1022
+ */
1023
+ get pivotTables(): PivotTable[];
1024
+ /**
1025
+ * Create a new pivot table.
1026
+ */
1027
+ createPivotTable(config: PivotTableConfig): PivotTable;
1028
+ private _extractPivotFields;
1029
+ private _normalizeRange;
1170
1030
  /**
1171
1031
  * Get a worksheet by name or index
1172
1032
  */
@@ -1247,35 +1107,6 @@ declare class Workbook {
1247
1107
  * Convert an unknown value to a CellValue
1248
1108
  */
1249
1109
  private _toCellValue;
1250
- /**
1251
- * Create a pivot table from source data.
1252
- *
1253
- * @param config - Pivot table configuration
1254
- * @returns PivotTable instance for fluent configuration
1255
- *
1256
- * @example
1257
- * ```typescript
1258
- * const pivot = wb.createPivotTable({
1259
- * name: 'SalesPivot',
1260
- * source: 'DataSheet!A1:D100',
1261
- * target: 'PivotSheet!A3',
1262
- * });
1263
- *
1264
- * pivot
1265
- * .addRowField('Region')
1266
- * .addColumnField('Product')
1267
- * .addValueField('Sales', 'sum', 'Total Sales');
1268
- * ```
1269
- */
1270
- createPivotTable(config: PivotTableConfig): PivotTable;
1271
- /**
1272
- * Parse a sheet reference like "Sheet1!A1:D100" into sheet name and range
1273
- */
1274
- private _parseSheetRef;
1275
- /**
1276
- * Extract headers and data from a source range
1277
- */
1278
- private _extractSourceData;
1279
1110
  /**
1280
1111
  * Save the workbook to a file
1281
1112
  */
@@ -1291,14 +1122,14 @@ declare class Workbook {
1291
1122
  private _updateRelationshipsXml;
1292
1123
  private _buildRelationshipInfo;
1293
1124
  private _updateContentTypes;
1294
- /**
1295
- * Generate all pivot table related files
1296
- */
1297
- private _updatePivotTableFiles;
1298
1125
  /**
1299
1126
  * Generate all table related files
1300
1127
  */
1301
1128
  private _updateTableFiles;
1129
+ /**
1130
+ * Generate pivot cache/table parts and worksheet relationships.
1131
+ */
1132
+ private _updatePivotFiles;
1302
1133
  }
1303
1134
 
1304
1135
  /**
@@ -1326,7 +1157,23 @@ declare const parseRange: (range: string) => RangeAddress;
1326
1157
  * @returns Range string like 'A1:B10'
1327
1158
  */
1328
1159
  declare const toRange: (range: RangeAddress) => string;
1160
+ /**
1161
+ * Parses a qualified sheet + address reference.
1162
+ * Supports Sheet!A1 and 'Sheet Name'!A1.
1163
+ */
1164
+ declare const parseSheetAddress: (reference: string) => {
1165
+ sheet: string;
1166
+ address: CellAddress;
1167
+ };
1168
+ /**
1169
+ * Parses a qualified sheet + range reference.
1170
+ * Supports Sheet!A1:B10 and 'Sheet Name'!A1:B10.
1171
+ */
1172
+ declare const parseSheetRange: (reference: string) => {
1173
+ sheet: string;
1174
+ range: RangeAddress;
1175
+ };
1329
1176
 
1330
- export { Cell, PivotCache, PivotTable, Range, SharedStrings, Styles, Table, Workbook, Worksheet, parseAddress, parseRange, toAddress, toRange };
1331
- 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 };
1177
+ export { Cell, PivotTable, Range, SharedStrings, Styles, Table, Workbook, Worksheet, parseAddress, parseRange, parseSheetAddress, parseSheetRange, toAddress, toRange };
1178
+ export type { Alignment, BorderStyle, BorderType, CellAddress, CellError, CellStyle, CellType, CellValue, ColumnConfig, DateHandling, ErrorType, PivotAggregationType, PivotFieldFilter, PivotSortOrder, PivotTableConfig, PivotValueConfig, RangeAddress, RichCellValue, SheetFromDataConfig, SheetToJsonConfig, TableConfig, TableStyleConfig, TableTotalFunction, WorkbookReadOptions };
1332
1179
  //# sourceMappingURL=index.d.ts.map