bl-common-vue3 3.8.119 → 3.8.121
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/package.json
CHANGED
package/src/common/utils/xlsx.js
CHANGED
|
@@ -5,6 +5,7 @@ const DEFAULT_COLUMN_WIDTH = 16;
|
|
|
5
5
|
const MAX_COLUMN_WIDTH = 36;
|
|
6
6
|
const HEADER_ROW_HEIGHT = 30;
|
|
7
7
|
const DATA_ROW_HEIGHT = 24;
|
|
8
|
+
const DEFAULT_BODY_FONT_COLOR = "FF333333";
|
|
8
9
|
const XLSX_MIME_TYPE =
|
|
9
10
|
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8";
|
|
10
11
|
const INVALID_FILE_NAME_REG = /[\\/:*?"<>|]/g;
|
|
@@ -68,6 +69,8 @@ const getStyledExportOptions = (options = {}) => {
|
|
|
68
69
|
detailFields = [],
|
|
69
70
|
fieldAlignments = {},
|
|
70
71
|
columnWidths = {},
|
|
72
|
+
fieldStyles = {},
|
|
73
|
+
fieldValueColors = {},
|
|
71
74
|
} = options;
|
|
72
75
|
|
|
73
76
|
return {
|
|
@@ -79,9 +82,78 @@ const getStyledExportOptions = (options = {}) => {
|
|
|
79
82
|
: {},
|
|
80
83
|
columnWidths:
|
|
81
84
|
columnWidths && typeof columnWidths === "object" ? columnWidths : {},
|
|
85
|
+
fieldStyles:
|
|
86
|
+
fieldStyles && typeof fieldStyles === "object" ? fieldStyles : {},
|
|
87
|
+
fieldValueColors:
|
|
88
|
+
fieldValueColors && typeof fieldValueColors === "object"
|
|
89
|
+
? fieldValueColors
|
|
90
|
+
: {},
|
|
82
91
|
};
|
|
83
92
|
};
|
|
84
93
|
|
|
94
|
+
const normalizeArgbColor = (color, fallbackColor = DEFAULT_BODY_FONT_COLOR) => {
|
|
95
|
+
const normalizedColor = String(color || "")
|
|
96
|
+
.replace(/^#/, "")
|
|
97
|
+
.toUpperCase();
|
|
98
|
+
if (/^[0-9A-F]{8}$/.test(normalizedColor)) {
|
|
99
|
+
return normalizedColor;
|
|
100
|
+
}
|
|
101
|
+
if (/^[0-9A-F]{6}$/.test(normalizedColor)) {
|
|
102
|
+
return `FF${normalizedColor}`;
|
|
103
|
+
}
|
|
104
|
+
return fallbackColor;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const getFieldValueFontColor = (
|
|
108
|
+
item,
|
|
109
|
+
field,
|
|
110
|
+
fieldValueColors,
|
|
111
|
+
defaultColor
|
|
112
|
+
) => {
|
|
113
|
+
const colorConfig = fieldValueColors[field];
|
|
114
|
+
if (!colorConfig || typeof colorConfig !== "object") {
|
|
115
|
+
return defaultColor;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
const sourceField = colorConfig.sourceField || field;
|
|
119
|
+
const valueColors = colorConfig.colors;
|
|
120
|
+
if (!valueColors || typeof valueColors !== "object") {
|
|
121
|
+
return defaultColor;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return normalizeArgbColor(
|
|
125
|
+
valueColors[String(item?.[sourceField] ?? "")],
|
|
126
|
+
defaultColor
|
|
127
|
+
);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const getFieldStyle = (field, isDetailField, fieldStyles) => {
|
|
131
|
+
const defaultStyle = isDetailField
|
|
132
|
+
? {
|
|
133
|
+
headerFontColor: "FF1F4E78",
|
|
134
|
+
headerFillColor: "FFDCE6F1",
|
|
135
|
+
bodyFontColor: DEFAULT_BODY_FONT_COLOR,
|
|
136
|
+
bodyFillColor: "FFF7FAFD",
|
|
137
|
+
}
|
|
138
|
+
: {
|
|
139
|
+
headerFontColor: "FFFFFFFF",
|
|
140
|
+
headerFillColor: "FF4472C4",
|
|
141
|
+
bodyFontColor: DEFAULT_BODY_FONT_COLOR,
|
|
142
|
+
bodyFillColor: "FFFFFFFF",
|
|
143
|
+
};
|
|
144
|
+
const configuredStyle = fieldStyles[field];
|
|
145
|
+
if (!configuredStyle || typeof configuredStyle !== "object") {
|
|
146
|
+
return defaultStyle;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return Object.fromEntries(
|
|
150
|
+
Object.entries(defaultStyle).map(([key, defaultColor]) => [
|
|
151
|
+
key,
|
|
152
|
+
normalizeArgbColor(configuredStyle[key], defaultColor),
|
|
153
|
+
])
|
|
154
|
+
);
|
|
155
|
+
};
|
|
156
|
+
|
|
85
157
|
export function getVerticalMergeCells(json, fields, options = {}) {
|
|
86
158
|
const { groupField, mergeFields = [] } = options;
|
|
87
159
|
if (!groupField || !Array.isArray(mergeFields) || !mergeFields.length) {
|
|
@@ -160,18 +232,23 @@ const applyStyledExport = (XLSX, ws, json, fields, options) => {
|
|
|
160
232
|
|
|
161
233
|
fieldKeys.forEach((field, columnIndex) => {
|
|
162
234
|
const headerCell = ws[XLSX.utils.encode_cell({ r: 0, c: columnIndex })];
|
|
235
|
+
const isDetailField = detailFieldSet.has(field);
|
|
236
|
+
const fieldStyle = getFieldStyle(
|
|
237
|
+
field,
|
|
238
|
+
isDetailField,
|
|
239
|
+
options.fieldStyles
|
|
240
|
+
);
|
|
163
241
|
if (headerCell) {
|
|
164
|
-
const isDetailField = detailFieldSet.has(field);
|
|
165
242
|
headerCell.s = {
|
|
166
243
|
font: {
|
|
167
244
|
name: "Microsoft YaHei",
|
|
168
245
|
sz: 11,
|
|
169
246
|
bold: true,
|
|
170
|
-
color: { rgb:
|
|
247
|
+
color: { rgb: fieldStyle.headerFontColor },
|
|
171
248
|
},
|
|
172
249
|
fill: {
|
|
173
250
|
patternType: "solid",
|
|
174
|
-
fgColor: { rgb:
|
|
251
|
+
fgColor: { rgb: fieldStyle.headerFillColor },
|
|
175
252
|
},
|
|
176
253
|
alignment: {
|
|
177
254
|
horizontal: "center",
|
|
@@ -190,16 +267,21 @@ const applyStyledExport = (XLSX, ws, json, fields, options) => {
|
|
|
190
267
|
return;
|
|
191
268
|
}
|
|
192
269
|
|
|
193
|
-
const
|
|
270
|
+
const fontColor = getFieldValueFontColor(
|
|
271
|
+
item,
|
|
272
|
+
field,
|
|
273
|
+
options.fieldValueColors,
|
|
274
|
+
fieldStyle.bodyFontColor
|
|
275
|
+
);
|
|
194
276
|
cell.s = {
|
|
195
277
|
font: {
|
|
196
278
|
name: "Microsoft YaHei",
|
|
197
279
|
sz: 10,
|
|
198
|
-
color: { rgb:
|
|
280
|
+
color: { rgb: fontColor },
|
|
199
281
|
},
|
|
200
282
|
fill: {
|
|
201
283
|
patternType: "solid",
|
|
202
|
-
fgColor: { rgb:
|
|
284
|
+
fgColor: { rgb: fieldStyle.bodyFillColor },
|
|
203
285
|
},
|
|
204
286
|
alignment: {
|
|
205
287
|
horizontal: options.fieldAlignments[field] || "left",
|
|
@@ -234,18 +316,14 @@ export async function createWorkbook(
|
|
|
234
316
|
const sourceJson = styledExportOptions.enabled
|
|
235
317
|
? json.map((item) => ({ ...item }))
|
|
236
318
|
: null;
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
}
|
|
244
|
-
delete item[i]; //删除原先的对象属性
|
|
245
|
-
}
|
|
246
|
-
});
|
|
319
|
+
const fieldKeys = Object.keys(fields);
|
|
320
|
+
// 字段 key 必须保留到取值阶段;不同字段允许使用相同表头,不能用表头文案作为对象 key,否则后写入的同名列会丢失。
|
|
321
|
+
const sheetRows = [
|
|
322
|
+
Object.values(fields),
|
|
323
|
+
...json.map((item) => fieldKeys.map((field) => item?.[field])),
|
|
324
|
+
];
|
|
247
325
|
let wb = XLSX.utils.book_new(); //工作簿对象包含一SheetNames数组,以及一个表对象映射表名称到表对象。XLSX.utils.book_new实用函数创建一个新的工作簿对象。
|
|
248
|
-
let ws = XLSX.utils.
|
|
326
|
+
let ws = XLSX.utils.aoa_to_sheet(sheetRows); //按列顺序写入,允许不同字段展示相同的表头文案。
|
|
249
327
|
if (merges.length) {
|
|
250
328
|
ws["!merges"] = merges;
|
|
251
329
|
}
|
|
@@ -1345,6 +1345,8 @@
|
|
|
1345
1345
|
:params="listParams"
|
|
1346
1346
|
:limit="exportLimit"
|
|
1347
1347
|
:columnstitle="exportColumnsTitle"
|
|
1348
|
+
:merge_options="exportMergeOptions"
|
|
1349
|
+
:style_options="exportStyleOptions"
|
|
1348
1350
|
:custom_result="exportResultData"
|
|
1349
1351
|
:custom_data="dictionaryFilling"
|
|
1350
1352
|
@customDataFunc="handleExportData"
|
|
@@ -1577,6 +1579,16 @@ export default defineComponent({
|
|
|
1577
1579
|
type: Number,
|
|
1578
1580
|
default: 500,
|
|
1579
1581
|
},
|
|
1582
|
+
/* 导出表格纵向合并配置 */
|
|
1583
|
+
exportMergeOptions: {
|
|
1584
|
+
type: Object,
|
|
1585
|
+
default: () => ({}),
|
|
1586
|
+
},
|
|
1587
|
+
/* 导出表格样式配置 */
|
|
1588
|
+
exportStyleOptions: {
|
|
1589
|
+
type: Object,
|
|
1590
|
+
default: () => ({}),
|
|
1591
|
+
},
|
|
1580
1592
|
/* 自定义导出的方法 */
|
|
1581
1593
|
customImport: {
|
|
1582
1594
|
type: [Function, undefined],
|