Skip to content

Commit 8e682bf

Browse files
committed
feat(xlsx): add cross-schema condition import and export
Signed-off-by: Dariy Miseldzhani <dariy.miseldzhani@hashgraph.com>
1 parent 69bc524 commit 8e682bf

2 files changed

Lines changed: 43 additions & 41 deletions

File tree

common/src/xlsx/json-to-xlsx.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -448,32 +448,20 @@ export class JsonToXlsx {
448448
fieldCache: Map<string, IRowField>,
449449
) {
450450
const baseFormula = JsonToXlsx.buildIfFormula(condition.ifCondition, fieldCache);
451-
452451
const thenFormula = baseFormula;
453452
const elseFormula = `NOT(${baseFormula})`;
454453

455-
if (Array.isArray(condition.thenFields)) {
456-
for (const field of condition.thenFields) {
457-
const thenField = fieldCache.get(field.name);
458-
if (!thenField) {
459-
continue;
460-
}
461-
worksheet
462-
.getCell(table.getCol(Dictionary.VISIBILITY), thenField.row)
463-
.setFormulae(thenFormula);
464-
}
465-
}
466-
if (Array.isArray(condition.elseFields)) {
467-
for (const field of condition.elseFields) {
468-
const elseField = fieldCache.get(field.name);
469-
if (!elseField) {
470-
continue;
471-
}
472-
worksheet
473-
.getCell(table.getCol(Dictionary.VISIBILITY), elseField.row)
474-
.setFormulae(elseFormula);
454+
const writeCell = (key: string, formula: string) => {
455+
const rowField = fieldCache.get(key);
456+
if (rowField) {
457+
worksheet.getCell(table.getCol(Dictionary.VISIBILITY), rowField.row).setFormulae(formula);
475458
}
476-
}
459+
};
460+
461+
for (const field of condition.thenFields || []) { writeCell(field.name, thenFormula); }
462+
for (const field of condition.elseFields || []) { writeCell(field.name, elseFormula); }
463+
for (const t of condition.thenTargets || []) { writeCell(t.fieldPath.join('.'), thenFormula); }
464+
for (const t of condition.elseTargets || []) { writeCell(t.fieldPath.join('.'), elseFormula); }
477465
}
478466

479467
public static writeSubFields(

common/src/xlsx/xlsx-to-json.ts

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,6 @@ export class XlsxToJson {
487487
conditionCache.push(condition);
488488
}
489489
}
490-
491490
row = table.end.r + 1;
492491
const expressions: XlsxExpressions = new XlsxExpressions();
493492
for (; row < range.e.r; row++) {
@@ -878,15 +877,13 @@ export class XlsxToJson {
878877
if (worksheet.empty(table.start.c, table.end.c, row)) {
879878
return null;
880879
}
881-
if (worksheet.getRow(row).getOutline()) {
882-
return null;
883-
}
884880

885881
const key = XlsxToJson.getFieldKey(worksheet, table, row, xlsxResult);
886-
const field = fields.find((f) => f.title === key.path);
882+
const field = allFields.get(key.path) || fields.find((f) => f.title === key.path);
883+
const targetPath = fieldPaths.get(key.path);
884+
const isNested = targetPath && targetPath.length > 1;
887885

888886
try {
889-
//visibility
890887
if (worksheet.outColumnRange(table.getCol(Dictionary.VISIBILITY))) {
891888
return;
892889
}
@@ -922,41 +919,49 @@ export class XlsxToJson {
922919
}
923920

924921
if (result.type === 'const') {
925-
field.hidden = field.hidden || !result.value;
922+
if (field) { field.hidden = field.hidden || !result.value; }
926923
return;
927924
}
928925

926+
const addToCondition = (holder: XlsxSchemaConditions, invert: boolean) => {
927+
if (isNested && field) {
928+
holder.addTarget(field, targetPath, invert);
929+
} else if (field) {
930+
holder.addField(field, invert);
931+
}
932+
};
933+
929934
if (result.op && Array.isArray(result.items)) {
930935
const resolved = result.items.map(it => {
931-
const target = allFields.get(it.fieldPath) || fields.find(f => f.title === it.fieldPath);
932-
if (!target) {
936+
const trigger = allFields.get(it.fieldPath) || fields.find(f => f.title === it.fieldPath);
937+
if (!trigger) {
933938
throw new Error(`Invalid target in ${result.op} condition: ${it.fieldPath}`);
934939
}
935-
return { field: target, value: it.compareValue, fieldPath: fieldPaths.get(it.fieldPath) };
940+
return { field: trigger, value: it.compareValue, fieldPath: fieldPaths.get(it.fieldPath) };
936941
});
937942

938943
const conditionKey = { op: result.op, items: resolved };
939944
const existed = conditionCache.find(c => (c as any).equal(conditionKey));
940945
const holder = existed || new XlsxSchemaConditions(conditionKey as any);
941946

942-
holder.addField(field, !!result.invert);
947+
addToCondition(holder, !!result.invert);
943948
if (!existed) {
944949
return holder;
945950
}
946951
return null;
947952
} else {
948-
const target = allFields.get(result.fieldPath) || fields.find((f) => f.title === result.fieldPath);
949-
if (!target) {
950-
throw new Error('Invalid target');
953+
const trigger = allFields.get(result.fieldPath) || fields.find((f) => f.title === result.fieldPath);
954+
if (!trigger) {
955+
throw new Error('Invalid trigger field');
951956
}
952-
const fieldPath = fieldPaths.get(result.fieldPath);
953-
const condition = conditionCache.find(c => c.equal(target, result.compareValue));
957+
const triggerPath = fieldPaths.get(result.fieldPath);
958+
const condition = conditionCache.find(c => c.equal(trigger, result.compareValue));
954959
if (condition) {
955-
condition.addField(field, result.invert);
960+
addToCondition(condition, result.invert);
956961
return null;
957962
} else {
958-
const newCondition = new XlsxSchemaConditions(target, result.compareValue, fieldPath);
959-
newCondition.addField(field, result.invert);
963+
const newCondition = new XlsxSchemaConditions(trigger, result.compareValue, triggerPath);
964+
addToCondition(newCondition, result.invert);
960965
return newCondition;
961966
}
962967
}
@@ -1098,6 +1103,15 @@ export class XlsxToJson {
10981103
}
10991104
}
11001105

1106+
if ((node as any).type === 'AssignmentNode') {
1107+
const assign = node as any;
1108+
const obj = assign.object;
1109+
const val = assign.value;
1110+
if (obj?.type === 'SymbolNode' && val?.type === 'ConstantNode') {
1111+
return { type: 'formulae', fieldPath: obj.name, compareValue: val.value, invert };
1112+
}
1113+
}
1114+
11011115
throw new Error(`Failed to parse formulae: ${formulae}.`);
11021116
};
11031117

0 commit comments

Comments
 (0)