@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 CHANGED
@@ -1837,25 +1837,40 @@ const builder = new fastXmlParser.XMLBuilder(builderOptions);
1837
1837
  });
1838
1838
  return this;
1839
1839
  }
1840
- /**
1841
- * Add a field to the values area with aggregation
1842
- * @param fieldName - Name of the source field (column header)
1843
- * @param aggregation - Aggregation function (sum, count, average, min, max)
1844
- * @param displayName - Optional display name (defaults to "Sum of FieldName")
1845
- * @param numberFormat - Optional number format (e.g., '$#,##0.00', '0.00%')
1846
- */ addValueField(fieldName, aggregation = 'sum', displayName, numberFormat) {
1840
+ addValueField(fieldNameOrConfig, aggregation = 'sum', displayName, numberFormat) {
1841
+ // Normalize arguments to a common form
1842
+ let fieldName;
1843
+ let agg;
1844
+ let name;
1845
+ let format;
1846
+ if (typeof fieldNameOrConfig === 'object') {
1847
+ fieldName = fieldNameOrConfig.field;
1848
+ agg = fieldNameOrConfig.aggregation ?? 'sum';
1849
+ name = fieldNameOrConfig.name;
1850
+ format = fieldNameOrConfig.numberFormat;
1851
+ } else {
1852
+ fieldName = fieldNameOrConfig;
1853
+ agg = aggregation;
1854
+ name = displayName;
1855
+ format = numberFormat;
1856
+ }
1847
1857
  const fieldIndex = this._cache.getFieldIndex(fieldName);
1848
1858
  if (fieldIndex < 0) {
1849
1859
  throw new Error(`Field not found in source data: ${fieldName}`);
1850
1860
  }
1851
- const defaultName = `${aggregation.charAt(0).toUpperCase() + aggregation.slice(1)} of ${fieldName}`;
1861
+ const defaultName = `${agg.charAt(0).toUpperCase() + agg.slice(1)} of ${fieldName}`;
1862
+ // Resolve numFmtId immediately if format is provided and styles are available
1863
+ let numFmtId;
1864
+ if (format && this._styles) {
1865
+ numFmtId = this._styles.getOrCreateNumFmtId(format);
1866
+ }
1852
1867
  this._valueFields.push({
1853
1868
  fieldName,
1854
1869
  fieldIndex,
1855
1870
  axis: 'value',
1856
- aggregation,
1857
- displayName: displayName || defaultName,
1858
- numberFormat
1871
+ aggregation: agg,
1872
+ displayName: name || defaultName,
1873
+ numFmtId
1859
1874
  });
1860
1875
  return this;
1861
1876
  }
@@ -1986,9 +2001,9 @@ const builder = new fastXmlParser.XMLBuilder(builderOptions);
1986
2001
  baseItem: '0',
1987
2002
  subtotal: f.aggregation || 'sum'
1988
2003
  };
1989
- // Add numFmtId if format specified and styles available
1990
- if (f.numberFormat && this._styles) {
1991
- attrs.numFmtId = String(this._styles.getOrCreateNumFmtId(f.numberFormat));
2004
+ // Add numFmtId if it was resolved during addValueField
2005
+ if (f.numFmtId !== undefined) {
2006
+ attrs.numFmtId = String(f.numFmtId);
1992
2007
  }
1993
2008
  return createElement('dataField', attrs, []);
1994
2009
  });
@@ -1997,7 +2012,7 @@ const builder = new fastXmlParser.XMLBuilder(builderOptions);
1997
2012
  }, dataFieldNodes));
1998
2013
  }
1999
2014
  // Check if any value field has a number format
2000
- const hasNumberFormats = this._valueFields.some((f)=>f.numberFormat);
2015
+ const hasNumberFormats = this._valueFields.some((f)=>f.numFmtId !== undefined);
2001
2016
  // Pivot table style
2002
2017
  children.push(createElement('pivotTableStyleInfo', {
2003
2018
  name: 'PivotStyleMedium9',
package/dist/index.d.cts CHANGED
@@ -89,10 +89,12 @@ type AggregationType = 'sum' | 'count' | 'average' | 'min' | 'max';
89
89
  interface PivotValueConfig {
90
90
  /** Source field name (column header) */
91
91
  field: string;
92
- /** Aggregation function */
93
- aggregation: AggregationType;
92
+ /** Aggregation function (default: 'sum') */
93
+ aggregation?: AggregationType;
94
94
  /** Display name (e.g., "Sum of Sales") */
95
95
  name?: string;
96
+ /** Number format (e.g., '$#,##0.00', '0.00%') */
97
+ numberFormat?: string;
96
98
  }
97
99
  /**
98
100
  * Configuration for creating a pivot table
@@ -664,12 +666,20 @@ declare class PivotTable {
664
666
  */
665
667
  addColumnField(fieldName: string): this;
666
668
  /**
667
- * Add a field to the values area with aggregation
668
- * @param fieldName - Name of the source field (column header)
669
- * @param aggregation - Aggregation function (sum, count, average, min, max)
670
- * @param displayName - Optional display name (defaults to "Sum of FieldName")
671
- * @param numberFormat - Optional number format (e.g., '$#,##0.00', '0.00%')
669
+ * Add a field to the values area with aggregation.
670
+ *
671
+ * Supports two call signatures:
672
+ * - Positional: `addValueField(fieldName, aggregation?, displayName?, numberFormat?)`
673
+ * - Object: `addValueField({ field, aggregation?, name?, numberFormat? })`
674
+ *
675
+ * @example
676
+ * // Positional arguments
677
+ * pivot.addValueField('Sales', 'sum', 'Total Sales', '$#,##0.00');
678
+ *
679
+ * // Object form
680
+ * pivot.addValueField({ field: 'Sales', aggregation: 'sum', name: 'Total Sales', numberFormat: '$#,##0.00' });
672
681
  */
682
+ addValueField(config: PivotValueConfig): this;
673
683
  addValueField(fieldName: string, aggregation?: AggregationType, displayName?: string, numberFormat?: string): this;
674
684
  /**
675
685
  * Add a field to the filter (page) area