fix: 修复 export-excel 中 mapping 不支持 array 格式 map 问题 Closes #9136 (#9141)

This commit is contained in:
吴多益 2023-12-25 11:00:11 +08:00 committed by GitHub
parent 068c4240d1
commit 71c3324170
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,7 @@
* Excel
*/
import {filter, isEffectiveApi, arraySlice} from 'amis-core';
import {filter, isEffectiveApi, arraySlice, isObject} from 'amis-core';
import './ColumnToggler';
import {TableStore} from 'amis-core';
import {saveAs} from 'file-saver';
@ -425,6 +425,7 @@ export async function exportExcel(
}
} else if (type == 'link' || (type as any) === 'static-link') {
const href = column.pristine.href;
const linkURL =
(typeof href === 'string' && href
? filter(href, rowData, '| raw')
@ -443,6 +444,8 @@ export async function exportExcel(
};
} else if (type === 'mapping' || (type as any) === 'static-mapping') {
let map = column.pristine.map;
const valueField = column.pristine.valueField || 'value';
const labelField = column.pristine.labelField || 'label';
const source = column.pristine.source;
if (source) {
let sourceValue = source;
@ -462,6 +465,29 @@ export async function exportExcel(
}
}
if (Array.isArray(map)) {
map = map.reduce((res, now) => {
if (now == null) {
return res;
} else if (isObject(now)) {
let keys = Object.keys(now);
if (
keys.length === 1 ||
(keys.length == 2 && keys.includes('$$id'))
) {
// 针对amis-editor的特殊处理
keys = keys.filter(key => key !== '$$id');
// 单key 数组对象
res[keys[0]] = now[keys[0]];
} else if (keys.length > 1) {
// 多key 数组对象
res[now[valueField]] = now;
}
}
return res;
}, {});
}
if (typeof value !== 'undefined' && map && (map[value] ?? map['*'])) {
const viewValue =
map[value] ??
@ -470,7 +496,23 @@ export async function exportExcel(
: value === false && map['0']
? map['0']
: map['*']); // 兼容平台旧用法:即 value 为 true 时映射 1 ,为 false 时映射 0
let text = removeHTMLTag(viewValue);
let label = viewValue;
if (isObject(viewValue)) {
if (labelField === undefined || labelField === '') {
if (!viewValue.hasOwnProperty('type')) {
// 映射值是object
// 没配置labelField
// object 也没有 type不能作为schema渲染
// 默认取 label 字段
label = viewValue['label'];
}
} else {
label = viewValue[labelField || 'label'];
}
}
let text = removeHTMLTag(label);
/** map可能会使用比较复杂的html结构富文本也无法完全支持直接把里面的变量解析出来即可 */
if (isPureVariable(text)) {