@bchr/bchr-front-lib 0.6.9 → 0.6.11

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.
@@ -1,374 +1,373 @@
1
- import {saveAs} from "file-saver";
2
- import Values from "./Values";
3
-
4
-
5
- const DEFAULT_BORDER_COLOR_FOR_EXCEL = "CCCCCC";
6
- const DEFAULT_FG_COLOR_FOR_EXCEL = "FFFFFF";
7
- const DEFAULT_COL_HEADER_BG_COLOR = "F2F3F5";
8
-
9
- const excelHeaderRowStyle = {
10
- font: {
11
- size: 10,
12
- bold: true
13
- },
14
- alignment: {
15
- vertical: 'middle',
16
- horizontal: 'center'
17
- },
18
- fill: {
19
- type: 'pattern',
20
- pattern: 'solid',
21
- fgColor: {
22
- argb: DEFAULT_COL_HEADER_BG_COLOR
23
- }
24
- },
25
- border: {
26
- top: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
27
- left: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
28
- bottom: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
29
- right: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}}
30
- }
31
- }
32
-
33
- const excelBodyRowStyle = {
34
- font: {
35
- size: 10,
36
- bold: true
37
- },
38
- alignment: {
39
- vertical: 'middle',
40
- horizontal: 'center'
41
- },
42
- fill: {
43
- type: 'pattern',
44
- pattern: 'solid',
45
- fgColor: {
46
- argb: DEFAULT_FG_COLOR_FOR_EXCEL
47
- }
48
- },
49
- border: {
50
- top: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
51
- left: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
52
- bottom: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
53
- right: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}}
54
- }
55
- }
56
-
57
- const ExcelKits = {
58
-
59
- exportHandsontableData2CSVFile(handsonTableInstance, fileName) {
60
- if (handsonTableInstance === null || handsonTableInstance === void 0) {
61
- throw new IllegalArgumentError("HandsontableInstance is null for ExcelKits#exportHandsontableData2CSVFile");
62
- }
63
-
64
- if (Values.isBlankString(fileName)) {
65
- fileName = `${t('ui.export.csv.file')}-` + new Date().getTime();
66
- }
67
-
68
- const exportPlugin = handsonTableInstance.getPlugin('exportFile');
69
- exportPlugin.downloadFile('csv', {
70
- bom: false,
71
- columnDelimiter: ',',
72
- columnHeaders: false,
73
- exportHiddenColumns: true,
74
- exportHiddenRows: true,
75
- fileExtension: 'csv',
76
- filename: fileName,
77
- mimeType: 'text/csv',
78
- rowDelimiter: '\r\n',
79
- rowHeaders: true
80
- });
81
- },
82
-
83
- exportHandsontableData2ExcelFile(handsontableInstance, fileName) {
84
- if (handsontableInstance === null || handsontableInstance === void 0) {
85
- throw new IllegalArgumentError("HandsontableInstance is null for ExcelKits#exportHandsontableData2ExcelFile");
86
- }
87
-
88
- if (Values.isBlankString(fileName)) {
89
- fileName = t('ui.export.excel.file') + "-" + new Date().getTime();
90
- }
91
-
92
- let exportGridColumns = [];
93
- let exportGridData = handsontableInstance.hotInstance.getSourceData();
94
-
95
- window.handsontableInstance = handsontableInstance;
96
-
97
- if (handsontableInstance.columnSettings && handsontableInstance.columnSettings.length) {
98
- handsontableInstance.columnSettings.forEach(column => {
99
- exportGridColumns.push({
100
- ...column,
101
- dataIndex: column.data
102
- })
103
- })
104
- }
105
-
106
- console.log('exportGridColumns :', exportGridColumns)
107
- console.log('exportGridData :', exportGridData)
108
-
109
- this.exportTableData2ExcelFile(exportGridColumns, exportGridData, fileName);
110
- },
111
-
112
- async exportTableData2ExcelFile(columns = [], data = [], fileName) {
113
-
114
- if (Values.isBlankString(fileName)) {
115
- fileName = `${t('ui.export.excel.file')}-` + new Date().getTime();
116
- }
117
-
118
- const excelModule = await import("exceljs/dist/exceljs.bare.min.js");
119
- const ExcelJS = excelModule.default ?? excelModule;
120
- const workbook = new ExcelJS.Workbook();
121
- const worksheet = workbook.addWorksheet('sheet');
122
-
123
- let exportGridColumnsData = this.gatherExcelColumnHeaderStructureInfo(this.extractColumns(columns));
124
- let totalColumnHeaderRowCount = exportGridColumnsData.totalColumnHeaderRowCount;
125
- let columnHeaderData = exportGridColumnsData.columnHeaderData;
126
- let rowHeaderArray = exportGridColumnsData.rowHeaderArray;
127
- let columnHeaderMergeCellArray = exportGridColumnsData.columnHeaderMergeCellArray;
128
-
129
- let exportGridData = data;
130
-
131
- worksheet.columns = columnHeaderData;
132
- worksheet.spliceRows(1, 1);
133
- worksheet.insertRows(1, rowHeaderArray);
134
-
135
- exportGridData.forEach(gridItem => {
136
- worksheet.addRow(gridItem);
137
- })
138
-
139
- columnHeaderMergeCellArray.forEach(mergeCells => {
140
- worksheet.mergeCells(mergeCells)
141
- })
142
-
143
- // 设置表头样式
144
- const headerRows = worksheet.getRows(1, totalColumnHeaderRowCount);
145
- headerRows.forEach((row, rowIndex) => {
146
- row.eachCell((cell, cellIndex) => {
147
- cell.style = excelHeaderRowStyle;
148
- })
149
- })
150
-
151
- // 设置表体样式
152
- const bodyRows = worksheet.getRows(totalColumnHeaderRowCount + 1, totalColumnHeaderRowCount + exportGridData.length + 5);
153
- bodyRows.forEach((row) => {
154
- row.eachCell((cell, cellIndex) => {
155
- cell.style = excelBodyRowStyle;
156
- })
157
- })
158
-
159
- workbook.xlsx.writeBuffer().then(buffer => {
160
- saveAs(new Blob([buffer], {
161
- type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
162
- }), fileName + ".xlsx")
163
- })
164
- },
165
-
166
- extractColumns(children, level) {
167
- let _this = this;
168
- if (level === void 0) {
169
- level = 0;
170
- }
171
- return Array.isArray(children)
172
- ? children.map(function (child) {
173
- return _this.extractChild(child, level);
174
- })
175
- : [children, _this.extractChild(children, level)];
176
- },
177
-
178
- extractChild(child, level) {
179
- let _this = this;
180
- if (level === void 0) {
181
- level = 0;
182
- }
183
- return Object.assign(Object.assign({}, child), {
184
- level: level,
185
- children: child.children && _this.extractColumns(child.children, level + 1)
186
- })
187
- },
188
-
189
- /**
190
- * 收集Excel列头信息(复合表头)
191
- * @param gridColumns
192
- * @returns {{totalColumnCount: {int}, totalColumnHeaderRowCount: {int}, columnHeaderArray: [] }}
193
- */
194
- gatherExcelColumnHeaderStructureInfo(gridColumns) {
195
- let [totalColumnCount, totalColumnHeaderRowCount] = this.getTotalColumnCountAndTotalColumnHeaderRowCount(gridColumns, 0, 1);
196
-
197
- let columnHeaderArray = [];
198
- for (let i = 0, count = gridColumns.length; i < count; i++) {
199
- let column = gridColumns[i];
200
- this.$buildColumnHeaderArray(column, columnHeaderArray);
201
- }
202
-
203
- for (let i = 0, count = columnHeaderArray.length; i < count; i++) {
204
- let subArray = columnHeaderArray[i];
205
- let lastElem = subArray[subArray.length - 1];
206
- for (let r = subArray.length; r < totalColumnHeaderRowCount; r++) {
207
- subArray.push(lastElem);
208
- }
209
- }
210
-
211
- let columnHeaderMergeCellArray = [];
212
- let nextColIndex = 1;
213
- for (let i = 0, count = gridColumns.length; i < count; i++) {
214
- let gridColumn = gridColumns[i];
215
- nextColIndex = this.gatherExcelColumnHeaderMergeCellInfo(gridColumn, columnHeaderMergeCellArray, totalColumnHeaderRowCount, 1, nextColIndex);
216
- }
217
-
218
- let rowHeaderArray = [];
219
- for (let totalColumnHeaderRowStep = 0; totalColumnHeaderRowStep < totalColumnHeaderRowCount; totalColumnHeaderRowStep++) {
220
- let rowHeaderColumns = [];
221
- columnHeaderArray.forEach(columns => {
222
- rowHeaderColumns.push(columns[totalColumnHeaderRowStep])
223
- })
224
- rowHeaderArray.push(rowHeaderColumns);
225
- }
226
-
227
- let columnHeaderData = [];
228
- let columnIndex = 0;
229
-
230
- let addColumns = (columns, result) => {
231
- columns.forEach((column) => {
232
- let exporterColumn = this.$normalizedExportColumn(column);
233
- result.push(exporterColumn);
234
- if (column.children && column.children.length > 1) {
235
- let children = exporterColumn.columns = [];
236
- addColumns(column.children, children);
237
- } else {
238
- columnIndex++;
239
- }
240
- });
241
- };
242
- addColumns(gridColumns, columnHeaderData);
243
- const autoCreateArrayItems = function (column, columnStep, columns) {
244
- if (column.columns && column.columns.length) {
245
- columns.splice(columnStep, 1, ...column.columns);
246
- }
247
-
248
- if (columnStep === columns.length - 1) {
249
- console.log('success');
250
- } else {
251
- columnStep++;
252
- autoCreateArrayItems(columns[columnStep], columnStep, columns);
253
- }
254
- }
255
-
256
- autoCreateArrayItems(columnHeaderData[0], 0, columnHeaderData);
257
-
258
- return {
259
- totalColumnCount,
260
- totalColumnHeaderRowCount,
261
- columnHeaderArray,
262
- columnHeaderData,
263
- rowHeaderArray,
264
- columnHeaderMergeCellArray
265
- };
266
- },
267
-
268
- /**
269
- * @return {Array} eg -
270
- * [
271
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
272
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
273
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
274
- * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
275
- * ]
276
- */
277
- gatherExcelColumnHeaderMergeCellInfo(gridColumn, columnHeaderMergeCellArray, totalColumnHeaderRowCount, currentColumnHeaderRowIndex = 1, colIndex) {
278
-
279
- let allChildColumnCount = this.$countAllChildColumnCount(gridColumn);
280
-
281
- if (Array.isArray(gridColumn.children) && gridColumn.children.length) {
282
- let gridColumns = gridColumn.children;
283
- let gridColumnCount = gridColumns.length;
284
-
285
- columnHeaderMergeCellArray.push([currentColumnHeaderRowIndex, colIndex, currentColumnHeaderRowIndex, colIndex + allChildColumnCount - 1]);
286
-
287
- for (let i = 0; i < gridColumnCount; i++) {
288
- this.gatherExcelColumnHeaderMergeCellInfo(gridColumns[i], columnHeaderMergeCellArray, totalColumnHeaderRowCount,
289
- currentColumnHeaderRowIndex + 1, colIndex + i);
290
- }
291
- } else {
292
- if (currentColumnHeaderRowIndex !== totalColumnHeaderRowCount) {
293
- columnHeaderMergeCellArray.push([currentColumnHeaderRowIndex, colIndex, totalColumnHeaderRowCount, colIndex]);
294
- }
295
- }
296
-
297
- return colIndex + allChildColumnCount;
298
- },
299
-
300
- $countAllChildColumnCount(gridColumn) {
301
- let columnCount = 0;
302
- if (gridColumn === null || gridColumn === void 0) {
303
- return columnCount;
304
- }
305
-
306
- if (Array.isArray(gridColumn.children) && gridColumn.children.length) {
307
- let gridColumns = gridColumn.children;
308
- let gridColumnCount = gridColumns.length;
309
-
310
- for (let i = 0; i < gridColumnCount; i++) {
311
- let childColumnCount = this.$countAllChildColumnCount(gridColumns[i]);
312
- columnCount = columnCount + childColumnCount;
313
- }
314
- } else {
315
- columnCount = 1;
316
- }
317
-
318
- return columnCount;
319
- },
320
-
321
- getTotalColumnCountAndTotalColumnHeaderRowCount(gridColumns, totalColumnCount, totalColumnHeaderCount) {
322
- let maxN2 = totalColumnHeaderCount;
323
- for (let i = 0, count = gridColumns.length; i < count; i++) {
324
- let column = gridColumns[i];
325
- if (Array.isArray(column.children) && column.children.length) {
326
- let [n1, n2] = this.getTotalColumnCountAndTotalColumnHeaderRowCount(column.children, 0, totalColumnHeaderCount + 1);
327
- totalColumnCount = totalColumnCount + n1;
328
- if (n2 > maxN2) {
329
- maxN2 = n2;
330
- }
331
- } else {
332
- totalColumnCount = totalColumnCount + 1;
333
- }
334
- }
335
-
336
- return [totalColumnCount, maxN2]
337
- },
338
-
339
- $buildColumnHeaderArray(column, columnHeaderArray, parentHeaderTitles) {
340
- if (Array.isArray(column.children) && column.children.length) {
341
- if (parentHeaderTitles === null || parentHeaderTitles == void 0) {
342
- parentHeaderTitles = [column.title];
343
- } else {
344
- parentHeaderTitles.push(column.title);
345
- }
346
-
347
- for (let i = 0, count = column.children.length; i < count; i++) {
348
- let childColumn = column.children[i];
349
- this.$buildColumnHeaderArray(childColumn, columnHeaderArray, parentHeaderTitles);
350
- }
351
- } else {
352
- let columnSelfHeaderTitleArray = [];
353
- if (Array.isArray(parentHeaderTitles)) {
354
- columnSelfHeaderTitleArray.push(...parentHeaderTitles);
355
- }
356
-
357
- columnSelfHeaderTitleArray.push(column.title);
358
-
359
- columnHeaderArray.push(columnSelfHeaderTitleArray)
360
- }
361
- },
362
-
363
-
364
- $normalizedExportColumn(column) {
365
- return {
366
- header : column.title,
367
- key : column.dataIndex,
368
- locked : column.locked,
369
- width : column.width * 0.1
370
- }
371
- }
372
- }
373
-
374
- export default ExcelKits;
1
+ import * as ExcelJS from "exceljs/dist/exceljs.bare.js";
2
+ import {saveAs} from "file-saver";
3
+ import Values from "./Values";
4
+
5
+
6
+ const DEFAULT_BORDER_COLOR_FOR_EXCEL = "CCCCCC";
7
+ const DEFAULT_FG_COLOR_FOR_EXCEL = "FFFFFF";
8
+ const DEFAULT_COL_HEADER_BG_COLOR = "F2F3F5";
9
+
10
+ const excelHeaderRowStyle = {
11
+ font: {
12
+ size: 10,
13
+ bold: true
14
+ },
15
+ alignment: {
16
+ vertical: 'middle',
17
+ horizontal: 'center'
18
+ },
19
+ fill: {
20
+ type: 'pattern',
21
+ pattern: 'solid',
22
+ fgColor: {
23
+ argb: DEFAULT_COL_HEADER_BG_COLOR
24
+ }
25
+ },
26
+ border: {
27
+ top: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
28
+ left: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
29
+ bottom: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
30
+ right: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}}
31
+ }
32
+ }
33
+
34
+ const excelBodyRowStyle = {
35
+ font: {
36
+ size: 10,
37
+ bold: true
38
+ },
39
+ alignment: {
40
+ vertical: 'middle',
41
+ horizontal: 'center'
42
+ },
43
+ fill: {
44
+ type: 'pattern',
45
+ pattern: 'solid',
46
+ fgColor: {
47
+ argb: DEFAULT_FG_COLOR_FOR_EXCEL
48
+ }
49
+ },
50
+ border: {
51
+ top: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
52
+ left: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
53
+ bottom: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}},
54
+ right: {style: 'thin', color: {argb: DEFAULT_BORDER_COLOR_FOR_EXCEL}}
55
+ }
56
+ }
57
+
58
+ const ExcelKits = {
59
+
60
+ exportHandsontableData2CSVFile(handsonTableInstance, fileName) {
61
+ if (handsonTableInstance === null || handsonTableInstance === void 0) {
62
+ throw new IllegalArgumentError("HandsontableInstance is null for ExcelKits#exportHandsontableData2CSVFile");
63
+ }
64
+
65
+ if (Values.isBlankString(fileName)) {
66
+ fileName = `${t('ui.export.csv.file')}-` + new Date().getTime();
67
+ }
68
+
69
+ const exportPlugin = handsonTableInstance.getPlugin('exportFile');
70
+ exportPlugin.downloadFile('csv', {
71
+ bom: false,
72
+ columnDelimiter: ',',
73
+ columnHeaders: false,
74
+ exportHiddenColumns: true,
75
+ exportHiddenRows: true,
76
+ fileExtension: 'csv',
77
+ filename: fileName,
78
+ mimeType: 'text/csv',
79
+ rowDelimiter: '\r\n',
80
+ rowHeaders: true
81
+ });
82
+ },
83
+
84
+ exportHandsontableData2ExcelFile(handsontableInstance, fileName) {
85
+ if (handsontableInstance === null || handsontableInstance === void 0) {
86
+ throw new IllegalArgumentError("HandsontableInstance is null for ExcelKits#exportHandsontableData2ExcelFile");
87
+ }
88
+
89
+ if (Values.isBlankString(fileName)) {
90
+ fileName = t('ui.export.excel.file') + "-" + new Date().getTime();
91
+ }
92
+
93
+ let exportGridColumns = [];
94
+ let exportGridData = handsontableInstance.hotInstance.getSourceData();
95
+
96
+ window.handsontableInstance = handsontableInstance;
97
+
98
+ if (handsontableInstance.columnSettings && handsontableInstance.columnSettings.length) {
99
+ handsontableInstance.columnSettings.forEach(column => {
100
+ exportGridColumns.push({
101
+ ...column,
102
+ dataIndex: column.data
103
+ })
104
+ })
105
+ }
106
+
107
+ console.log('exportGridColumns :', exportGridColumns)
108
+ console.log('exportGridData :', exportGridData)
109
+
110
+ this.exportTableData2ExcelFile(exportGridColumns, exportGridData, fileName);
111
+ },
112
+
113
+ exportTableData2ExcelFile(columns = [], data = [], fileName) {
114
+
115
+ if (Values.isBlankString(fileName)) {
116
+ fileName = `${t('ui.export.excel.file')}-` + new Date().getTime();
117
+ }
118
+
119
+ const workbook = new ExcelJS.Workbook();
120
+ const worksheet = workbook.addWorksheet('sheet');
121
+
122
+ let exportGridColumnsData = this.gatherExcelColumnHeaderStructureInfo(this.extractColumns(columns));
123
+ let totalColumnHeaderRowCount = exportGridColumnsData.totalColumnHeaderRowCount;
124
+ let columnHeaderData = exportGridColumnsData.columnHeaderData;
125
+ let rowHeaderArray = exportGridColumnsData.rowHeaderArray;
126
+ let columnHeaderMergeCellArray = exportGridColumnsData.columnHeaderMergeCellArray;
127
+
128
+ let exportGridData = data;
129
+
130
+ worksheet.columns = columnHeaderData;
131
+ worksheet.spliceRows(1, 1);
132
+ worksheet.insertRows(1, rowHeaderArray);
133
+
134
+ exportGridData.forEach(gridItem => {
135
+ worksheet.addRow(gridItem);
136
+ })
137
+
138
+ columnHeaderMergeCellArray.forEach(mergeCells => {
139
+ worksheet.mergeCells(mergeCells)
140
+ })
141
+
142
+ // 设置表头样式
143
+ const headerRows = worksheet.getRows(1, totalColumnHeaderRowCount);
144
+ headerRows.forEach((row, rowIndex) => {
145
+ row.eachCell((cell, cellIndex) => {
146
+ cell.style = excelHeaderRowStyle;
147
+ })
148
+ })
149
+
150
+ // 设置表体样式
151
+ const bodyRows = worksheet.getRows(totalColumnHeaderRowCount + 1, totalColumnHeaderRowCount + exportGridData.length + 5);
152
+ bodyRows.forEach((row) => {
153
+ row.eachCell((cell, cellIndex) => {
154
+ cell.style = excelBodyRowStyle;
155
+ })
156
+ })
157
+
158
+ workbook.xlsx.writeBuffer().then(buffer => {
159
+ saveAs(new Blob([buffer], {
160
+ type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
161
+ }), fileName + ".xlsx")
162
+ })
163
+ },
164
+
165
+ extractColumns(children, level) {
166
+ let _this = this;
167
+ if (level === void 0) {
168
+ level = 0;
169
+ }
170
+ return Array.isArray(children)
171
+ ? children.map(function (child) {
172
+ return _this.extractChild(child, level);
173
+ })
174
+ : [children, _this.extractChild(children, level)];
175
+ },
176
+
177
+ extractChild(child, level) {
178
+ let _this = this;
179
+ if (level === void 0) {
180
+ level = 0;
181
+ }
182
+ return Object.assign(Object.assign({}, child), {
183
+ level: level,
184
+ children: child.children && _this.extractColumns(child.children, level + 1)
185
+ })
186
+ },
187
+
188
+ /**
189
+ * 收集Excel列头信息(复合表头)
190
+ * @param gridColumns
191
+ * @returns {{totalColumnCount: {int}, totalColumnHeaderRowCount: {int}, columnHeaderArray: [] }}
192
+ */
193
+ gatherExcelColumnHeaderStructureInfo(gridColumns) {
194
+ let [totalColumnCount, totalColumnHeaderRowCount] = this.getTotalColumnCountAndTotalColumnHeaderRowCount(gridColumns, 0, 1);
195
+
196
+ let columnHeaderArray = [];
197
+ for (let i = 0, count = gridColumns.length; i < count; i++) {
198
+ let column = gridColumns[i];
199
+ this.$buildColumnHeaderArray(column, columnHeaderArray);
200
+ }
201
+
202
+ for (let i = 0, count = columnHeaderArray.length; i < count; i++) {
203
+ let subArray = columnHeaderArray[i];
204
+ let lastElem = subArray[subArray.length - 1];
205
+ for (let r = subArray.length; r < totalColumnHeaderRowCount; r++) {
206
+ subArray.push(lastElem);
207
+ }
208
+ }
209
+
210
+ let columnHeaderMergeCellArray = [];
211
+ let nextColIndex = 1;
212
+ for (let i = 0, count = gridColumns.length; i < count; i++) {
213
+ let gridColumn = gridColumns[i];
214
+ nextColIndex = this.gatherExcelColumnHeaderMergeCellInfo(gridColumn, columnHeaderMergeCellArray, totalColumnHeaderRowCount, 1, nextColIndex);
215
+ }
216
+
217
+ let rowHeaderArray = [];
218
+ for (let totalColumnHeaderRowStep = 0; totalColumnHeaderRowStep < totalColumnHeaderRowCount; totalColumnHeaderRowStep++) {
219
+ let rowHeaderColumns = [];
220
+ columnHeaderArray.forEach(columns => {
221
+ rowHeaderColumns.push(columns[totalColumnHeaderRowStep])
222
+ })
223
+ rowHeaderArray.push(rowHeaderColumns);
224
+ }
225
+
226
+ let columnHeaderData = [];
227
+ let columnIndex = 0;
228
+
229
+ let addColumns = (columns, result) => {
230
+ columns.forEach((column) => {
231
+ let exporterColumn = this.$normalizedExportColumn(column);
232
+ result.push(exporterColumn);
233
+ if (column.children && column.children.length > 1) {
234
+ let children = exporterColumn.columns = [];
235
+ addColumns(column.children, children);
236
+ } else {
237
+ columnIndex++;
238
+ }
239
+ });
240
+ };
241
+ addColumns(gridColumns, columnHeaderData);
242
+ const autoCreateArrayItems = function (column, columnStep, columns) {
243
+ if (column.columns && column.columns.length) {
244
+ columns.splice(columnStep, 1, ...column.columns);
245
+ }
246
+
247
+ if (columnStep === columns.length - 1) {
248
+ console.log('success');
249
+ } else {
250
+ columnStep++;
251
+ autoCreateArrayItems(columns[columnStep], columnStep, columns);
252
+ }
253
+ }
254
+
255
+ autoCreateArrayItems(columnHeaderData[0], 0, columnHeaderData);
256
+
257
+ return {
258
+ totalColumnCount,
259
+ totalColumnHeaderRowCount,
260
+ columnHeaderArray,
261
+ columnHeaderData,
262
+ rowHeaderArray,
263
+ columnHeaderMergeCellArray
264
+ };
265
+ },
266
+
267
+ /**
268
+ * @return {Array} eg -
269
+ * [
270
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
271
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
272
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
273
+ * [leftTopRowIndex, leftTopColumnIndex, rightBottomRowIndex, rightBottomColumnIndex],
274
+ * ]
275
+ */
276
+ gatherExcelColumnHeaderMergeCellInfo(gridColumn, columnHeaderMergeCellArray, totalColumnHeaderRowCount, currentColumnHeaderRowIndex = 1, colIndex) {
277
+
278
+ let allChildColumnCount = this.$countAllChildColumnCount(gridColumn);
279
+
280
+ if (Array.isArray(gridColumn.children) && gridColumn.children.length) {
281
+ let gridColumns = gridColumn.children;
282
+ let gridColumnCount = gridColumns.length;
283
+
284
+ columnHeaderMergeCellArray.push([currentColumnHeaderRowIndex, colIndex, currentColumnHeaderRowIndex, colIndex + allChildColumnCount - 1]);
285
+
286
+ for (let i = 0; i < gridColumnCount; i++) {
287
+ this.gatherExcelColumnHeaderMergeCellInfo(gridColumns[i], columnHeaderMergeCellArray, totalColumnHeaderRowCount,
288
+ currentColumnHeaderRowIndex + 1, colIndex + i);
289
+ }
290
+ } else {
291
+ if (currentColumnHeaderRowIndex !== totalColumnHeaderRowCount) {
292
+ columnHeaderMergeCellArray.push([currentColumnHeaderRowIndex, colIndex, totalColumnHeaderRowCount, colIndex]);
293
+ }
294
+ }
295
+
296
+ return colIndex + allChildColumnCount;
297
+ },
298
+
299
+ $countAllChildColumnCount(gridColumn) {
300
+ let columnCount = 0;
301
+ if (gridColumn === null || gridColumn === void 0) {
302
+ return columnCount;
303
+ }
304
+
305
+ if (Array.isArray(gridColumn.children) && gridColumn.children.length) {
306
+ let gridColumns = gridColumn.children;
307
+ let gridColumnCount = gridColumns.length;
308
+
309
+ for (let i = 0; i < gridColumnCount; i++) {
310
+ let childColumnCount = this.$countAllChildColumnCount(gridColumns[i]);
311
+ columnCount = columnCount + childColumnCount;
312
+ }
313
+ } else {
314
+ columnCount = 1;
315
+ }
316
+
317
+ return columnCount;
318
+ },
319
+
320
+ getTotalColumnCountAndTotalColumnHeaderRowCount(gridColumns, totalColumnCount, totalColumnHeaderCount) {
321
+ let maxN2 = totalColumnHeaderCount;
322
+ for (let i = 0, count = gridColumns.length; i < count; i++) {
323
+ let column = gridColumns[i];
324
+ if (Array.isArray(column.children) && column.children.length) {
325
+ let [n1, n2] = this.getTotalColumnCountAndTotalColumnHeaderRowCount(column.children, 0, totalColumnHeaderCount + 1);
326
+ totalColumnCount = totalColumnCount + n1;
327
+ if (n2 > maxN2) {
328
+ maxN2 = n2;
329
+ }
330
+ } else {
331
+ totalColumnCount = totalColumnCount + 1;
332
+ }
333
+ }
334
+
335
+ return [totalColumnCount, maxN2]
336
+ },
337
+
338
+ $buildColumnHeaderArray(column, columnHeaderArray, parentHeaderTitles) {
339
+ if (Array.isArray(column.children) && column.children.length) {
340
+ if (parentHeaderTitles === null || parentHeaderTitles == void 0) {
341
+ parentHeaderTitles = [column.title];
342
+ } else {
343
+ parentHeaderTitles.push(column.title);
344
+ }
345
+
346
+ for (let i = 0, count = column.children.length; i < count; i++) {
347
+ let childColumn = column.children[i];
348
+ this.$buildColumnHeaderArray(childColumn, columnHeaderArray, parentHeaderTitles);
349
+ }
350
+ } else {
351
+ let columnSelfHeaderTitleArray = [];
352
+ if (Array.isArray(parentHeaderTitles)) {
353
+ columnSelfHeaderTitleArray.push(...parentHeaderTitles);
354
+ }
355
+
356
+ columnSelfHeaderTitleArray.push(column.title);
357
+
358
+ columnHeaderArray.push(columnSelfHeaderTitleArray)
359
+ }
360
+ },
361
+
362
+
363
+ $normalizedExportColumn(column) {
364
+ return {
365
+ header : column.title,
366
+ key : column.dataIndex,
367
+ locked : column.locked,
368
+ width : column.width != null ? column.width * 0.1 : undefined
369
+ }
370
+ }
371
+ }
372
+
373
+ export default ExcelKits;