mirror of
https://gitee.com/baidu/amis.git
synced 2024-11-30 02:58:05 +08:00
chore: input-excel 默认解析为纯文本格式 (#2773)
This commit is contained in:
parent
65a798ea65
commit
dec79820ab
@ -160,6 +160,61 @@ order: 14
|
||||
]
|
||||
```
|
||||
|
||||
## 富文本模式
|
||||
|
||||
默认情况下 Excel 内容将会解析为纯文本,如果要使用富文本格式,可以通过 `plainText` 属性控制
|
||||
|
||||
```schema: scope="body"
|
||||
{
|
||||
"type": "form",
|
||||
"api": "/api/mock2/form/saveForm",
|
||||
"debug": true,
|
||||
"body": [
|
||||
{
|
||||
"type": "input-excel",
|
||||
"name": "excel",
|
||||
"plainText": false,
|
||||
"label": "上传 Excel"
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
开启这个模式后,对于富文本的内容会解析成对象的形式,有以下几种
|
||||
|
||||
- 富文本,内容放在 richText 属性下
|
||||
|
||||
```
|
||||
{
|
||||
"richText": [
|
||||
{text: 'This is '},
|
||||
{font: {italic: true}, text: 'italic'}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
- 出错
|
||||
|
||||
```
|
||||
{ error: '#N/A' }
|
||||
```
|
||||
|
||||
- 公式
|
||||
|
||||
```
|
||||
{ formula: 'A1+A2', result: 7 };
|
||||
```
|
||||
|
||||
- 超链接
|
||||
|
||||
```
|
||||
{
|
||||
text: 'www.mylink.com',
|
||||
hyperlink: 'http://www.mylink.com',
|
||||
tooltip: 'www.mylink.com'
|
||||
}
|
||||
```
|
||||
|
||||
## 属性表
|
||||
|
||||
| 属性名 | 类型 | 默认值 | 说明 |
|
||||
@ -167,3 +222,4 @@ order: 14
|
||||
| allSheets | `boolean` | false | 是否解析所有 sheet |
|
||||
| parseMode | `'array'` 或 `'object'` | 'object' | 解析模式 |
|
||||
| includeEmpty | `boolean` | true | 是否包含空值 |
|
||||
| plainText | `boolean` | true | 是否解析为纯文本 |
|
||||
|
@ -28,6 +28,11 @@ export interface InputExcelControlSchema extends FormBaseControl {
|
||||
* 是否包含空内容,主要用于二维数组模式
|
||||
*/
|
||||
includeEmpty: boolean;
|
||||
|
||||
/**
|
||||
* 纯文本模式
|
||||
*/
|
||||
plainText: boolean;
|
||||
}
|
||||
|
||||
export interface ExcelProps
|
||||
@ -48,12 +53,15 @@ export default class ExcelControl extends React.PureComponent<
|
||||
static defaultProps: Partial<ExcelProps> = {
|
||||
allSheets: false,
|
||||
parseMode: 'object',
|
||||
includeEmpty: true
|
||||
includeEmpty: true,
|
||||
plainText: true
|
||||
};
|
||||
state: ExcelControlState = {
|
||||
open: false
|
||||
};
|
||||
|
||||
ExcelJS: any;
|
||||
|
||||
@autobind
|
||||
handleDrop(files: File[]) {
|
||||
const {allSheets, onChange} = this.props;
|
||||
@ -63,6 +71,7 @@ export default class ExcelControl extends React.PureComponent<
|
||||
reader.onload = async () => {
|
||||
if (reader.result) {
|
||||
import('exceljs').then(async (ExcelJS: any) => {
|
||||
this.ExcelJS = ExcelJS;
|
||||
const workbook = new ExcelJS.Workbook();
|
||||
await workbook.xlsx.load(reader.result);
|
||||
if (allSheets) {
|
||||
@ -88,7 +97,7 @@ export default class ExcelControl extends React.PureComponent<
|
||||
*/
|
||||
readWorksheet(worksheet: any) {
|
||||
const result: any[] = [];
|
||||
const {parseMode} = this.props;
|
||||
const {parseMode, plainText} = this.props;
|
||||
|
||||
if (parseMode === 'array') {
|
||||
worksheet.eachRow((row: any, rowNumber: number) => {
|
||||
@ -107,7 +116,23 @@ export default class ExcelControl extends React.PureComponent<
|
||||
const data: any = {};
|
||||
row.eachCell((cell: any, colNumber: any) => {
|
||||
if (firstRowValues[colNumber]) {
|
||||
data[firstRowValues[colNumber]] = cell.value;
|
||||
let value = cell.value;
|
||||
if (plainText) {
|
||||
const ExcelValueType = this.ExcelJS.ValueType;
|
||||
if (cell.type === ExcelValueType.Hyperlink) {
|
||||
value = cell.value.hyperlink;
|
||||
} else if (cell.type === ExcelValueType.Formula) {
|
||||
value = cell.value.result;
|
||||
} else if (cell.type === ExcelValueType.RichText) {
|
||||
value = cell.value.richText
|
||||
.map((item: any) => item.text)
|
||||
.join('');
|
||||
} else if (cell.type === ExcelValueType.Error) {
|
||||
value = '';
|
||||
}
|
||||
}
|
||||
|
||||
data[firstRowValues[colNumber]] = value;
|
||||
}
|
||||
});
|
||||
result.push(data);
|
||||
|
Loading…
Reference in New Issue
Block a user