@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.js CHANGED
@@ -1835,25 +1835,40 @@ const builder = new XMLBuilder(builderOptions);
1835
1835
  });
1836
1836
  return this;
1837
1837
  }
1838
- /**
1839
- * Add a field to the values area with aggregation
1840
- * @param fieldName - Name of the source field (column header)
1841
- * @param aggregation - Aggregation function (sum, count, average, min, max)
1842
- * @param displayName - Optional display name (defaults to "Sum of FieldName")
1843
- * @param numberFormat - Optional number format (e.g., '$#,##0.00', '0.00%')
1844
- */ addValueField(fieldName, aggregation = 'sum', displayName, numberFormat) {
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 = `${aggregation.charAt(0).toUpperCase() + aggregation.slice(1)} of ${fieldName}`;
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: displayName || defaultName,
1856
- numberFormat
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 format specified and styles available
1988
- if (f.numberFormat && this._styles) {
1989
- attrs.numFmtId = String(this._styles.getOrCreateNumFmtId(f.numberFormat));
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.numberFormat);
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@niicojs/excel",
3
- "version": "0.2.6",
3
+ "version": "0.2.7",
4
4
  "description": "typescript library to manipulate excel files",
5
5
  "homepage": "https://github.com/niicojs/excel#readme",
6
6
  "bugs": {
@@ -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
- numberFormat?: string;
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
- * @param fieldName - Name of the source field (column header)
140
- * @param aggregation - Aggregation function (sum, count, average, min, max)
141
- * @param displayName - Optional display name (defaults to "Sum of FieldName")
142
- * @param numberFormat - Optional number format (e.g., '$#,##0.00', '0.00%')
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 = `${aggregation.charAt(0).toUpperCase() + aggregation.slice(1)} of ${fieldName}`;
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: displayName || defaultName,
163
- numberFormat,
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 format specified and styles available
283
- if (f.numberFormat && this._styles) {
284
- attrs.numFmtId = String(this._styles.getOrCreateNumFmtId(f.numberFormat));
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.numberFormat);
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: AggregationType;
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
  /**