@niicojs/excel 0.2.1 → 0.2.2

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
@@ -2525,13 +2525,20 @@ const builder = new fastXmlParser.XMLBuilder(builderOptions);
2525
2525
  * { key: 'age', header: 'Age (years)' },
2526
2526
  * ],
2527
2527
  * });
2528
+ *
2529
+ * // With rich cell values (value, formula, style)
2530
+ * const dataWithFormulas = [
2531
+ * { product: 'Widget', price: 10, qty: 5, total: { formula: 'B2*C2', style: { bold: true } } },
2532
+ * { product: 'Gadget', price: 20, qty: 3, total: { formula: 'B3*C3', style: { bold: true } } },
2533
+ * ];
2534
+ * const sheet3 = wb.addSheetFromData({
2535
+ * name: 'With Formulas',
2536
+ * data: dataWithFormulas,
2537
+ * });
2528
2538
  * ```
2529
2539
  */ addSheetFromData(config) {
2530
2540
  const { name, data, columns, headerStyle = true, startCell = 'A1' } = config;
2531
- if (data.length === 0) {
2532
- // Create empty sheet if no data
2533
- return this.addSheet(name);
2534
- }
2541
+ if (!data?.length) return this.addSheet(name);
2535
2542
  // Create the new sheet
2536
2543
  const sheet = this.addSheet(name);
2537
2544
  // Parse start cell
@@ -2562,17 +2569,41 @@ const builder = new fastXmlParser.XMLBuilder(builderOptions);
2562
2569
  const colConfig = columnConfigs[colIdx];
2563
2570
  const value = rowData[colConfig.key];
2564
2571
  const cell = sheet.cell(startRow + rowIdx, startCol + colIdx);
2565
- // Convert value to CellValue
2566
- cell.value = this._toCellValue(value);
2567
- // Apply column style if defined
2572
+ // Check if value is a rich cell definition
2573
+ if (this._isRichCellValue(value)) {
2574
+ const richValue = value;
2575
+ if (richValue.value !== undefined) cell.value = richValue.value;
2576
+ if (richValue.formula !== undefined) cell.formula = richValue.formula;
2577
+ if (richValue.style !== undefined) cell.style = richValue.style;
2578
+ } else {
2579
+ // Convert value to CellValue
2580
+ cell.value = this._toCellValue(value);
2581
+ }
2582
+ // Apply column style if defined (merged with cell style)
2568
2583
  if (colConfig.style) {
2569
- cell.style = colConfig.style;
2584
+ cell.style = {
2585
+ ...cell.style,
2586
+ ...colConfig.style
2587
+ };
2570
2588
  }
2571
2589
  }
2572
2590
  }
2573
2591
  return sheet;
2574
2592
  }
2575
2593
  /**
2594
+ * Check if a value is a rich cell value object with value, formula, or style fields
2595
+ */ _isRichCellValue(value) {
2596
+ if (value === null || value === undefined) {
2597
+ return false;
2598
+ }
2599
+ if (typeof value !== 'object' || value instanceof Date) {
2600
+ return false;
2601
+ }
2602
+ // Check if it has at least one of the rich cell properties
2603
+ const obj = value;
2604
+ return 'value' in obj || 'formula' in obj || 'style' in obj;
2605
+ }
2606
+ /**
2576
2607
  * Infer column configuration from the first data object
2577
2608
  */ _inferColumns(sample) {
2578
2609
  return Object.keys(sample).map((key)=>({
package/dist/index.d.cts CHANGED
@@ -156,6 +156,18 @@ interface ColumnConfig<T = Record<string, unknown>> {
156
156
  /** Cell style for data cells in this column */
157
157
  style?: CellStyle;
158
158
  }
159
+ /**
160
+ * Rich cell value with optional formula and style.
161
+ * Use this when you need to set value, formula, or style for individual cells.
162
+ */
163
+ interface RichCellValue {
164
+ /** Cell value */
165
+ value?: CellValue;
166
+ /** Formula (without leading '=') */
167
+ formula?: string;
168
+ /** Cell style */
169
+ style?: CellStyle;
170
+ }
159
171
 
160
172
  /**
161
173
  * Represents a single cell in a worksheet
@@ -723,9 +735,23 @@ declare class Workbook {
723
735
  * { key: 'age', header: 'Age (years)' },
724
736
  * ],
725
737
  * });
738
+ *
739
+ * // With rich cell values (value, formula, style)
740
+ * const dataWithFormulas = [
741
+ * { product: 'Widget', price: 10, qty: 5, total: { formula: 'B2*C2', style: { bold: true } } },
742
+ * { product: 'Gadget', price: 20, qty: 3, total: { formula: 'B3*C3', style: { bold: true } } },
743
+ * ];
744
+ * const sheet3 = wb.addSheetFromData({
745
+ * name: 'With Formulas',
746
+ * data: dataWithFormulas,
747
+ * });
726
748
  * ```
727
749
  */
728
750
  addSheetFromData<T extends object>(config: SheetFromDataConfig<T>): Worksheet;
751
+ /**
752
+ * Check if a value is a rich cell value object with value, formula, or style fields
753
+ */
754
+ private _isRichCellValue;
729
755
  /**
730
756
  * Infer column configuration from the first data object
731
757
  */
@@ -810,5 +836,5 @@ declare const parseRange: (range: string) => RangeAddress;
810
836
  declare const toRange: (range: RangeAddress) => string;
811
837
 
812
838
  export { Cell, PivotCache, PivotTable, Range, SharedStrings, Styles, Workbook, Worksheet, parseAddress, parseRange, toAddress, toRange };
813
- export type { AggregationType, Alignment, BorderStyle, BorderType, CellAddress, CellError, CellStyle, CellType, CellValue, ColumnConfig, ErrorType, PivotFieldAxis, PivotTableConfig, PivotValueConfig, RangeAddress, SheetFromDataConfig };
839
+ export type { AggregationType, Alignment, BorderStyle, BorderType, CellAddress, CellError, CellStyle, CellType, CellValue, ColumnConfig, ErrorType, PivotFieldAxis, PivotTableConfig, PivotValueConfig, RangeAddress, RichCellValue, SheetFromDataConfig };
814
840
  //# sourceMappingURL=index.d.cts.map