Skip to content

Commit

Permalink
fix: 修复 export-excel 中 mapping 不支持 array 格式 map 问题 Closes baidu#9136 (b…
Browse files Browse the repository at this point in the history
  • Loading branch information
吴多益 authored Dec 25, 2023
1 parent 068c424 commit 71c3324
Showing 1 changed file with 44 additions and 2 deletions.
46 changes: 44 additions & 2 deletions packages/amis/src/renderers/Table/exportExcel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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')
Expand All @@ -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;
Expand All @@ -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] ??
Expand All @@ -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)) {
Expand Down

0 comments on commit 71c3324

Please sign in to comment.