Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(sheet): handle key value conflicts (#1720)

Gpound.liu9abc7c575c68
diff --git a/packages/core/src/shared/__test__/ref-alias.spec.ts b/packages/core/src/shared/__test__/ref-alias.spec.ts
index 379fab9f86..98f36e38ec 100644
--- a/packages/core/src/shared/__test__/ref-alias.spec.ts
+++ b/packages/core/src/shared/__test__/ref-alias.spec.ts
@@ -95,4 +95,15 @@ describe('test for RefAlias', () => {
expect(newInstance.getValue('aa')).toEqual(instance.getValue('aa'));
expect(newInstance.getValues()).toEqual(instance.getValues());
});
+
+ it('test the same key and value', () => {
+ const instance = createInstance();
+ const obj = { a: 'cc', c: 'aa', b: 'b', d: 'd' };
+ instance.addValue({ a: 'cc', c: 'aa', b: 'b', d: 'd' });
+ // When the second getValue parameter is not set, the order of values is determined by the key order at construction time
+ // Retrieves from an object with a key of' A' first
+ expect(instance.getValue('cc')).toEqual(obj);
+ // When setting the second parameter of getValue, the order of values is determined by the second parameter.
+ expect(instance.getValue('cc', ['c'])).toEqual({ a: 'aa', b: 'bb', c: 'cc', d: 'dd' });
+ });
});
diff --git a/packages/core/src/shared/ref-alias.ts b/packages/core/src/shared/ref-alias.ts
index dc76ffa5d3..673a803082 100644
--- a/packages/core/src/shared/ref-alias.ts
+++ b/packages/core/src/shared/ref-alias.ts
@@ -36,9 +36,17 @@ export class RefAlias<T extends Record<string, unknown>, K extends keyof T = key
});
}
- getValue(key: string) {
- for (let index = 0; index < this._keys.length; index++) {
- const keyMap = this._keyMaps.get(this._keys[index]);
+ /**
+ * If a key group is specified, the order of values is determined by the key group, otherwise it depends on the keys at initialization
+ * @param {string} key
+ * @param {K[]} [keyGroup]
+ * @return {*}
+ * @memberof RefAlias
+ */
+ getValue(key: string, keyGroup?: K[]) {
+ const keys = keyGroup || this._keys;
+ for (let index = 0; index < keys.length; index++) {
+ const keyMap = this._keyMaps.get(keys[index]);
if (keyMap?.has(key)) {
return keyMap.get(key);
}
diff --git a/packages/sheets-conditional-formatting/src/services/conditional-formatting-formula.service.ts b/packages/sheets-conditional-formatting/src/services/conditional-formatting-formula.service.ts
index 04edc9ba58..01fe206edd 100644
--- a/packages/sheets-conditional-formatting/src/services/conditional-formatting-formula.service.ts
+++ b/packages/sheets-conditional-formatting/src/services/conditional-formatting-formula.service.ts
@@ -116,7 +116,7 @@ export class ConditionalFormattingFormulaService extends Disposable {
}
const cfIdSet = new Set<string>();
for (const formulaId in params.unitOtherData[unitId]![subUnitId]) {
- const item = map.getValue(formulaId);
+ const item = map.getValue(formulaId, ['formulaId']);
if (item) {
const value = getResultFromFormula(params.unitOtherData[unitId]![subUnitId]![formulaId]);
item.result = value!;
@@ -223,7 +223,7 @@ export class ConditionalFormattingFormulaService extends Disposable {
public registerFormula(unitId: string, subUnitId: string, cfId: string, formulaText: string) {
const subUnitFormulaMap = this._ensureSubunitFormulaMap(unitId, subUnitId);
- const formulaItem = subUnitFormulaMap.getValue(formulaText);
+ const formulaItem = subUnitFormulaMap.getValue(formulaText, ['formulaText']);
if (formulaItem) {
if (!subUnitFormulaMap.getValues().some((item) => item.cfId === cfId)) {
formulaItem.count++;
@@ -238,7 +238,7 @@ export class ConditionalFormattingFormulaService extends Disposable {
private _removeFormulaByCfId(unitId: string, subUnitId: string, cfId: string) {
const subUnitFormulaMap = this._ensureSubunitFormulaMap(unitId, subUnitId);
const reduceCount = (formulaText: string) => {
- const formulaItem = subUnitFormulaMap.getValue(formulaText);
+ const formulaItem = subUnitFormulaMap.getValue(formulaText, ['formulaText']);
if (formulaItem) {
formulaItem.count--;
if (formulaItem.count <= 0) {
@@ -259,7 +259,7 @@ export class ConditionalFormattingFormulaService extends Disposable {
if (!map) {
return { status: FormulaResultStatus.NOT_REGISTER };
}
- const item = map.getValue(formulaText);
+ const item = map.getValue(formulaText, ['formulaText']);
if (!item) {
return { status: FormulaResultStatus.NOT_REGISTER };
}
diff --git a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts
index b4412d18c3..2a222b02eb 100644
--- a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts
+++ b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts
@@ -185,7 +185,7 @@ export class TriggerCalculationController extends Disposable {
numfmtModel.forValue((row, col, numfmtValue) => {
if (numfmtValue && refMode) {
- const refValue = refMode.getValue(numfmtValue?.i);
+ const refValue = refMode.getValue(numfmtValue?.i, ['i']);
if (!refValue) return;
diff --git a/packages/sheets/src/services/numfmt/numfmt.service.ts b/packages/sheets/src/services/numfmt/numfmt.service.ts
index 04af40b2aa..e0abfbb259 100644
--- a/packages/sheets/src/services/numfmt/numfmt.service.ts
+++ b/packages/sheets/src/services/numfmt/numfmt.service.ts
@@ -186,7 +186,7 @@ export class NumfmtService extends Disposable implements INumfmtService {
const refMode = this._refAliasModel.get(unitId);
const value = _model.getValue(row, col);
if (value && refMode) {
- const refValue = refMode.getValue(value?.i);
+ const refValue = refMode.getValue(value?.i, ['i']);
if (!refValue) {
this._logService.error('[Numfmt Service]:', 'RefAliasModel is not match model');
return null;
@@ -210,7 +210,7 @@ export class NumfmtService extends Disposable implements INumfmtService {
Range.foreach(range, (row, col) => {
const oldValue = this.getValue(unitId, subUnitId, row, col, model);
if (oldValue && oldValue.pattern) {
- const oldRefPattern = refModel.getValue(oldValue.pattern);
+ const oldRefPattern = refModel.getValue(oldValue.pattern, ['pattern']);
if (oldRefPattern) {
oldRefPattern.count--;
}
@@ -232,7 +232,7 @@ export class NumfmtService extends Disposable implements INumfmtService {
this._refAliasModel.set(unitId, refModel);
}
values.forEach((value) => {
- let refPattern = refModel.getValue(value.pattern);
+ let refPattern = refModel.getValue(value.pattern, ['pattern']);
if (!refPattern) {
refPattern = {
count: 0,
@@ -248,7 +248,7 @@ export class NumfmtService extends Disposable implements INumfmtService {
if (model) {
const oldValue = this.getValue(unitId, subUnitId, row, col, model);
if (oldValue && oldValue.pattern) {
- const oldRefPattern = refModel.getValue(oldValue.pattern);
+ const oldRefPattern = refModel.getValue(oldValue.pattern, ['pattern']);
if (oldRefPattern) {
oldRefPattern.count--;
}