fix: InputExcel表头包含RichText时解析错误问题 (#5441)

This commit is contained in:
RUNZE LU 2022-09-26 14:18:41 +08:00 committed by GitHub
parent 3b0d9a7a87
commit b4b39d04f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,8 +1,9 @@
import React, {Suspense} from 'react';
import React from 'react';
import Dropzone from 'react-dropzone';
import {autobind, createObject} from 'amis-core';
import {FormItem, FormControlProps, FormBaseControl} from 'amis-core';
import {autobind, createObject, isObject} from 'amis-core';
import {FormItem, FormControlProps} from 'amis-core';
import {FormBaseControlSchema} from '../../Schema';
import type {CellValue, CellRichTextValue} from 'exceljs';
/**
* Excel
@ -115,6 +116,69 @@ export default class ExcelControl extends React.PureComponent<
})
);
}
/**
*
*
* @reference https://github.com/exceljs/exceljs#rich-text
*/
isRichTextValue(value: any) {
return !!(
value &&
isObject(value) &&
value.hasOwnProperty('richText') &&
Array.isArray(value?.richText)
);
}
/**
* Plain Text
*
* @param {CellRichTextValue} cellValue
* @param {Boolean} html html格式
*/
richText2PlainString(cellValue: CellRichTextValue, html = false) {
const result = cellValue.richText.map(({text, font = {}}) => {
let outputStr = text;
/* 如果以HTML格式输出简单处理一下样式 */
if (html) {
let styles = '';
const htmlTag = font?.bold
? 'strong'
: font?.italic
? 'em'
: font?.vertAlign === 'superscript'
? 'sup'
: font?.vertAlign === 'subscript'
? 'sub'
: 'span';
if (font?.strike) {
styles += 'text-decoration: line-through;';
} else if (font?.underline) {
styles += 'text-decoration: underline;';
}
if (font?.outline) {
styles += 'outline: solid;';
}
if (font?.size) {
styles += `font-size: ${font.size}px;`;
}
outputStr = `<${htmlTag} ${
styles ? `style=${styles}` : ''
}>${text}</${htmlTag}>`;
}
return outputStr;
});
return result.join('');
}
/**
* sheet
*/
@ -134,7 +198,11 @@ export default class ExcelControl extends React.PureComponent<
worksheet.eachRow((row: any, rowNumber: number) => {
// 将第一列作为字段名
if (rowNumber == 1) {
firstRowValues = row.values;
firstRowValues = (row.values ?? []).map((item: CellValue) =>
this.isRichTextValue(item)
? this.richText2PlainString(item as CellRichTextValue)
: item
);
} else {
const data: any = {};
if (includeEmpty) {