@niicojs/excel 0.2.0 → 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
@@ -2341,8 +2341,6 @@ const builder = new fastXmlParser.XMLBuilder(builderOptions);
2341
2341
  */ static create() {
2342
2342
  const workbook = new Workbook();
2343
2343
  workbook._dirty = true;
2344
- // Add default sheet
2345
- workbook.addSheet('Sheet1');
2346
2344
  return workbook;
2347
2345
  }
2348
2346
  /**
@@ -2527,13 +2525,20 @@ const builder = new fastXmlParser.XMLBuilder(builderOptions);
2527
2525
  * { key: 'age', header: 'Age (years)' },
2528
2526
  * ],
2529
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
+ * });
2530
2538
  * ```
2531
2539
  */ addSheetFromData(config) {
2532
2540
  const { name, data, columns, headerStyle = true, startCell = 'A1' } = config;
2533
- if (data.length === 0) {
2534
- // Create empty sheet if no data
2535
- return this.addSheet(name);
2536
- }
2541
+ if (!data?.length) return this.addSheet(name);
2537
2542
  // Create the new sheet
2538
2543
  const sheet = this.addSheet(name);
2539
2544
  // Parse start cell
@@ -2564,17 +2569,41 @@ const builder = new fastXmlParser.XMLBuilder(builderOptions);
2564
2569
  const colConfig = columnConfigs[colIdx];
2565
2570
  const value = rowData[colConfig.key];
2566
2571
  const cell = sheet.cell(startRow + rowIdx, startCol + colIdx);
2567
- // Convert value to CellValue
2568
- cell.value = this._toCellValue(value);
2569
- // 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)
2570
2583
  if (colConfig.style) {
2571
- cell.style = colConfig.style;
2584
+ cell.style = {
2585
+ ...cell.style,
2586
+ ...colConfig.style
2587
+ };
2572
2588
  }
2573
2589
  }
2574
2590
  }
2575
2591
  return sheet;
2576
2592
  }
2577
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
+ /**
2578
2607
  * Infer column configuration from the first data object
2579
2608
  */ _inferColumns(sample) {
2580
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