From b6608603892a2d051ea5d48c3e7a701897fcf38a Mon Sep 17 00:00:00 2001 From: Geequlim Date: Sat, 23 Jan 2021 22:34:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E7=BB=84=E9=95=BF=E5=BA=A6=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E5=A1=AB=E8=A1=A8=E5=86=85=E5=AE=B9=E5=8F=98=E5=8C=96?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/excel-exporter/TableParser.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/excel-exporter/TableParser.ts b/src/excel-exporter/TableParser.ts index 22d2257..5873dd7 100644 --- a/src/excel-exporter/TableParser.ts +++ b/src/excel-exporter/TableParser.ts @@ -41,6 +41,8 @@ export class Field { children: ReadonlyArray; /** 数据类型 */ type?: DataType; + /** 保持数组长度和配置表中的列数量一致,没填的数据使用 null 填充 */ + constant_array_length?: boolean; /** 添加子字段 */ add_field(field: Field) { @@ -95,6 +97,7 @@ 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); @@ -102,7 +105,9 @@ export class Field { } for (const [name, fields] of Object.entries(named_fields)) { if (fields.length > 1) { - const type = Field.TYPE_ORDER[Math.min(...(fields.map(f => Field.TYPE_ORDER.indexOf(f.type))))]; + 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)]; for (const f of fields) { f._is_array = true; f.type = type; @@ -118,17 +123,21 @@ export class Field { return this.get_cell_value(row[this.columns.start], this.type); } else if (this.children && this.children.length) { let obj = {}; + let isAllNullish = true; for (const c of this.children) { let value = c.parse_row(row); if (c.is_array) { let arr: any[] = obj[c.name] || []; - arr.push(value); + if (this.constant_array_length || value != null) { + arr.push(value); + } obj[c.name] = arr; } else { obj[c.name] = value; } + isAllNullish = isAllNullish && value == null; } - return obj; + return isAllNullish ? null : obj; } } @@ -317,6 +326,7 @@ export class TableParser { } field.type = type; } + root.constant_array_length = this.configs.constant_array_length && this.configs.constant_array_length.includes(root.name); root.build(); return { struct: root,