* 读取工作表内容
(worksheet: any, plainText = true)
| 705 | * 读取工作表内容 |
| 706 | */ |
| 707 | readWorksheet(worksheet: any, plainText = true) { |
| 708 | const result: any[] = []; |
| 709 | const {parseMode, includeEmpty} = this.props; |
| 710 | |
| 711 | if (parseMode === 'array') { |
| 712 | worksheet.eachRow((_row: any) => { |
| 713 | let values = _row.values; |
| 714 | values.shift(); // excel 返回的值是从 1 开始的,0 节点永远是 null |
| 715 | if (plainText) { |
| 716 | values = values.map((item: any) => { |
| 717 | if (item instanceof Object) { |
| 718 | if (item.hyperlink) { |
| 719 | if (item.hyperlink.startsWith('mailto:')) { |
| 720 | return item.hyperlink.substring(7); |
| 721 | } |
| 722 | return item.hyperlink; |
| 723 | } else if (item.result) { |
| 724 | return item.result; |
| 725 | } else if (item.richText) { |
| 726 | return this.richText2PlainString(item); |
| 727 | } |
| 728 | } |
| 729 | return item; |
| 730 | }); |
| 731 | } |
| 732 | result.push(values); |
| 733 | }); |
| 734 | return result; |
| 735 | } else { |
| 736 | let firstRowValues: any[] = []; |
| 737 | worksheet.eachRow((row: any, rowNumber: number) => { |
| 738 | // 将第一列作为字段名 |
| 739 | if (rowNumber == 1) { |
| 740 | firstRowValues = (row.values ?? []).map((item: CellValue) => |
| 741 | this.isRichTextValue(item) |
| 742 | ? this.richText2PlainString(item as CellRichTextValue) |
| 743 | : item |
| 744 | ); |
| 745 | } else { |
| 746 | const data: any = {}; |
| 747 | if (includeEmpty) { |
| 748 | firstRowValues.forEach((item: any) => { |
| 749 | data[item] = ''; |
| 750 | }); |
| 751 | } |
| 752 | row.eachCell((cell: any, colNumber: any) => { |
| 753 | if (firstRowValues[colNumber]) { |
| 754 | let value = cell.value; |
| 755 | if (plainText) { |
| 756 | const ExcelValueType = this.ExcelJS.ValueType; |
| 757 | if (cell.type === ExcelValueType.Hyperlink) { |
| 758 | value = cell.value.hyperlink; |
| 759 | if (value.startsWith('mailto:')) { |
| 760 | value = value.substring(7); |
| 761 | } |
| 762 | } else if (cell.type === ExcelValueType.Formula) { |
| 763 | value = cell.value.result; |
| 764 | } else if (cell.type === ExcelValueType.RichText) { |
no test coverage detected