@niicojs/excel 0.2.6 → 0.2.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.cjs +30 -15
- package/dist/index.d.cts +17 -7
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.ts +17 -7
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -15
- package/package.json +1 -1
- package/src/pivot-table.ts +53 -15
- package/src/types.ts +4 -2
package/dist/index.js
CHANGED
|
@@ -1835,25 +1835,40 @@ const builder = new XMLBuilder(builderOptions);
|
|
|
1835
1835
|
});
|
|
1836
1836
|
return this;
|
|
1837
1837
|
}
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
|
|
1838
|
+
addValueField(fieldNameOrConfig, aggregation = 'sum', displayName, numberFormat) {
|
|
1839
|
+
// Normalize arguments to a common form
|
|
1840
|
+
let fieldName;
|
|
1841
|
+
let agg;
|
|
1842
|
+
let name;
|
|
1843
|
+
let format;
|
|
1844
|
+
if (typeof fieldNameOrConfig === 'object') {
|
|
1845
|
+
fieldName = fieldNameOrConfig.field;
|
|
1846
|
+
agg = fieldNameOrConfig.aggregation ?? 'sum';
|
|
1847
|
+
name = fieldNameOrConfig.name;
|
|
1848
|
+
format = fieldNameOrConfig.numberFormat;
|
|
1849
|
+
} else {
|
|
1850
|
+
fieldName = fieldNameOrConfig;
|
|
1851
|
+
agg = aggregation;
|
|
1852
|
+
name = displayName;
|
|
1853
|
+
format = numberFormat;
|
|
1854
|
+
}
|
|
1845
1855
|
const fieldIndex = this._cache.getFieldIndex(fieldName);
|
|
1846
1856
|
if (fieldIndex < 0) {
|
|
1847
1857
|
throw new Error(`Field not found in source data: ${fieldName}`);
|
|
1848
1858
|
}
|
|
1849
|
-
const defaultName = `${
|
|
1859
|
+
const defaultName = `${agg.charAt(0).toUpperCase() + agg.slice(1)} of ${fieldName}`;
|
|
1860
|
+
// Resolve numFmtId immediately if format is provided and styles are available
|
|
1861
|
+
let numFmtId;
|
|
1862
|
+
if (format && this._styles) {
|
|
1863
|
+
numFmtId = this._styles.getOrCreateNumFmtId(format);
|
|
1864
|
+
}
|
|
1850
1865
|
this._valueFields.push({
|
|
1851
1866
|
fieldName,
|
|
1852
1867
|
fieldIndex,
|
|
1853
1868
|
axis: 'value',
|
|
1854
|
-
aggregation,
|
|
1855
|
-
displayName:
|
|
1856
|
-
|
|
1869
|
+
aggregation: agg,
|
|
1870
|
+
displayName: name || defaultName,
|
|
1871
|
+
numFmtId
|
|
1857
1872
|
});
|
|
1858
1873
|
return this;
|
|
1859
1874
|
}
|
|
@@ -1984,9 +1999,9 @@ const builder = new XMLBuilder(builderOptions);
|
|
|
1984
1999
|
baseItem: '0',
|
|
1985
2000
|
subtotal: f.aggregation || 'sum'
|
|
1986
2001
|
};
|
|
1987
|
-
// Add numFmtId if
|
|
1988
|
-
if (f.
|
|
1989
|
-
attrs.numFmtId = String(
|
|
2002
|
+
// Add numFmtId if it was resolved during addValueField
|
|
2003
|
+
if (f.numFmtId !== undefined) {
|
|
2004
|
+
attrs.numFmtId = String(f.numFmtId);
|
|
1990
2005
|
}
|
|
1991
2006
|
return createElement('dataField', attrs, []);
|
|
1992
2007
|
});
|
|
@@ -1995,7 +2010,7 @@ const builder = new XMLBuilder(builderOptions);
|
|
|
1995
2010
|
}, dataFieldNodes));
|
|
1996
2011
|
}
|
|
1997
2012
|
// Check if any value field has a number format
|
|
1998
|
-
const hasNumberFormats = this._valueFields.some((f)=>f.
|
|
2013
|
+
const hasNumberFormats = this._valueFields.some((f)=>f.numFmtId !== undefined);
|
|
1999
2014
|
// Pivot table style
|
|
2000
2015
|
children.push(createElement('pivotTableStyleInfo', {
|
|
2001
2016
|
name: 'PivotStyleMedium9',
|
package/package.json
CHANGED
package/src/pivot-table.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AggregationType, PivotFieldAxis } from './types';
|
|
1
|
+
import type { AggregationType, PivotFieldAxis, PivotValueConfig } from './types';
|
|
2
2
|
import type { Styles } from './styles';
|
|
3
3
|
import { PivotCache } from './pivot-cache';
|
|
4
4
|
import { createElement, stringifyXml, XmlNode } from './utils/xml';
|
|
@@ -12,7 +12,7 @@ interface FieldAssignment {
|
|
|
12
12
|
axis: PivotFieldAxis;
|
|
13
13
|
aggregation?: AggregationType;
|
|
14
14
|
displayName?: string;
|
|
15
|
-
|
|
15
|
+
numFmtId?: number;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
/**
|
|
@@ -135,32 +135,70 @@ export class PivotTable {
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
/**
|
|
138
|
-
* Add a field to the values area with aggregation
|
|
139
|
-
*
|
|
140
|
-
*
|
|
141
|
-
*
|
|
142
|
-
*
|
|
138
|
+
* Add a field to the values area with aggregation.
|
|
139
|
+
*
|
|
140
|
+
* Supports two call signatures:
|
|
141
|
+
* - Positional: `addValueField(fieldName, aggregation?, displayName?, numberFormat?)`
|
|
142
|
+
* - Object: `addValueField({ field, aggregation?, name?, numberFormat? })`
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* // Positional arguments
|
|
146
|
+
* pivot.addValueField('Sales', 'sum', 'Total Sales', '$#,##0.00');
|
|
147
|
+
*
|
|
148
|
+
* // Object form
|
|
149
|
+
* pivot.addValueField({ field: 'Sales', aggregation: 'sum', name: 'Total Sales', numberFormat: '$#,##0.00' });
|
|
143
150
|
*/
|
|
151
|
+
addValueField(config: PivotValueConfig): this;
|
|
144
152
|
addValueField(
|
|
145
153
|
fieldName: string,
|
|
154
|
+
aggregation?: AggregationType,
|
|
155
|
+
displayName?: string,
|
|
156
|
+
numberFormat?: string,
|
|
157
|
+
): this;
|
|
158
|
+
addValueField(
|
|
159
|
+
fieldNameOrConfig: string | PivotValueConfig,
|
|
146
160
|
aggregation: AggregationType = 'sum',
|
|
147
161
|
displayName?: string,
|
|
148
162
|
numberFormat?: string,
|
|
149
163
|
): this {
|
|
164
|
+
// Normalize arguments to a common form
|
|
165
|
+
let fieldName: string;
|
|
166
|
+
let agg: AggregationType;
|
|
167
|
+
let name: string | undefined;
|
|
168
|
+
let format: string | undefined;
|
|
169
|
+
|
|
170
|
+
if (typeof fieldNameOrConfig === 'object') {
|
|
171
|
+
fieldName = fieldNameOrConfig.field;
|
|
172
|
+
agg = fieldNameOrConfig.aggregation ?? 'sum';
|
|
173
|
+
name = fieldNameOrConfig.name;
|
|
174
|
+
format = fieldNameOrConfig.numberFormat;
|
|
175
|
+
} else {
|
|
176
|
+
fieldName = fieldNameOrConfig;
|
|
177
|
+
agg = aggregation;
|
|
178
|
+
name = displayName;
|
|
179
|
+
format = numberFormat;
|
|
180
|
+
}
|
|
181
|
+
|
|
150
182
|
const fieldIndex = this._cache.getFieldIndex(fieldName);
|
|
151
183
|
if (fieldIndex < 0) {
|
|
152
184
|
throw new Error(`Field not found in source data: ${fieldName}`);
|
|
153
185
|
}
|
|
154
186
|
|
|
155
|
-
const defaultName = `${
|
|
187
|
+
const defaultName = `${agg.charAt(0).toUpperCase() + agg.slice(1)} of ${fieldName}`;
|
|
188
|
+
|
|
189
|
+
// Resolve numFmtId immediately if format is provided and styles are available
|
|
190
|
+
let numFmtId: number | undefined;
|
|
191
|
+
if (format && this._styles) {
|
|
192
|
+
numFmtId = this._styles.getOrCreateNumFmtId(format);
|
|
193
|
+
}
|
|
156
194
|
|
|
157
195
|
this._valueFields.push({
|
|
158
196
|
fieldName,
|
|
159
197
|
fieldIndex,
|
|
160
198
|
axis: 'value',
|
|
161
|
-
aggregation,
|
|
162
|
-
displayName:
|
|
163
|
-
|
|
199
|
+
aggregation: agg,
|
|
200
|
+
displayName: name || defaultName,
|
|
201
|
+
numFmtId,
|
|
164
202
|
});
|
|
165
203
|
|
|
166
204
|
return this;
|
|
@@ -279,9 +317,9 @@ export class PivotTable {
|
|
|
279
317
|
subtotal: f.aggregation || 'sum',
|
|
280
318
|
};
|
|
281
319
|
|
|
282
|
-
// Add numFmtId if
|
|
283
|
-
if (f.
|
|
284
|
-
attrs.numFmtId = String(
|
|
320
|
+
// Add numFmtId if it was resolved during addValueField
|
|
321
|
+
if (f.numFmtId !== undefined) {
|
|
322
|
+
attrs.numFmtId = String(f.numFmtId);
|
|
285
323
|
}
|
|
286
324
|
|
|
287
325
|
return createElement('dataField', attrs, []);
|
|
@@ -290,7 +328,7 @@ export class PivotTable {
|
|
|
290
328
|
}
|
|
291
329
|
|
|
292
330
|
// Check if any value field has a number format
|
|
293
|
-
const hasNumberFormats = this._valueFields.some((f) => f.
|
|
331
|
+
const hasNumberFormats = this._valueFields.some((f) => f.numFmtId !== undefined);
|
|
294
332
|
|
|
295
333
|
// Pivot table style
|
|
296
334
|
children.push(
|
package/src/types.ts
CHANGED
|
@@ -119,10 +119,12 @@ export type AggregationType = 'sum' | 'count' | 'average' | 'min' | 'max';
|
|
|
119
119
|
export interface PivotValueConfig {
|
|
120
120
|
/** Source field name (column header) */
|
|
121
121
|
field: string;
|
|
122
|
-
/** Aggregation function */
|
|
123
|
-
aggregation
|
|
122
|
+
/** Aggregation function (default: 'sum') */
|
|
123
|
+
aggregation?: AggregationType;
|
|
124
124
|
/** Display name (e.g., "Sum of Sales") */
|
|
125
125
|
name?: string;
|
|
126
|
+
/** Number format (e.g., '$#,##0.00', '0.00%') */
|
|
127
|
+
numberFormat?: string;
|
|
126
128
|
}
|
|
127
129
|
|
|
128
130
|
/**
|