@memberjunction/export-engine 0.0.1 → 2.133.0
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/base-exporter.d.ts +64 -0
- package/dist/base-exporter.d.ts.map +1 -0
- package/dist/base-exporter.js +158 -0
- package/dist/base-exporter.js.map +1 -0
- package/dist/csv-exporter.d.ts +27 -0
- package/dist/csv-exporter.d.ts.map +1 -0
- package/dist/csv-exporter.js +109 -0
- package/dist/csv-exporter.js.map +1 -0
- package/dist/excel-exporter.d.ts +125 -0
- package/dist/excel-exporter.d.ts.map +1 -0
- package/dist/excel-exporter.js +889 -0
- package/dist/excel-exporter.js.map +1 -0
- package/dist/export-engine.d.ts +100 -0
- package/dist/export-engine.d.ts.map +1 -0
- package/dist/export-engine.js +158 -0
- package/dist/export-engine.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +33 -0
- package/dist/index.js.map +1 -0
- package/dist/json-exporter.d.ts +18 -0
- package/dist/json-exporter.d.ts.map +1 -0
- package/dist/json-exporter.js +66 -0
- package/dist/json-exporter.js.map +1 -0
- package/dist/types.d.ts +612 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +97 -0
- package/dist/types.js.map +1 -0
- package/package.json +22 -7
- package/README.md +0 -45
|
@@ -0,0 +1,889 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.ExcelExporter = void 0;
|
|
27
|
+
const ExcelJS = __importStar(require("exceljs"));
|
|
28
|
+
const base_exporter_1 = require("./base-exporter");
|
|
29
|
+
/**
|
|
30
|
+
* Excel exporter using ExcelJS
|
|
31
|
+
* Supports multi-sheet workbooks, formatting, styling, formulas, and all sampling options
|
|
32
|
+
*/
|
|
33
|
+
class ExcelExporter extends base_exporter_1.BaseExporter {
|
|
34
|
+
constructor(options = {}) {
|
|
35
|
+
super({ ...options, format: 'excel' });
|
|
36
|
+
}
|
|
37
|
+
getMimeType() {
|
|
38
|
+
return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
|
|
39
|
+
}
|
|
40
|
+
getFileExtension() {
|
|
41
|
+
return 'xlsx';
|
|
42
|
+
}
|
|
43
|
+
async export(data) {
|
|
44
|
+
try {
|
|
45
|
+
// Check if we're doing multi-sheet export
|
|
46
|
+
if (this.options.sheets && this.options.sheets.length > 0) {
|
|
47
|
+
return this.exportMultiSheet();
|
|
48
|
+
}
|
|
49
|
+
// Single sheet export (backward compatible)
|
|
50
|
+
return this.exportSingleSheet(data);
|
|
51
|
+
}
|
|
52
|
+
catch (error) {
|
|
53
|
+
return {
|
|
54
|
+
success: false,
|
|
55
|
+
error: error instanceof Error ? error.message : String(error)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Export multiple sheets to a single workbook
|
|
61
|
+
*/
|
|
62
|
+
async exportMultiSheet() {
|
|
63
|
+
const workbook = this.createWorkbook();
|
|
64
|
+
const sheets = this.options.sheets;
|
|
65
|
+
let totalRows = 0;
|
|
66
|
+
let maxColumns = 0;
|
|
67
|
+
const sheetStats = [];
|
|
68
|
+
for (const sheetDef of sheets) {
|
|
69
|
+
const { rowCount, columnCount } = await this.addSheet(workbook, sheetDef);
|
|
70
|
+
totalRows += rowCount;
|
|
71
|
+
maxColumns = Math.max(maxColumns, columnCount);
|
|
72
|
+
sheetStats.push({ name: sheetDef.name, rowCount, columnCount });
|
|
73
|
+
}
|
|
74
|
+
// Generate buffer
|
|
75
|
+
const buffer = await workbook.xlsx.writeBuffer();
|
|
76
|
+
const data = new Uint8Array(buffer);
|
|
77
|
+
return {
|
|
78
|
+
success: true,
|
|
79
|
+
data,
|
|
80
|
+
mimeType: this.getMimeType(),
|
|
81
|
+
fileName: this.getFullFileName(),
|
|
82
|
+
rowCount: totalRows,
|
|
83
|
+
columnCount: maxColumns,
|
|
84
|
+
sheetCount: sheets.length,
|
|
85
|
+
sheetStats,
|
|
86
|
+
sizeBytes: data.length
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Export single sheet (original behavior)
|
|
91
|
+
*/
|
|
92
|
+
async exportSingleSheet(data) {
|
|
93
|
+
// Apply sampling
|
|
94
|
+
const sampledData = this.applySampling(data);
|
|
95
|
+
// Derive columns
|
|
96
|
+
const columns = this.deriveColumns(sampledData);
|
|
97
|
+
if (columns.length === 0) {
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
error: 'No columns to export'
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
// Create workbook
|
|
104
|
+
const workbook = this.createWorkbook();
|
|
105
|
+
// Create worksheet
|
|
106
|
+
const worksheet = workbook.addWorksheet(this.options.sheetName || 'Sheet1');
|
|
107
|
+
// Add headers
|
|
108
|
+
let rowIndex = 1;
|
|
109
|
+
if (this.options.includeHeaders !== false) {
|
|
110
|
+
this.addHeaderRow(worksheet, columns, this.options.styling?.headerStyle);
|
|
111
|
+
rowIndex++;
|
|
112
|
+
}
|
|
113
|
+
// Add data rows
|
|
114
|
+
for (const row of sampledData) {
|
|
115
|
+
const values = this.extractRowValues(row, columns);
|
|
116
|
+
const excelRow = worksheet.addRow(values);
|
|
117
|
+
// Apply data style
|
|
118
|
+
if (this.options.styling?.dataStyle) {
|
|
119
|
+
this.applyRowCellStyle(excelRow, this.options.styling.dataStyle);
|
|
120
|
+
}
|
|
121
|
+
// Apply alternating row colors
|
|
122
|
+
if (this.options.styling?.alternatingRowColors && rowIndex % 2 === 0) {
|
|
123
|
+
const altStyle = this.options.styling.alternateRowStyle ||
|
|
124
|
+
{ fill: { pattern: 'solid', fgColor: this.options.styling.alternateRowColor || 'F5F5F5' } };
|
|
125
|
+
this.applyRowCellStyle(excelRow, altStyle);
|
|
126
|
+
}
|
|
127
|
+
rowIndex++;
|
|
128
|
+
}
|
|
129
|
+
// Apply column widths
|
|
130
|
+
this.applyColumnWidths(worksheet, columns, sampledData);
|
|
131
|
+
// Apply auto-filter
|
|
132
|
+
if (this.options.styling?.autoFilter && this.options.includeHeaders !== false) {
|
|
133
|
+
worksheet.autoFilter = {
|
|
134
|
+
from: { row: 1, column: 1 },
|
|
135
|
+
to: { row: 1, column: columns.length }
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
// Freeze header row
|
|
139
|
+
if (this.options.styling?.freezeHeader && this.options.includeHeaders !== false) {
|
|
140
|
+
worksheet.views = [{ state: 'frozen', ySplit: 1 }];
|
|
141
|
+
}
|
|
142
|
+
// Generate buffer
|
|
143
|
+
const buffer = await workbook.xlsx.writeBuffer();
|
|
144
|
+
const resultData = new Uint8Array(buffer);
|
|
145
|
+
return {
|
|
146
|
+
success: true,
|
|
147
|
+
data: resultData,
|
|
148
|
+
mimeType: this.getMimeType(),
|
|
149
|
+
fileName: this.getFullFileName(),
|
|
150
|
+
rowCount: sampledData.length,
|
|
151
|
+
columnCount: columns.length,
|
|
152
|
+
sheetCount: 1,
|
|
153
|
+
sizeBytes: resultData.length
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Create and configure workbook with metadata
|
|
158
|
+
*/
|
|
159
|
+
createWorkbook() {
|
|
160
|
+
const workbook = new ExcelJS.Workbook();
|
|
161
|
+
// Set metadata from options
|
|
162
|
+
const meta = this.options.metadata || {};
|
|
163
|
+
workbook.creator = meta.author || this.options.author || 'MemberJunction';
|
|
164
|
+
workbook.created = new Date();
|
|
165
|
+
workbook.modified = new Date();
|
|
166
|
+
if (meta.title || this.options.title) {
|
|
167
|
+
workbook.title = meta.title || this.options.title;
|
|
168
|
+
}
|
|
169
|
+
if (meta.subject) {
|
|
170
|
+
workbook.subject = meta.subject;
|
|
171
|
+
}
|
|
172
|
+
if (meta.description) {
|
|
173
|
+
workbook.description = meta.description;
|
|
174
|
+
}
|
|
175
|
+
if (meta.company) {
|
|
176
|
+
workbook.company = meta.company;
|
|
177
|
+
}
|
|
178
|
+
if (meta.manager) {
|
|
179
|
+
workbook.manager = meta.manager;
|
|
180
|
+
}
|
|
181
|
+
if (meta.keywords) {
|
|
182
|
+
workbook.keywords = Array.isArray(meta.keywords) ? meta.keywords.join(', ') : meta.keywords;
|
|
183
|
+
}
|
|
184
|
+
if (meta.category) {
|
|
185
|
+
workbook.category = meta.category;
|
|
186
|
+
}
|
|
187
|
+
// Calculate on save option
|
|
188
|
+
if (this.options.calcOnSave !== undefined) {
|
|
189
|
+
workbook.calcProperties = { fullCalcOnLoad: this.options.calcOnSave };
|
|
190
|
+
}
|
|
191
|
+
return workbook;
|
|
192
|
+
}
|
|
193
|
+
/**
|
|
194
|
+
* Add a sheet to the workbook based on SheetDefinition
|
|
195
|
+
*/
|
|
196
|
+
async addSheet(workbook, sheetDef) {
|
|
197
|
+
const worksheet = workbook.addWorksheet(sheetDef.name, {
|
|
198
|
+
properties: {
|
|
199
|
+
tabColor: sheetDef.tabColor ? { argb: `FF${sheetDef.tabColor}` } : undefined,
|
|
200
|
+
defaultRowHeight: sheetDef.defaultRowHeight,
|
|
201
|
+
defaultColWidth: sheetDef.defaultColWidth,
|
|
202
|
+
showGridLines: sheetDef.showGridLines
|
|
203
|
+
},
|
|
204
|
+
views: sheetDef.rightToLeft ? [{ rightToLeft: true }] : undefined
|
|
205
|
+
});
|
|
206
|
+
// Derive columns from data or use provided columns/headers
|
|
207
|
+
let columns;
|
|
208
|
+
if (sheetDef.columns && sheetDef.columns.length > 0) {
|
|
209
|
+
columns = sheetDef.columns;
|
|
210
|
+
}
|
|
211
|
+
else if (sheetDef.headers && sheetDef.headers.length > 0) {
|
|
212
|
+
columns = sheetDef.headers.map((header, index) => ({
|
|
213
|
+
name: header,
|
|
214
|
+
displayName: header,
|
|
215
|
+
width: sheetDef.columnWidths?.[index]
|
|
216
|
+
}));
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
columns = this.deriveColumnsFromData(sheetDef.data);
|
|
220
|
+
}
|
|
221
|
+
// Apply column widths if specified separately
|
|
222
|
+
if (sheetDef.columnWidths && !sheetDef.columns) {
|
|
223
|
+
columns = columns.map((col, index) => ({
|
|
224
|
+
...col,
|
|
225
|
+
width: sheetDef.columnWidths?.[index] || col.width
|
|
226
|
+
}));
|
|
227
|
+
}
|
|
228
|
+
let rowIndex = 1;
|
|
229
|
+
const includeHeaders = sheetDef.includeHeaders !== false;
|
|
230
|
+
// Add headers
|
|
231
|
+
if (includeHeaders) {
|
|
232
|
+
const headerStyle = sheetDef.headerStyle || this.options.styling?.headerStyle;
|
|
233
|
+
this.addHeaderRow(worksheet, columns, headerStyle);
|
|
234
|
+
rowIndex++;
|
|
235
|
+
}
|
|
236
|
+
// Add data rows
|
|
237
|
+
for (let i = 0; i < sheetDef.data.length; i++) {
|
|
238
|
+
const row = sheetDef.data[i];
|
|
239
|
+
const values = this.extractRowValuesForSheet(row, columns);
|
|
240
|
+
const excelRow = worksheet.addRow(values);
|
|
241
|
+
// Apply data style
|
|
242
|
+
if (sheetDef.dataStyle) {
|
|
243
|
+
this.applyRowCellStyle(excelRow, sheetDef.dataStyle);
|
|
244
|
+
}
|
|
245
|
+
// Apply alternating row style
|
|
246
|
+
if (sheetDef.alternateRowStyle && rowIndex % 2 === 0) {
|
|
247
|
+
this.applyRowCellStyle(excelRow, sheetDef.alternateRowStyle);
|
|
248
|
+
}
|
|
249
|
+
rowIndex++;
|
|
250
|
+
}
|
|
251
|
+
// Apply specific row styles
|
|
252
|
+
if (sheetDef.rowStyles) {
|
|
253
|
+
this.applyRowStyles(worksheet, sheetDef.rowStyles);
|
|
254
|
+
}
|
|
255
|
+
// Apply specific cell style overrides
|
|
256
|
+
if (sheetDef.cellStyles) {
|
|
257
|
+
this.applyCellStyleOverrides(worksheet, sheetDef.cellStyles);
|
|
258
|
+
}
|
|
259
|
+
// Add formulas
|
|
260
|
+
if (sheetDef.formulas) {
|
|
261
|
+
this.addFormulas(worksheet, sheetDef.formulas);
|
|
262
|
+
}
|
|
263
|
+
// Apply merged cells
|
|
264
|
+
if (sheetDef.mergedCells) {
|
|
265
|
+
this.applyMergedCells(worksheet, sheetDef.mergedCells);
|
|
266
|
+
}
|
|
267
|
+
// Apply data validation
|
|
268
|
+
if (sheetDef.dataValidation) {
|
|
269
|
+
this.applyDataValidation(worksheet, sheetDef.dataValidation);
|
|
270
|
+
}
|
|
271
|
+
// Apply conditional formatting
|
|
272
|
+
if (sheetDef.conditionalFormatting) {
|
|
273
|
+
this.applyConditionalFormatting(worksheet, sheetDef.conditionalFormatting);
|
|
274
|
+
}
|
|
275
|
+
// Add images
|
|
276
|
+
if (sheetDef.images) {
|
|
277
|
+
await this.addImages(workbook, worksheet, sheetDef.images);
|
|
278
|
+
}
|
|
279
|
+
// Apply freeze panes
|
|
280
|
+
if (sheetDef.freeze) {
|
|
281
|
+
worksheet.views = [{
|
|
282
|
+
state: 'frozen',
|
|
283
|
+
xSplit: sheetDef.freeze.column ? sheetDef.freeze.column - 1 : 0,
|
|
284
|
+
ySplit: sheetDef.freeze.row ? sheetDef.freeze.row - 1 : 0
|
|
285
|
+
}];
|
|
286
|
+
}
|
|
287
|
+
// Apply auto-filter
|
|
288
|
+
if (sheetDef.autoFilter) {
|
|
289
|
+
if (typeof sheetDef.autoFilter === 'string') {
|
|
290
|
+
worksheet.autoFilter = sheetDef.autoFilter;
|
|
291
|
+
}
|
|
292
|
+
else if (sheetDef.autoFilter === true && includeHeaders) {
|
|
293
|
+
worksheet.autoFilter = {
|
|
294
|
+
from: { row: 1, column: 1 },
|
|
295
|
+
to: { row: 1, column: columns.length }
|
|
296
|
+
};
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
// Apply page setup
|
|
300
|
+
if (sheetDef.pageSetup) {
|
|
301
|
+
this.applyPageSetup(worksheet, sheetDef.pageSetup);
|
|
302
|
+
}
|
|
303
|
+
// Apply protection
|
|
304
|
+
if (sheetDef.protection) {
|
|
305
|
+
await this.applyProtection(worksheet, sheetDef.protection);
|
|
306
|
+
}
|
|
307
|
+
// Apply outline properties
|
|
308
|
+
if (sheetDef.outlineProperties) {
|
|
309
|
+
worksheet.properties.outlineProperties = {
|
|
310
|
+
summaryBelow: sheetDef.outlineProperties.summaryBelow ?? true,
|
|
311
|
+
summaryRight: sheetDef.outlineProperties.summaryRight ?? true
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
// Auto-fit columns if no widths specified
|
|
315
|
+
this.autoFitColumns(worksheet, columns, sheetDef.data);
|
|
316
|
+
return {
|
|
317
|
+
rowCount: sheetDef.data.length,
|
|
318
|
+
columnCount: columns.length
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Add styled header row
|
|
323
|
+
*/
|
|
324
|
+
addHeaderRow(worksheet, columns, headerStyle) {
|
|
325
|
+
const headerValues = columns.map(col => col.displayName || col.name);
|
|
326
|
+
const headerRow = worksheet.addRow(headerValues);
|
|
327
|
+
// Apply header styling
|
|
328
|
+
const styling = this.options.styling;
|
|
329
|
+
// Build combined style
|
|
330
|
+
let style = {};
|
|
331
|
+
if (styling?.boldHeaders !== false) {
|
|
332
|
+
style.font = { bold: true };
|
|
333
|
+
}
|
|
334
|
+
if (styling?.headerBackgroundColor) {
|
|
335
|
+
style.fill = {
|
|
336
|
+
pattern: 'solid',
|
|
337
|
+
fgColor: styling.headerBackgroundColor
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
if (styling?.headerTextColor) {
|
|
341
|
+
style.font = {
|
|
342
|
+
...style.font,
|
|
343
|
+
color: styling.headerTextColor
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
// Override with full header style if provided
|
|
347
|
+
if (headerStyle) {
|
|
348
|
+
style = { ...style, ...headerStyle };
|
|
349
|
+
}
|
|
350
|
+
// Apply the combined style
|
|
351
|
+
this.applyRowCellStyle(headerRow, style);
|
|
352
|
+
// Add bottom border to header
|
|
353
|
+
headerRow.eachCell(cell => {
|
|
354
|
+
cell.border = {
|
|
355
|
+
...cell.border,
|
|
356
|
+
bottom: { style: 'thin', color: { argb: 'FF000000' } }
|
|
357
|
+
};
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
/**
|
|
361
|
+
* Apply CellStyle to an ExcelJS cell
|
|
362
|
+
*/
|
|
363
|
+
applyCellStyle(cell, style) {
|
|
364
|
+
if (style.font) {
|
|
365
|
+
cell.font = this.convertFontStyle(style.font);
|
|
366
|
+
}
|
|
367
|
+
if (style.fill) {
|
|
368
|
+
cell.fill = this.convertFillStyle(style.fill);
|
|
369
|
+
}
|
|
370
|
+
if (style.border) {
|
|
371
|
+
cell.border = this.convertBorderStyle(style.border);
|
|
372
|
+
}
|
|
373
|
+
if (style.alignment) {
|
|
374
|
+
cell.alignment = this.convertAlignmentStyle(style.alignment);
|
|
375
|
+
}
|
|
376
|
+
if (style.numFmt) {
|
|
377
|
+
cell.numFmt = style.numFmt;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
/**
|
|
381
|
+
* Apply CellStyle to all cells in a row
|
|
382
|
+
*/
|
|
383
|
+
applyRowCellStyle(row, style) {
|
|
384
|
+
row.eachCell({ includeEmpty: false }, cell => {
|
|
385
|
+
this.applyCellStyle(cell, style);
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
/**
|
|
389
|
+
* Convert our FontStyle to ExcelJS font
|
|
390
|
+
*/
|
|
391
|
+
convertFontStyle(font) {
|
|
392
|
+
const result = {};
|
|
393
|
+
if (font.name)
|
|
394
|
+
result.name = font.name;
|
|
395
|
+
if (font.size)
|
|
396
|
+
result.size = font.size;
|
|
397
|
+
if (font.bold !== undefined)
|
|
398
|
+
result.bold = font.bold;
|
|
399
|
+
if (font.italic !== undefined)
|
|
400
|
+
result.italic = font.italic;
|
|
401
|
+
if (font.strike !== undefined)
|
|
402
|
+
result.strike = font.strike;
|
|
403
|
+
if (font.color)
|
|
404
|
+
result.color = { argb: `FF${font.color}` };
|
|
405
|
+
if (font.underline !== undefined) {
|
|
406
|
+
if (typeof font.underline === 'boolean') {
|
|
407
|
+
result.underline = font.underline;
|
|
408
|
+
}
|
|
409
|
+
else {
|
|
410
|
+
result.underline = font.underline;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
return result;
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Convert our FillStyle to ExcelJS fill
|
|
417
|
+
*/
|
|
418
|
+
convertFillStyle(fill) {
|
|
419
|
+
return {
|
|
420
|
+
type: 'pattern',
|
|
421
|
+
pattern: fill.pattern || 'solid',
|
|
422
|
+
fgColor: fill.fgColor ? { argb: `FF${fill.fgColor}` } : undefined,
|
|
423
|
+
bgColor: fill.bgColor ? { argb: `FF${fill.bgColor}` } : undefined
|
|
424
|
+
};
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Convert our CellBorder to ExcelJS border
|
|
428
|
+
*/
|
|
429
|
+
convertBorderStyle(border) {
|
|
430
|
+
const result = {};
|
|
431
|
+
if (border.top)
|
|
432
|
+
result.top = this.convertBorderSide(border.top);
|
|
433
|
+
if (border.bottom)
|
|
434
|
+
result.bottom = this.convertBorderSide(border.bottom);
|
|
435
|
+
if (border.left)
|
|
436
|
+
result.left = this.convertBorderSide(border.left);
|
|
437
|
+
if (border.right)
|
|
438
|
+
result.right = this.convertBorderSide(border.right);
|
|
439
|
+
if (border.diagonal) {
|
|
440
|
+
result.diagonal = {
|
|
441
|
+
...this.convertBorderSide(border.diagonal),
|
|
442
|
+
up: border.diagonal.up,
|
|
443
|
+
down: border.diagonal.down
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
return result;
|
|
447
|
+
}
|
|
448
|
+
/**
|
|
449
|
+
* Convert BorderSide to ExcelJS border
|
|
450
|
+
*/
|
|
451
|
+
convertBorderSide(side) {
|
|
452
|
+
return {
|
|
453
|
+
style: side.style,
|
|
454
|
+
color: side.color ? { argb: `FF${side.color}` } : undefined
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* Convert our AlignmentStyle to ExcelJS alignment
|
|
459
|
+
*/
|
|
460
|
+
convertAlignmentStyle(alignment) {
|
|
461
|
+
return {
|
|
462
|
+
horizontal: alignment.horizontal,
|
|
463
|
+
vertical: alignment.vertical,
|
|
464
|
+
wrapText: alignment.wrapText,
|
|
465
|
+
shrinkToFit: alignment.shrinkToFit,
|
|
466
|
+
textRotation: alignment.textRotation,
|
|
467
|
+
indent: alignment.indent
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Apply row styles
|
|
472
|
+
*/
|
|
473
|
+
applyRowStyles(worksheet, rowStyles) {
|
|
474
|
+
for (const rs of rowStyles) {
|
|
475
|
+
const row = worksheet.getRow(rs.row);
|
|
476
|
+
if (rs.height)
|
|
477
|
+
row.height = rs.height;
|
|
478
|
+
if (rs.hidden)
|
|
479
|
+
row.hidden = true;
|
|
480
|
+
if (rs.outlineLevel !== undefined)
|
|
481
|
+
row.outlineLevel = rs.outlineLevel;
|
|
482
|
+
if (rs.style) {
|
|
483
|
+
this.applyRowCellStyle(row, rs.style);
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
/**
|
|
488
|
+
* Apply cell style overrides
|
|
489
|
+
*/
|
|
490
|
+
applyCellStyleOverrides(worksheet, overrides) {
|
|
491
|
+
for (const override of overrides) {
|
|
492
|
+
// Handle both single cell and range
|
|
493
|
+
if (override.cell.includes(':')) {
|
|
494
|
+
// Range - apply to all cells in range
|
|
495
|
+
const [start, end] = override.cell.split(':');
|
|
496
|
+
const startCell = worksheet.getCell(start);
|
|
497
|
+
const endCell = worksheet.getCell(end);
|
|
498
|
+
// Get numeric indices
|
|
499
|
+
const startRow = this.getRowNumber(startCell);
|
|
500
|
+
const endRow = this.getRowNumber(endCell);
|
|
501
|
+
const startCol = this.getColumnNumber(startCell);
|
|
502
|
+
const endCol = this.getColumnNumber(endCell);
|
|
503
|
+
for (let row = startRow; row <= endRow; row++) {
|
|
504
|
+
for (let col = startCol; col <= endCol; col++) {
|
|
505
|
+
const cell = worksheet.getCell(row, col);
|
|
506
|
+
this.applyCellStyle(cell, override.style);
|
|
507
|
+
if (override.value !== undefined) {
|
|
508
|
+
cell.value = override.value;
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
else {
|
|
514
|
+
// Single cell
|
|
515
|
+
const cell = worksheet.getCell(override.cell);
|
|
516
|
+
this.applyCellStyle(cell, override.style);
|
|
517
|
+
if (override.value !== undefined) {
|
|
518
|
+
cell.value = override.value;
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Get numeric column index from cell
|
|
525
|
+
*/
|
|
526
|
+
getColumnNumber(cell) {
|
|
527
|
+
// ExcelJS col can be string (letter) or have fullAddress
|
|
528
|
+
if (typeof cell.col === 'number') {
|
|
529
|
+
return cell.col;
|
|
530
|
+
}
|
|
531
|
+
// Parse from address
|
|
532
|
+
const match = cell.address.match(/^([A-Z]+)/);
|
|
533
|
+
if (match) {
|
|
534
|
+
return this.columnLetterToNumber(match[1]);
|
|
535
|
+
}
|
|
536
|
+
return 1;
|
|
537
|
+
}
|
|
538
|
+
/**
|
|
539
|
+
* Get numeric row index from cell
|
|
540
|
+
*/
|
|
541
|
+
getRowNumber(cell) {
|
|
542
|
+
if (typeof cell.row === 'number') {
|
|
543
|
+
return cell.row;
|
|
544
|
+
}
|
|
545
|
+
// Parse from address
|
|
546
|
+
const match = cell.address.match(/(\d+)$/);
|
|
547
|
+
if (match) {
|
|
548
|
+
return parseInt(match[1], 10);
|
|
549
|
+
}
|
|
550
|
+
return 1;
|
|
551
|
+
}
|
|
552
|
+
/**
|
|
553
|
+
* Add formulas to worksheet
|
|
554
|
+
*/
|
|
555
|
+
addFormulas(worksheet, formulas) {
|
|
556
|
+
for (const formula of formulas) {
|
|
557
|
+
const cell = worksheet.getCell(formula.cell);
|
|
558
|
+
// Cast to the formula value type expected by ExcelJS
|
|
559
|
+
const formulaValue = {
|
|
560
|
+
formula: formula.formula
|
|
561
|
+
};
|
|
562
|
+
if (formula.result !== undefined) {
|
|
563
|
+
formulaValue.result = formula.result;
|
|
564
|
+
}
|
|
565
|
+
cell.value = formulaValue;
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* Apply merged cells
|
|
570
|
+
*/
|
|
571
|
+
applyMergedCells(worksheet, mergedCells) {
|
|
572
|
+
for (const range of mergedCells) {
|
|
573
|
+
worksheet.mergeCells(range.startRow, range.startColumn, range.endRow, range.endColumn);
|
|
574
|
+
}
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Apply data validation rules
|
|
578
|
+
*/
|
|
579
|
+
applyDataValidation(worksheet, rules) {
|
|
580
|
+
for (const rule of rules) {
|
|
581
|
+
// Build formulae array first
|
|
582
|
+
let formulae = [];
|
|
583
|
+
if (rule.type === 'list' && rule.allowedValues) {
|
|
584
|
+
formulae = [rule.allowedValues.join(',')];
|
|
585
|
+
}
|
|
586
|
+
else if (rule.formula1 !== undefined) {
|
|
587
|
+
formulae = [String(rule.formula1)];
|
|
588
|
+
if (rule.formula2 !== undefined) {
|
|
589
|
+
formulae.push(String(rule.formula2));
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
const validation = {
|
|
593
|
+
type: rule.type,
|
|
594
|
+
formulae,
|
|
595
|
+
allowBlank: rule.allowBlank,
|
|
596
|
+
showErrorMessage: rule.showError,
|
|
597
|
+
errorTitle: rule.errorTitle,
|
|
598
|
+
error: rule.errorMessage,
|
|
599
|
+
errorStyle: rule.errorStyle,
|
|
600
|
+
showInputMessage: rule.showInputMessage,
|
|
601
|
+
promptTitle: rule.inputTitle,
|
|
602
|
+
prompt: rule.inputMessage
|
|
603
|
+
};
|
|
604
|
+
if (rule.operator) {
|
|
605
|
+
validation.operator = rule.operator;
|
|
606
|
+
}
|
|
607
|
+
// Apply to range
|
|
608
|
+
const [start, end] = rule.range.includes(':')
|
|
609
|
+
? rule.range.split(':')
|
|
610
|
+
: [rule.range, rule.range];
|
|
611
|
+
const startCell = worksheet.getCell(start);
|
|
612
|
+
const endCell = worksheet.getCell(end);
|
|
613
|
+
const startRow = this.getRowNumber(startCell);
|
|
614
|
+
const endRow = this.getRowNumber(endCell);
|
|
615
|
+
const startCol = this.getColumnNumber(startCell);
|
|
616
|
+
const endCol = this.getColumnNumber(endCell);
|
|
617
|
+
for (let row = startRow; row <= endRow; row++) {
|
|
618
|
+
for (let col = startCol; col <= endCol; col++) {
|
|
619
|
+
worksheet.getCell(row, col).dataValidation = validation;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
}
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Apply conditional formatting rules
|
|
626
|
+
*/
|
|
627
|
+
applyConditionalFormatting(worksheet, rules) {
|
|
628
|
+
for (const rule of rules) {
|
|
629
|
+
// Map our operator names to ExcelJS operator names
|
|
630
|
+
const operatorMap = {
|
|
631
|
+
'equal': 'equal',
|
|
632
|
+
'notEqual': 'notEqual',
|
|
633
|
+
'greaterThan': 'greaterThan',
|
|
634
|
+
'lessThan': 'lessThan',
|
|
635
|
+
'greaterThanOrEqual': 'greaterThanOrEqual',
|
|
636
|
+
'lessThanOrEqual': 'lessThanOrEqual',
|
|
637
|
+
'between': 'between',
|
|
638
|
+
'notBetween': 'notBetween'
|
|
639
|
+
};
|
|
640
|
+
// Build formulae if values provided
|
|
641
|
+
const formulae = [];
|
|
642
|
+
if (rule.value !== undefined) {
|
|
643
|
+
formulae.push(String(rule.value));
|
|
644
|
+
if (rule.value2 !== undefined) {
|
|
645
|
+
formulae.push(String(rule.value2));
|
|
646
|
+
}
|
|
647
|
+
}
|
|
648
|
+
// Build the conditional formatting rule
|
|
649
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
650
|
+
const cfRule = {
|
|
651
|
+
type: rule.type,
|
|
652
|
+
priority: rule.priority || 1
|
|
653
|
+
};
|
|
654
|
+
if (rule.operator && operatorMap[rule.operator]) {
|
|
655
|
+
cfRule.operator = operatorMap[rule.operator];
|
|
656
|
+
}
|
|
657
|
+
if (formulae.length > 0) {
|
|
658
|
+
cfRule.formulae = formulae;
|
|
659
|
+
}
|
|
660
|
+
if (rule.style) {
|
|
661
|
+
cfRule.style = {
|
|
662
|
+
font: rule.style.font ? this.convertFontStyle(rule.style.font) : undefined,
|
|
663
|
+
fill: rule.style.fill ? this.convertFillStyle(rule.style.fill) : undefined
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
worksheet.addConditionalFormatting({
|
|
667
|
+
ref: rule.range,
|
|
668
|
+
rules: [cfRule]
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
/**
|
|
673
|
+
* Add images to worksheet
|
|
674
|
+
*/
|
|
675
|
+
async addImages(workbook, worksheet, images) {
|
|
676
|
+
for (const img of images) {
|
|
677
|
+
const imageData = typeof img.data === 'string'
|
|
678
|
+
? Buffer.from(img.data, 'base64')
|
|
679
|
+
: img.data;
|
|
680
|
+
const imageId = workbook.addImage({
|
|
681
|
+
buffer: imageData,
|
|
682
|
+
extension: img.type
|
|
683
|
+
});
|
|
684
|
+
const col = typeof img.position.col === 'string'
|
|
685
|
+
? this.columnLetterToNumber(img.position.col)
|
|
686
|
+
: img.position.col;
|
|
687
|
+
worksheet.addImage(imageId, {
|
|
688
|
+
tl: {
|
|
689
|
+
col: col - 1 + (img.position.colOffset ? img.position.colOffset / 64 : 0),
|
|
690
|
+
row: img.position.row - 1 + (img.position.rowOffset ? img.position.rowOffset / 20 : 0)
|
|
691
|
+
},
|
|
692
|
+
ext: img.size ? { width: img.size.width || 100, height: img.size.height || 100 } : { width: 100, height: 100 }
|
|
693
|
+
});
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
/**
|
|
697
|
+
* Apply page setup options
|
|
698
|
+
*/
|
|
699
|
+
applyPageSetup(worksheet, setup) {
|
|
700
|
+
if (!setup)
|
|
701
|
+
return;
|
|
702
|
+
const pageSetup = {};
|
|
703
|
+
if (setup.paperSize) {
|
|
704
|
+
const paperSizes = {
|
|
705
|
+
letter: 1, legal: 5, tabloid: 3, a3: 8, a4: 9, a5: 11
|
|
706
|
+
};
|
|
707
|
+
pageSetup.paperSize = paperSizes[setup.paperSize];
|
|
708
|
+
}
|
|
709
|
+
if (setup.orientation) {
|
|
710
|
+
pageSetup.orientation = setup.orientation;
|
|
711
|
+
}
|
|
712
|
+
if (setup.fitToWidth !== undefined) {
|
|
713
|
+
pageSetup.fitToPage = true;
|
|
714
|
+
pageSetup.fitToWidth = setup.fitToWidth;
|
|
715
|
+
}
|
|
716
|
+
if (setup.fitToHeight !== undefined) {
|
|
717
|
+
pageSetup.fitToPage = true;
|
|
718
|
+
pageSetup.fitToHeight = setup.fitToHeight;
|
|
719
|
+
}
|
|
720
|
+
if (setup.scale) {
|
|
721
|
+
pageSetup.scale = setup.scale;
|
|
722
|
+
}
|
|
723
|
+
if (setup.showGridLines !== undefined) {
|
|
724
|
+
pageSetup.showGridLines = setup.showGridLines;
|
|
725
|
+
}
|
|
726
|
+
if (setup.showRowColHeaders !== undefined) {
|
|
727
|
+
pageSetup.showRowColHeaders = setup.showRowColHeaders;
|
|
728
|
+
}
|
|
729
|
+
if (setup.margins) {
|
|
730
|
+
pageSetup.margins = {
|
|
731
|
+
left: setup.margins.left || 0.7,
|
|
732
|
+
right: setup.margins.right || 0.7,
|
|
733
|
+
top: setup.margins.top || 0.75,
|
|
734
|
+
bottom: setup.margins.bottom || 0.75,
|
|
735
|
+
header: setup.margins.header || 0.3,
|
|
736
|
+
footer: setup.margins.footer || 0.3
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
if (setup.printTitlesRow) {
|
|
740
|
+
pageSetup.printTitlesRow = setup.printTitlesRow;
|
|
741
|
+
}
|
|
742
|
+
if (setup.printTitlesColumn) {
|
|
743
|
+
pageSetup.printTitlesColumn = setup.printTitlesColumn;
|
|
744
|
+
}
|
|
745
|
+
worksheet.pageSetup = pageSetup;
|
|
746
|
+
// Header and footer
|
|
747
|
+
if (setup.header || setup.footer) {
|
|
748
|
+
worksheet.headerFooter = {
|
|
749
|
+
oddHeader: setup.header,
|
|
750
|
+
oddFooter: setup.footer
|
|
751
|
+
};
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
/**
|
|
755
|
+
* Apply sheet protection
|
|
756
|
+
*/
|
|
757
|
+
async applyProtection(worksheet, protection) {
|
|
758
|
+
const options = {
|
|
759
|
+
selectLockedCells: protection.selectLockedCells ?? true,
|
|
760
|
+
selectUnlockedCells: protection.selectUnlockedCells ?? true,
|
|
761
|
+
formatCells: protection.formatCells ?? false,
|
|
762
|
+
formatColumns: protection.formatColumns ?? false,
|
|
763
|
+
formatRows: protection.formatRows ?? false,
|
|
764
|
+
insertColumns: protection.insertColumns ?? false,
|
|
765
|
+
insertRows: protection.insertRows ?? false,
|
|
766
|
+
insertHyperlinks: protection.insertHyperlinks ?? false,
|
|
767
|
+
deleteColumns: protection.deleteColumns ?? false,
|
|
768
|
+
deleteRows: protection.deleteRows ?? false,
|
|
769
|
+
sort: protection.sort ?? false,
|
|
770
|
+
autoFilter: protection.autoFilter ?? false,
|
|
771
|
+
pivotTables: protection.pivotTables ?? false
|
|
772
|
+
};
|
|
773
|
+
if (protection.password) {
|
|
774
|
+
await worksheet.protect(protection.password, options);
|
|
775
|
+
}
|
|
776
|
+
else {
|
|
777
|
+
worksheet.protect('', options);
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
/**
|
|
781
|
+
* Apply column widths - either from options or auto-calculated
|
|
782
|
+
*/
|
|
783
|
+
applyColumnWidths(worksheet, columns, data) {
|
|
784
|
+
columns.forEach((col, index) => {
|
|
785
|
+
const column = worksheet.getColumn(index + 1);
|
|
786
|
+
if (col.width) {
|
|
787
|
+
column.width = col.width;
|
|
788
|
+
}
|
|
789
|
+
else {
|
|
790
|
+
// Auto-calculate width based on content
|
|
791
|
+
let maxLength = (col.displayName || col.name).length;
|
|
792
|
+
for (const row of data) {
|
|
793
|
+
const values = this.extractRowValues(row, columns);
|
|
794
|
+
const value = values[index];
|
|
795
|
+
const strValue = value != null ? String(value) : '';
|
|
796
|
+
maxLength = Math.max(maxLength, strValue.length);
|
|
797
|
+
}
|
|
798
|
+
// Set width with some padding, capped at 50
|
|
799
|
+
column.width = Math.min(maxLength + 2, 50);
|
|
800
|
+
}
|
|
801
|
+
// Apply column style if specified
|
|
802
|
+
if (col.style) {
|
|
803
|
+
// Column-level styles are applied to new cells, not existing
|
|
804
|
+
// For existing cells, we'd need to iterate
|
|
805
|
+
}
|
|
806
|
+
// Apply number format if specified
|
|
807
|
+
if (col.numberFormat) {
|
|
808
|
+
column.numFmt = col.numberFormat;
|
|
809
|
+
}
|
|
810
|
+
else if (col.dataType === 'currency') {
|
|
811
|
+
column.numFmt = '$#,##0.00';
|
|
812
|
+
}
|
|
813
|
+
else if (col.dataType === 'date') {
|
|
814
|
+
column.numFmt = this.options.defaultDateFormat || 'yyyy-mm-dd';
|
|
815
|
+
}
|
|
816
|
+
else if (col.dataType === 'percentage') {
|
|
817
|
+
column.numFmt = '0.00%';
|
|
818
|
+
}
|
|
819
|
+
// Hide column if specified
|
|
820
|
+
if (col.hidden) {
|
|
821
|
+
column.hidden = true;
|
|
822
|
+
}
|
|
823
|
+
});
|
|
824
|
+
}
|
|
825
|
+
/**
|
|
826
|
+
* Auto-fit columns based on content
|
|
827
|
+
*/
|
|
828
|
+
autoFitColumns(worksheet, columns, data) {
|
|
829
|
+
columns.forEach((col, index) => {
|
|
830
|
+
// Skip if width already set
|
|
831
|
+
if (col.width)
|
|
832
|
+
return;
|
|
833
|
+
const column = worksheet.getColumn(index + 1);
|
|
834
|
+
let maxLength = (col.displayName || col.name).length;
|
|
835
|
+
// Check all data values
|
|
836
|
+
for (const row of data) {
|
|
837
|
+
const values = this.extractRowValuesForSheet(row, columns);
|
|
838
|
+
const value = values[index];
|
|
839
|
+
const strValue = value != null ? String(value) : '';
|
|
840
|
+
maxLength = Math.max(maxLength, Math.min(strValue.length, 50));
|
|
841
|
+
}
|
|
842
|
+
column.width = Math.min(maxLength + 2, 50);
|
|
843
|
+
});
|
|
844
|
+
}
|
|
845
|
+
/**
|
|
846
|
+
* Derive columns from data
|
|
847
|
+
*/
|
|
848
|
+
deriveColumnsFromData(data) {
|
|
849
|
+
if (data.length === 0)
|
|
850
|
+
return [];
|
|
851
|
+
const firstRow = data[0];
|
|
852
|
+
if (Array.isArray(firstRow)) {
|
|
853
|
+
return firstRow.map((_, index) => ({
|
|
854
|
+
name: `Column${index + 1}`,
|
|
855
|
+
displayName: `Column ${index + 1}`
|
|
856
|
+
}));
|
|
857
|
+
}
|
|
858
|
+
else {
|
|
859
|
+
return Object.keys(firstRow).map(key => ({
|
|
860
|
+
name: key,
|
|
861
|
+
displayName: this.formatColumnName(key)
|
|
862
|
+
}));
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
/**
|
|
866
|
+
* Extract row values for sheet (handles both arrays and objects)
|
|
867
|
+
*/
|
|
868
|
+
extractRowValuesForSheet(row, columns) {
|
|
869
|
+
if (Array.isArray(row)) {
|
|
870
|
+
return row;
|
|
871
|
+
}
|
|
872
|
+
return columns.map(col => {
|
|
873
|
+
const value = row[col.name];
|
|
874
|
+
return this.formatValue(value, col);
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
/**
|
|
878
|
+
* Convert column letter to number (A=1, B=2, AA=27, etc.)
|
|
879
|
+
*/
|
|
880
|
+
columnLetterToNumber(letter) {
|
|
881
|
+
let result = 0;
|
|
882
|
+
for (let i = 0; i < letter.length; i++) {
|
|
883
|
+
result = result * 26 + (letter.charCodeAt(i) - 64);
|
|
884
|
+
}
|
|
885
|
+
return result;
|
|
886
|
+
}
|
|
887
|
+
}
|
|
888
|
+
exports.ExcelExporter = ExcelExporter;
|
|
889
|
+
//# sourceMappingURL=excel-exporter.js.map
|