From 10341ed46aaff653ea45e2770e45710e8db941f0 Mon Sep 17 00:00:00 2001 From: Geequlim Date: Mon, 25 Jan 2021 18:15:11 +0800 Subject: [PATCH] =?UTF-8?q?=E8=83=BD=E5=A4=9F=E6=8E=A8=E6=96=AD=E5=87=BAst?= =?UTF-8?q?ruct=E6=95=B0=E7=BB=84=E6=88=90=E5=91=98=E7=9A=84=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=EF=BC=8C=E6=9A=82=E6=97=B6=E5=8F=AA=E6=94=AF=E6=8C=81?= =?UTF-8?q?1=E5=B1=82=E6=B7=B1=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/excel-exporter/TableParser.ts | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/src/excel-exporter/TableParser.ts b/src/excel-exporter/TableParser.ts index 12e6b55..7a49c6a 100644 --- a/src/excel-exporter/TableParser.ts +++ b/src/excel-exporter/TableParser.ts @@ -106,26 +106,42 @@ export class Field { if (this.children) { let named_fields: {[key: string]: Field[]} = {}; for (const c of this.children) { - c.constant_array_length = this.constant_array_length; - c.build(); let arr = named_fields[c.name] || []; arr.push(c); named_fields[c.name] = arr; } for (const [name, fields] of Object.entries(named_fields)) { - if (fields.length > 1) { - let indeies = fields.map(f => Field.TYPE_ORDER.indexOf(f.type)); - indeies = indeies.filter(idx => idx >= 0); - const type = Field.TYPE_ORDER[Math.min(...indeies)]; + if (fields.length > 1) { // 推断数组类型 + const type = this.infer_field_type(fields); for (const f of fields) { f._is_array = true; f.type = type; + if (type === DataType.struct) { // 数组元素的类型 + for (const keys of fields.map( f => f.children.map(cf => cf.name) )) { + for (const key of keys) { + let same_name_fileds = fields.map( f => f.children.find( cf => cf.name === key)); + const ctype = this.infer_field_type(same_name_fileds); + same_name_fileds.forEach( f => { f.type = ctype }); + } + } + } } } } + for (const c of this.children) { + c.constant_array_length = this.constant_array_length; + c.build(); + } } } + protected infer_field_type(fields: Field[]) { + let indeies = fields.map(f => Field.TYPE_ORDER.indexOf(f.type)); + indeies = indeies.filter(idx => idx >= 0); + const type = Field.TYPE_ORDER[Math.min(...indeies)]; + return type; + } + /** 解析一条数据 */ public parse_row(row: RawTableCell[]) { if (this.type != DataType.struct) {