@fecp/designer 5.5.105 → 5.5.106

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.
@@ -0,0 +1,809 @@
1
+ "use strict";
2
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
3
+ const XEUtils = require("xe-utils");
4
+ let VxeUI;
5
+ let globalExcelJS;
6
+ const defaultHeaderBackgroundColor = "f2f2f2";
7
+ const defaultCellFontColor = "000000";
8
+ const defaultCellBorderStyle = "thin";
9
+ const defaultCellBorderColor = "e0e0e0";
10
+ const defaultFontSizeMaps = {
11
+ default: 14,
12
+ medium: 14,
13
+ small: 13,
14
+ mini: 12
15
+ };
16
+ const httpPromiseMaps = {};
17
+ const httpBufferMaps = {};
18
+ function getExcelJS() {
19
+ const ExcelJS = globalExcelJS || window.ExcelJS;
20
+ if (!ExcelJS) {
21
+ console.error("[@vxe-ui/plugin-export-xlsx 4.5.2] Install error. ExcelJS not exist.");
22
+ }
23
+ return ExcelJS;
24
+ }
25
+ function getCellLabel($xeTable, column, cellValue) {
26
+ const { cellType, cellRender, editRender } = column;
27
+ const renderOpts = editRender || cellRender;
28
+ if (cellValue) {
29
+ if (renderOpts) {
30
+ if (["VxeImage", "VxeImageGroup", "VxeUpload"].includes(renderOpts.name || "")) {
31
+ return "";
32
+ }
33
+ }
34
+ if (column.type === "seq") {
35
+ const tableProps = $xeTable.props;
36
+ const { treeConfig } = tableProps;
37
+ if (!treeConfig) {
38
+ if (!isNaN(cellValue)) {
39
+ return Number(cellValue);
40
+ }
41
+ }
42
+ return XEUtils.toValueString(cellValue);
43
+ }
44
+ switch (cellType) {
45
+ case "string":
46
+ return XEUtils.toValueString(cellValue);
47
+ case "number":
48
+ if (!isNaN(cellValue)) {
49
+ return Number(cellValue);
50
+ }
51
+ break;
52
+ }
53
+ if (cellValue.length < 12 && !isNaN(cellValue)) {
54
+ return Number(cellValue);
55
+ }
56
+ }
57
+ return XEUtils.toValueString(cellValue);
58
+ }
59
+ function getFooterData($xeTable, opts, footerData) {
60
+ const $xeGrid = $xeTable.xeGrid;
61
+ const $xeGantt = $xeTable.xeGantt;
62
+ const { footerFilterMethod } = opts;
63
+ return footerFilterMethod ? footerData.filter((items, index) => footerFilterMethod({ $table: $xeTable, $grid: $xeGrid, $gantt: $xeGantt, items, $rowIndex: index })) : footerData;
64
+ }
65
+ function getFooterCellValue($xeTable, opts, row, column) {
66
+ const { renderer } = VxeUI;
67
+ const { computeColumnOpts } = $xeTable.getComputeMaps();
68
+ const columnOpts = computeColumnOpts.value;
69
+ const renderOpts = column.editRender || column.cellRender;
70
+ let footLabelMethod = column.footerExportMethod;
71
+ if (!footLabelMethod && renderOpts && renderOpts.name) {
72
+ const compConf = renderer.get(renderOpts.name);
73
+ if (compConf) {
74
+ footLabelMethod = compConf.tableFooterExportMethod || compConf.footerExportMethod;
75
+ }
76
+ }
77
+ if (!footLabelMethod) {
78
+ footLabelMethod = columnOpts.footerExportMethod;
79
+ }
80
+ if (footLabelMethod) {
81
+ const _columnIndex = $xeTable.getVTColumnIndex(column);
82
+ return footLabelMethod({ $table: $xeTable, items: row, itemIndex: _columnIndex, row, _columnIndex, column, options: opts });
83
+ }
84
+ if ($xeTable.getFooterCellLabel) {
85
+ return getCellLabel($xeTable, column, $xeTable.getFooterCellLabel(row, column));
86
+ }
87
+ if (XEUtils.isArray(row)) {
88
+ const _columnIndex = $xeTable.getVTColumnIndex(column);
89
+ return getCellLabel($xeTable, column, row[_columnIndex]);
90
+ }
91
+ return getCellLabel($xeTable, column, XEUtils.get(row, column.field));
92
+ }
93
+ function getValidColumn(column) {
94
+ const { childNodes } = column;
95
+ const isColGroup = childNodes && childNodes.length;
96
+ if (isColGroup) {
97
+ return getValidColumn(childNodes[0]);
98
+ }
99
+ return column;
100
+ }
101
+ function setExcelRowHeight(excelRow, height) {
102
+ if (height) {
103
+ excelRow.height = XEUtils.floor(height * 0.92, 2);
104
+ }
105
+ }
106
+ function setExcelCellStyle(excelCell, align) {
107
+ excelCell.protection = {
108
+ locked: false
109
+ };
110
+ excelCell.alignment = {
111
+ vertical: "middle",
112
+ horizontal: align || "left"
113
+ };
114
+ }
115
+ function getImgType(name) {
116
+ const index = name.lastIndexOf(".");
117
+ let ext = "";
118
+ if (index > 0) {
119
+ ext = name.substring(index + 1).toLowerCase();
120
+ }
121
+ return ext || "png";
122
+ }
123
+ function getExcelCellCurrencySymbol(currencySymbol) {
124
+ if (currencySymbol === "$") {
125
+ return "$";
126
+ }
127
+ if (currencySymbol === "¥" || currencySymbol === "¥") {
128
+ return "¥";
129
+ }
130
+ return currencySymbol;
131
+ }
132
+ function getExcelCellNumberValSymbol(digits) {
133
+ return `0.${XEUtils.padEnd("0", digits, "0")}`;
134
+ }
135
+ function getExcelCellNumberColorSymbol(showNegativeStatus) {
136
+ if (showNegativeStatus) {
137
+ return "[Red]";
138
+ }
139
+ return "";
140
+ }
141
+ function settExcelCellFormat(workbook, worksheet, excelCell, column, excelRow, row) {
142
+ const { getConfig, getI18n } = VxeUI;
143
+ const { cellType, cellRender, editRender } = column;
144
+ if (cellType !== "string") {
145
+ const renderOpts = editRender || cellRender;
146
+ if (renderOpts) {
147
+ const { name, props = {}, showNegativeStatus } = renderOpts;
148
+ if (name === "VxeNumberInput" || name === "FormatNumberInput") {
149
+ const { type, digits } = props;
150
+ const numberInputConfig = getConfig().numberInput || {};
151
+ if (type === "amount") {
152
+ const numDigits = digits || numberInputConfig.digits || 2;
153
+ const numSymbol = getExcelCellNumberValSymbol(numDigits);
154
+ excelCell.numFmt = `##${numSymbol};${getExcelCellNumberColorSymbol(showNegativeStatus)}\\-##${numSymbol}`;
155
+ const showCurrency = props.showCurrency;
156
+ if (XEUtils.isBoolean(showCurrency) ? showCurrency : numberInputConfig.showCurrency) {
157
+ const currencySymbol = getExcelCellCurrencySymbol(props.currencySymbol || numberInputConfig.currencySymbol || getI18n("vxe.numberInput.currencySymbol") || "");
158
+ excelCell.numFmt = `"${currencySymbol}"#,##${numSymbol};${getExcelCellNumberColorSymbol(showNegativeStatus)}-"${currencySymbol}"#,##${numSymbol}`;
159
+ }
160
+ } else {
161
+ const numDigits = digits || (type === "float" ? numberInputConfig.digits : 2) || 1;
162
+ const numSymbol = getExcelCellNumberValSymbol(numDigits);
163
+ excelCell.numFmt = `##${numSymbol};${getExcelCellNumberColorSymbol(showNegativeStatus)}\\-##${numSymbol}`;
164
+ }
165
+ } else if (renderOpts.name === "VxeImage" || renderOpts.name === "VxeImageGroup") {
166
+ const { width, height } = (renderOpts.name === "VxeImage" ? props : props.imageStyle) || {};
167
+ const cellValue = XEUtils.get(row, column.field);
168
+ if (cellValue) {
169
+ (XEUtils.isArray(cellValue) ? cellValue.slice(0, 1) : [cellValue]).forEach((item) => {
170
+ if (item) {
171
+ const imgUrl = XEUtils.isString(item) ? item : item.url;
172
+ if (imgUrl) {
173
+ const imgBuffer = httpBufferMaps[imgUrl];
174
+ if (imgBuffer) {
175
+ const cellImage = workbook.addImage({
176
+ buffer: imgBuffer,
177
+ extension: getImgType(imgUrl)
178
+ });
179
+ worksheet.addImage(cellImage, {
180
+ tl: {
181
+ col: Math.max(0, XEUtils.toNumber(excelCell.col) - 1),
182
+ row: Math.max(0, XEUtils.toNumber(excelCell.row) - 1)
183
+ },
184
+ ext: {
185
+ width: XEUtils.toNumber(width || height) || 24,
186
+ height: XEUtils.toNumber(height || width) || 24
187
+ }
188
+ });
189
+ }
190
+ }
191
+ }
192
+ });
193
+ }
194
+ } else if (renderOpts.name === "VxeUpload" && props.mode === "image") {
195
+ const { urlField, typeField, imageConfig } = props;
196
+ const { width, height } = imageConfig || {};
197
+ const cellValue = XEUtils.get(row, column.field);
198
+ if (cellValue) {
199
+ (XEUtils.isArray(cellValue) ? cellValue.slice(0, 1) : [cellValue]).forEach((item) => {
200
+ if (item) {
201
+ const imgUrl = XEUtils.isString(item) ? item : item[urlField || "url"];
202
+ if (imgUrl) {
203
+ const imgBuffer = httpBufferMaps[imgUrl];
204
+ if (imgBuffer) {
205
+ const cellImage = workbook.addImage({
206
+ buffer: imgBuffer,
207
+ extension: (XEUtils.isString(item) ? "" : item[typeField]) || getImgType(imgUrl)
208
+ });
209
+ worksheet.addImage(cellImage, {
210
+ tl: {
211
+ col: Math.max(0, XEUtils.toNumber(excelCell.col) - 1),
212
+ row: Math.max(0, XEUtils.toNumber(excelCell.row) - 1)
213
+ },
214
+ ext: {
215
+ width: XEUtils.toNumber(width || height) || 24,
216
+ height: XEUtils.toNumber(height || width) || 24
217
+ }
218
+ });
219
+ }
220
+ }
221
+ }
222
+ });
223
+ }
224
+ }
225
+ }
226
+ }
227
+ }
228
+ function getDefaultBorderStyle() {
229
+ return {
230
+ top: {
231
+ style: defaultCellBorderStyle,
232
+ color: {
233
+ argb: defaultCellBorderColor
234
+ }
235
+ },
236
+ left: {
237
+ style: defaultCellBorderStyle,
238
+ color: {
239
+ argb: defaultCellBorderColor
240
+ }
241
+ },
242
+ bottom: {
243
+ style: defaultCellBorderStyle,
244
+ color: {
245
+ argb: defaultCellBorderColor
246
+ }
247
+ },
248
+ right: {
249
+ style: defaultCellBorderStyle,
250
+ color: {
251
+ argb: defaultCellBorderColor
252
+ }
253
+ }
254
+ };
255
+ }
256
+ function handleParseColumnImageUrl(params) {
257
+ const { columns, datas } = params;
258
+ const urlList = [];
259
+ columns.forEach((column) => {
260
+ const { cellRender, editRender } = column;
261
+ const renderOpts = editRender || cellRender || {};
262
+ const { name, props = {} } = renderOpts;
263
+ if (name === "VxeImage" || name === "VxeImageGroup") {
264
+ datas.forEach((rowRest) => {
265
+ const row = rowRest._row;
266
+ const cellValue = XEUtils.get(row, column.field);
267
+ if (cellValue) {
268
+ (XEUtils.isArray(cellValue) ? cellValue : [cellValue]).forEach((item) => {
269
+ if (item) {
270
+ const imgUrl = item.url ? item.url : `${item || ""}`;
271
+ if (imgUrl) {
272
+ urlList.push(imgUrl);
273
+ }
274
+ }
275
+ });
276
+ }
277
+ });
278
+ } else if (name === "VxeUpload" && props.mode === "image") {
279
+ const { urlField } = props;
280
+ datas.forEach((rowRest) => {
281
+ const row = rowRest._row;
282
+ const cellValue = XEUtils.get(row, column.field);
283
+ if (cellValue) {
284
+ (XEUtils.isArray(cellValue) ? cellValue : [cellValue]).forEach((item) => {
285
+ if (item) {
286
+ const imgUrl = XEUtils.isString(item) ? item : item[urlField || "url"];
287
+ if (imgUrl) {
288
+ urlList.push(imgUrl);
289
+ }
290
+ }
291
+ });
292
+ }
293
+ });
294
+ }
295
+ });
296
+ if (urlList.length) {
297
+ return handleFetchImage(urlList);
298
+ }
299
+ }
300
+ function handleFetchImage(urlList) {
301
+ let fIndex = 0;
302
+ const handleStartFetch = () => {
303
+ if (fIndex < urlList.length) {
304
+ const restList = [];
305
+ let count = 1;
306
+ for (; fIndex < urlList.length; fIndex++) {
307
+ const url = urlList[fIndex];
308
+ if (!httpBufferMaps[url]) {
309
+ if (!httpPromiseMaps[url]) {
310
+ count++;
311
+ httpPromiseMaps[url] = fetch(url).then((res) => res.arrayBuffer()).then((arrayBuffer) => {
312
+ httpBufferMaps[url] = arrayBuffer;
313
+ delete httpPromiseMaps[url];
314
+ }).catch(() => {
315
+ delete httpPromiseMaps[url];
316
+ });
317
+ }
318
+ restList.push(httpPromiseMaps[url]);
319
+ }
320
+ if (count >= 4) {
321
+ break;
322
+ }
323
+ }
324
+ return Promise.all(restList).then(() => {
325
+ if (fIndex < urlList.length) {
326
+ return handleStartFetch();
327
+ }
328
+ });
329
+ }
330
+ return Promise.resolve();
331
+ };
332
+ return handleStartFetch();
333
+ }
334
+ function exportXLSX(params) {
335
+ const msgKey = XEUtils.uniqueId("xlsx");
336
+ const { modal, getI18n } = VxeUI;
337
+ const { $table, $grid, options, columns, colgroups, datas } = params;
338
+ const tableProps = $table.props;
339
+ const tableReactData = $table.reactData;
340
+ const { computeSize, computeColumnOpts } = $table.getComputeMaps();
341
+ const { headerAlign: allHeaderAlign, align: allAlign, footerAlign: allFooterAlign } = tableProps;
342
+ const { rowHeight } = tableReactData;
343
+ const { message, download, sheetName, isHeader, isFooter, isMerge, isColgroup, isTitle, useStyle, sheetMethod } = options;
344
+ const vSize = computeSize.value;
345
+ const columnOpts = computeColumnOpts.value;
346
+ const showMsg = message !== false;
347
+ const mergeCells = $table.getMergeCells();
348
+ const fontSize = defaultFontSizeMaps[vSize || ""] || 14;
349
+ const colList = [];
350
+ let footList = [];
351
+ const footArr = [];
352
+ const sheetCols = [];
353
+ const sheetMerges = [];
354
+ let beforeRowCount = 0;
355
+ columns.forEach((column) => {
356
+ const { id, renderWidth } = column;
357
+ sheetCols.push({
358
+ key: id,
359
+ width: XEUtils.ceil(renderWidth / 7.2, 1)
360
+ });
361
+ });
362
+ if (isHeader) {
363
+ if (isColgroup && colgroups) {
364
+ colgroups.forEach((cols, rIndex) => {
365
+ const groupHead = {};
366
+ columns.forEach((column) => {
367
+ groupHead[column.id] = null;
368
+ });
369
+ cols.forEach((column) => {
370
+ const { _colSpan, _rowSpan } = column;
371
+ const validColumn = getValidColumn(column);
372
+ const columnIndex = columns.indexOf(validColumn);
373
+ const headExportMethod = column.headerExportMethod || columnOpts.headerExportMethod;
374
+ groupHead[validColumn.id] = headExportMethod ? headExportMethod({ column, options, $table }) : isTitle ? column.getTitle() : validColumn.field;
375
+ if (_colSpan > 1 || _rowSpan > 1) {
376
+ sheetMerges.push({
377
+ s: { r: rIndex, c: columnIndex },
378
+ e: { r: rIndex + _rowSpan - 1, c: columnIndex + _colSpan - 1 }
379
+ });
380
+ }
381
+ });
382
+ colList.push(groupHead);
383
+ });
384
+ } else {
385
+ const colHead = {};
386
+ columns.forEach((column) => {
387
+ const { id, field } = column;
388
+ const headExportMethod = column.headerExportMethod || columnOpts.headerExportMethod;
389
+ colHead[id] = headExportMethod ? headExportMethod({ column, options, $table }) : isTitle ? column.getTitle() : field;
390
+ });
391
+ colList.push(colHead);
392
+ }
393
+ beforeRowCount += colList.length;
394
+ }
395
+ if (isMerge) {
396
+ mergeCells.forEach((mergeItem) => {
397
+ const { row: mergeRowIndex, rowspan: mergeRowspan, col: mergeColIndex, colspan: mergeColspan } = mergeItem;
398
+ sheetMerges.push({
399
+ s: { r: mergeRowIndex + beforeRowCount, c: mergeColIndex },
400
+ e: { r: mergeRowIndex + beforeRowCount + mergeRowspan - 1, c: mergeColIndex + mergeColspan - 1 }
401
+ });
402
+ });
403
+ }
404
+ const rowList = datas;
405
+ const rowArr = rowList.map((item) => {
406
+ const rest = {};
407
+ columns.forEach((column) => {
408
+ rest[column.id] = getCellLabel($table, column, item[column.id]);
409
+ });
410
+ return rest;
411
+ });
412
+ beforeRowCount += rowList.length;
413
+ if (isFooter) {
414
+ const { footerData } = $table.getTableData();
415
+ footList = getFooterData($table, options, footerData);
416
+ const mergeFooterItems = $table.getMergeFooterItems();
417
+ if (isMerge) {
418
+ mergeFooterItems.forEach((mergeItem) => {
419
+ const { row: mergeRowIndex, rowspan: mergeRowspan, col: mergeColIndex, colspan: mergeColspan } = mergeItem;
420
+ sheetMerges.push({
421
+ s: { r: mergeRowIndex + beforeRowCount, c: mergeColIndex },
422
+ e: { r: mergeRowIndex + beforeRowCount + mergeRowspan - 1, c: mergeColIndex + mergeColspan - 1 }
423
+ });
424
+ });
425
+ }
426
+ footList.forEach((row) => {
427
+ const item = {};
428
+ columns.forEach((column) => {
429
+ item[column.id] = getFooterCellValue($table, options, row, column);
430
+ });
431
+ footArr.push(item);
432
+ });
433
+ }
434
+ const exportMethod = () => {
435
+ const ExcelObj = getExcelJS();
436
+ if (!ExcelObj) {
437
+ if (modal) {
438
+ modal.close(msgKey);
439
+ }
440
+ return Promise.resolve({
441
+ status: false
442
+ });
443
+ }
444
+ const workbook = new ExcelObj.Workbook();
445
+ const sheet = workbook.addWorksheet(sheetName || "Sheet1");
446
+ workbook.creator = "vxe-table";
447
+ sheet.columns = sheetCols;
448
+ if (isHeader) {
449
+ sheet.addRows(colList).forEach((excelRow) => {
450
+ if (useStyle) {
451
+ setExcelRowHeight(excelRow, rowHeight);
452
+ }
453
+ excelRow.eachCell((excelCell) => {
454
+ const excelCol = sheet.getColumn(excelCell.col);
455
+ const column = $table.getColumnById(excelCol.key);
456
+ const { headerAlign, align } = column;
457
+ setExcelCellStyle(excelCell, headerAlign || align || allHeaderAlign || allAlign);
458
+ if (useStyle) {
459
+ Object.assign(excelCell, {
460
+ font: {
461
+ bold: true,
462
+ size: fontSize,
463
+ color: {
464
+ argb: defaultCellFontColor
465
+ }
466
+ },
467
+ fill: {
468
+ type: "pattern",
469
+ pattern: "solid",
470
+ fgColor: {
471
+ argb: defaultHeaderBackgroundColor
472
+ }
473
+ },
474
+ border: getDefaultBorderStyle()
475
+ });
476
+ }
477
+ });
478
+ });
479
+ }
480
+ sheet.addRows(rowArr).forEach((excelRow, rIndex) => {
481
+ const row = rowList[rIndex]._row;
482
+ if (useStyle) {
483
+ setExcelRowHeight(excelRow, rowHeight);
484
+ }
485
+ excelRow.eachCell((excelCell) => {
486
+ const excelCol = sheet.getColumn(excelCell.col);
487
+ const column = $table.getColumnById(excelCol.key);
488
+ if (column) {
489
+ const { align } = column;
490
+ setExcelCellStyle(excelCell, align || allAlign);
491
+ settExcelCellFormat(workbook, sheet, excelCell, column, excelRow, row);
492
+ if (useStyle) {
493
+ Object.assign(excelCell, {
494
+ font: {
495
+ size: fontSize,
496
+ color: {
497
+ argb: defaultCellFontColor
498
+ }
499
+ },
500
+ border: getDefaultBorderStyle()
501
+ });
502
+ }
503
+ }
504
+ });
505
+ });
506
+ if (isFooter) {
507
+ sheet.addRows(footArr).forEach((excelRow, rIndex) => {
508
+ const row = footList[rIndex];
509
+ if (useStyle) {
510
+ setExcelRowHeight(excelRow, rowHeight);
511
+ }
512
+ excelRow.eachCell((excelCell) => {
513
+ const excelCol = sheet.getColumn(excelCell.col);
514
+ const column = $table.getColumnById(excelCol.key);
515
+ if (column) {
516
+ const { footerAlign, align } = column;
517
+ setExcelCellStyle(excelCell, footerAlign || align || allFooterAlign || allAlign);
518
+ settExcelCellFormat(workbook, sheet, excelCell, column, excelRow, row);
519
+ if (useStyle) {
520
+ Object.assign(excelCell, {
521
+ font: {
522
+ size: fontSize,
523
+ color: {
524
+ argb: defaultCellFontColor
525
+ }
526
+ },
527
+ border: getDefaultBorderStyle()
528
+ });
529
+ }
530
+ }
531
+ });
532
+ });
533
+ }
534
+ return Promise.resolve(
535
+ // 自定义处理
536
+ sheetMethod ? sheetMethod({
537
+ options,
538
+ workbook,
539
+ worksheet: sheet,
540
+ columns,
541
+ colgroups,
542
+ datas,
543
+ $grid,
544
+ $table
545
+ }) : null
546
+ ).then(() => {
547
+ sheetMerges.forEach(({ s, e }) => {
548
+ sheet.mergeCells(s.r + 1, s.c + 1, e.r + 1, e.c + 1);
549
+ });
550
+ return workbook.xlsx.writeBuffer().then((buffer) => {
551
+ const type = "application/octet-stream";
552
+ const blob = new Blob([buffer], { type });
553
+ if (modal) {
554
+ modal.close(msgKey);
555
+ }
556
+ if (showMsg && modal) {
557
+ modal.message({
558
+ content: getI18n("vxe.table.expSuccess"),
559
+ status: "success"
560
+ });
561
+ }
562
+ if (download) {
563
+ downloadFile(params, blob, options);
564
+ return {
565
+ status: true
566
+ };
567
+ }
568
+ return { type, content: "", blob };
569
+ });
570
+ }).catch(() => {
571
+ if (modal) {
572
+ modal.close(msgKey);
573
+ }
574
+ if (showMsg && modal) {
575
+ modal.message({
576
+ content: getI18n("vxe.table.expError"),
577
+ status: "error"
578
+ });
579
+ }
580
+ return {
581
+ status: false
582
+ };
583
+ });
584
+ };
585
+ return Promise.all([
586
+ handleParseColumnImageUrl(params)
587
+ ]).then(() => {
588
+ if (showMsg && modal) {
589
+ modal.message({
590
+ id: msgKey,
591
+ content: getI18n("vxe.table.expLoading"),
592
+ status: "loading",
593
+ duration: -1
594
+ });
595
+ return new Promise((resolve) => {
596
+ setTimeout(() => {
597
+ resolve(exportMethod());
598
+ }, 1500);
599
+ });
600
+ }
601
+ return exportMethod();
602
+ });
603
+ }
604
+ function downloadFile(params, blob, options) {
605
+ const { modal, t } = VxeUI;
606
+ const { message, filename, type } = options;
607
+ const showMsg = message !== false;
608
+ if (window.Blob) {
609
+ if (navigator.msSaveBlob) {
610
+ navigator.msSaveBlob(blob, `${filename}.${type}`);
611
+ } else {
612
+ const linkElem = document.createElement("a");
613
+ linkElem.target = "_blank";
614
+ linkElem.download = `${filename}.${type}`;
615
+ linkElem.href = URL.createObjectURL(blob);
616
+ document.body.appendChild(linkElem);
617
+ linkElem.click();
618
+ document.body.removeChild(linkElem);
619
+ }
620
+ } else {
621
+ if (showMsg && modal) {
622
+ modal.alert({ content: t("vxe.error.notExp"), status: "error" });
623
+ }
624
+ }
625
+ }
626
+ function importError(params) {
627
+ const { modal, t } = VxeUI;
628
+ const { $table, options } = params;
629
+ const tableInternalData = $table.internalData;
630
+ const { _importReject } = tableInternalData;
631
+ const showMsg = options.message !== false;
632
+ if (showMsg && modal) {
633
+ modal.message({ content: t("vxe.error.impFields"), status: "error" });
634
+ }
635
+ if (_importReject) {
636
+ _importReject({ status: false });
637
+ }
638
+ }
639
+ function importXLSX(params) {
640
+ const { modal, getI18n } = VxeUI;
641
+ const { $table, columns, options, file } = params;
642
+ const tableInternalData = $table.internalData;
643
+ const { _importResolve } = tableInternalData;
644
+ const ExcelObj = getExcelJS();
645
+ if (!ExcelObj) {
646
+ if (_importResolve) {
647
+ _importResolve({
648
+ status: false
649
+ });
650
+ }
651
+ return;
652
+ }
653
+ const showMsg = options.message !== false;
654
+ const fileReader = new FileReader();
655
+ fileReader.onerror = () => {
656
+ importError(params);
657
+ };
658
+ fileReader.onload = (evnt) => {
659
+ const tableFieldMaps = {};
660
+ const tableTitleMaps = {};
661
+ columns.forEach((column) => {
662
+ const field = column.field;
663
+ const title = column.getTitle();
664
+ if (field) {
665
+ tableFieldMaps[field] = column;
666
+ }
667
+ if (title) {
668
+ tableTitleMaps[column.getTitle()] = column;
669
+ }
670
+ });
671
+ const workbook = new ExcelObj.Workbook();
672
+ const readerTarget = evnt.target;
673
+ if (readerTarget) {
674
+ workbook.xlsx.load(readerTarget.result).then((wb) => {
675
+ const firstSheet = wb.worksheets[0];
676
+ if (firstSheet) {
677
+ const sheetValues = Array.from(firstSheet.getSheetValues());
678
+ const fieldIndex = XEUtils.findIndexOf(sheetValues, (list) => list && list.length > 0);
679
+ const heads = sheetValues[fieldIndex];
680
+ let status = false;
681
+ const fields = heads.map((key) => {
682
+ if (tableFieldMaps[key]) {
683
+ if (!status) {
684
+ status = true;
685
+ }
686
+ return key;
687
+ }
688
+ if (tableTitleMaps[key]) {
689
+ if (!status) {
690
+ status = true;
691
+ }
692
+ return tableTitleMaps[key].field;
693
+ }
694
+ return null;
695
+ });
696
+ if (status) {
697
+ const records = sheetValues.slice(fieldIndex + 1).map((list) => {
698
+ const record = {};
699
+ fields.forEach((field, cIndex) => {
700
+ if (field) {
701
+ record[field] = XEUtils.isUndefined(list[cIndex]) ? null : list[cIndex];
702
+ }
703
+ });
704
+ return record;
705
+ });
706
+ $table.createData(records).then((data) => {
707
+ let loadRest;
708
+ if (options.mode === "insertTop") {
709
+ loadRest = $table.insertAt(data, 0);
710
+ } else if (options.mode === "insertBottom" || options.mode === "insert") {
711
+ loadRest = $table.insertAt(data, -1);
712
+ } else {
713
+ const { visibleData } = $table.getTableData();
714
+ loadRest = $table.remove(visibleData).then(() => $table.insertAt(data, -1));
715
+ }
716
+ return loadRest.then(() => {
717
+ if (_importResolve) {
718
+ _importResolve({ status: true });
719
+ }
720
+ });
721
+ });
722
+ if (showMsg && modal) {
723
+ modal.message({ content: getI18n("vxe.table.impSuccess", [records.length]), status: "success" });
724
+ }
725
+ } else {
726
+ importError(params);
727
+ }
728
+ } else {
729
+ importError(params);
730
+ }
731
+ });
732
+ } else {
733
+ importError(params);
734
+ }
735
+ };
736
+ fileReader.readAsArrayBuffer(file);
737
+ }
738
+ function hasAdvancedVersion(tableVersion) {
739
+ const vRest = tableVersion ? `${tableVersion}`.match(/(\d+)\.(\d+)\./) : null;
740
+ return vRest && XEUtils.toNumber(vRest[1]) >= 4 && XEUtils.toNumber(vRest[2]) >= 12;
741
+ }
742
+ function handleImportEvent(params) {
743
+ if (params.options.type === "xlsx") {
744
+ if (VxeUI && hasAdvancedVersion(VxeUI.tableVersion || "")) {
745
+ return {
746
+ result: importXLSX(params),
747
+ status: false
748
+ };
749
+ }
750
+ return false;
751
+ }
752
+ }
753
+ function handleExportEvent(params) {
754
+ if (params.options.type === "xlsx") {
755
+ if (VxeUI && hasAdvancedVersion(VxeUI.tableVersion || "")) {
756
+ return {
757
+ result: exportXLSX(params),
758
+ status: false
759
+ };
760
+ }
761
+ return false;
762
+ }
763
+ }
764
+ function pluginSetup(options) {
765
+ globalExcelJS = options ? options.ExcelJS : null;
766
+ }
767
+ const VxeUIPluginExportXLSX = {
768
+ setConfig: pluginSetup,
769
+ install(core, options) {
770
+ VxeUI = core;
771
+ if (VxeUI.checkVersion) {
772
+ const pVersion = 4;
773
+ const sVersion = 11;
774
+ if (!VxeUI.checkVersion(VxeUI.tableVersion, pVersion, sVersion)) {
775
+ console.error(`[@vxe-ui/plugin-export-xlsx 4.5.2] ${VxeUI.getI18n("vxe.error.errorVersion", [`vxe-table@${VxeUI.tableVersion || "?"}`, `vxe-table v${pVersion}.${sVersion}+`])} https://vxeui.com/other4/#/plugin-export-xlsx/install`);
776
+ }
777
+ } else {
778
+ if (!/^(4)\./.test(VxeUI.uiVersion || VxeUI.tableVersion)) {
779
+ console.error("[@vxe-ui/plugin-export-xlsx 4.5.2] Requires vxe-table 4.7.0+ version. https://vxeui.com/other4/#/plugin-export-xlsx/install");
780
+ }
781
+ }
782
+ if (options) {
783
+ pluginSetup(options);
784
+ }
785
+ VxeUI.setConfig({
786
+ table: {
787
+ importConfig: {
788
+ _typeMaps: {
789
+ xlsx: 1
790
+ }
791
+ },
792
+ exportConfig: {
793
+ _typeMaps: {
794
+ xlsx: 1
795
+ }
796
+ }
797
+ }
798
+ });
799
+ VxeUI.interceptor.mixin({
800
+ "event.import": handleImportEvent,
801
+ "event.export": handleExportEvent
802
+ });
803
+ }
804
+ };
805
+ if (typeof window !== "undefined" && window.VxeUI && window.VxeUI.use) {
806
+ window.VxeUI.use(VxeUIPluginExportXLSX);
807
+ }
808
+ exports.VxeUIPluginExportXLSX = VxeUIPluginExportXLSX;
809
+ exports.default = VxeUIPluginExportXLSX;