Skip to content

Commit f7423b7

Browse files
committed
fix: add params range
1 parent 65ef049 commit f7423b7

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

packages/sheets-formula/src/facade/f-formula.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
import type { IDisposable, ILocales } from '@univerjs/core';
17+
import type { IDisposable, ILocales, IUnitRange } from '@univerjs/core';
1818

1919
import type { IFunctionInfo } from '@univerjs/engine-formula';
2020
import type { CalculationMode, IFormulaCellAndFeatureItem, IRegisterAsyncFunction, IRegisterFunction, ISingleFunctionRegisterParams, IUniverSheetsFormulaBaseConfig } from '@univerjs/sheets-formula';
@@ -41,6 +41,7 @@ export interface IFFormulaSheetsMixin {
4141

4242
/**
4343
* Get the list of formula cells and feature IDs from the dependency tree.
44+
* @param {IUnitRange} [range] - Optional range to filter the results.
4445
* @returns {Promise<Array<IFormulaCellAndFeatureItem>>} An array of objects containing unitId, subUnitId, and either cell coordinates or featureId.
4546
*
4647
* @example
@@ -50,7 +51,7 @@ export interface IFFormulaSheetsMixin {
5051
* console.log(cellsAndFeatures);
5152
* ```
5253
*/
53-
getFormulaCellsAndFeatures(): Promise<Array<IFormulaCellAndFeatureItem>>;
54+
getFormulaCellsAndFeatures(range?: IUnitRange): Promise<Array<IFormulaCellAndFeatureItem>>;
5455

5556
/**
5657
* Register a custom synchronous formula function.
@@ -332,9 +333,9 @@ export class FFormulaSheetsMixin extends FFormula implements IFFormulaSheetsMixi
332333
config.initialFormulaComputing = calculationMode;
333334
}
334335

335-
override async getFormulaCellsAndFeatures(): Promise<Array<IFormulaCellAndFeatureItem>> {
336+
override async getFormulaCellsAndFeatures(range?: IUnitRange): Promise<Array<IFormulaCellAndFeatureItem>> {
336337
const dependencyGenerator = this._injector.get(IRemoteFormulaDependencyGenerator);
337-
return dependencyGenerator.generate();
338+
return dependencyGenerator.generate(range);
338339
}
339340

340341
override registerFunction(name: string, func: IRegisterFunction): IDisposable;

packages/sheets-formula/src/services/remote/remote-formula-dependency-generator.service.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import type { IUnitRange } from '@univerjs/core';
1718
import type { FormulaCurrentConfigService } from '@univerjs/engine-formula';
1819
import { createIdentifier, isFormulaId, isFormulaString } from '@univerjs/core';
1920
import { IFormulaCurrentConfigService, IFormulaDependencyGenerator } from '@univerjs/engine-formula';
@@ -29,7 +30,7 @@ export interface IFormulaCellAndFeatureItem {
2930
}
3031

3132
export interface IRemoteFormulaDependencyGenerator {
32-
generate(): Promise<Array<IFormulaCellAndFeatureItem>>;
33+
generate(range?: IUnitRange): Promise<Array<IFormulaCellAndFeatureItem>>;
3334
}
3435

3536
export const RemoteFormulaDependencyGeneratorServiceName = 'sheets-formula.remote-formula-dependency-generator.service';
@@ -44,7 +45,7 @@ export class RemoteFormulaDependencyGeneratorService implements IRemoteFormulaDe
4445
@IFormulaCurrentConfigService private readonly _currentConfigService: IFormulaCurrentConfigService
4546
) {}
4647

47-
async generate(): Promise<Array<IFormulaCellAndFeatureItem>> {
48+
async generate(range?: IUnitRange): Promise<Array<IFormulaCellAndFeatureItem>> {
4849
const configService = this._currentConfigService as FormulaCurrentConfigService;
4950
const originalForceCalculate = configService.isForceCalculate();
5051
configService.setForceCalculate(true);
@@ -54,15 +55,29 @@ export class RemoteFormulaDependencyGeneratorService implements IRemoteFormulaDe
5455
for (let i = 0; i < trees.length; i++) {
5556
const tree = trees[i];
5657
if ((isFormulaString(tree.formula) || isFormulaId(tree.formulaId)) || tree.featureId != null) {
57-
result.push({
58-
unitId: tree.unitId,
59-
subUnitId: tree.subUnitId,
60-
row: tree.row,
61-
column: tree.column,
62-
featureId: tree.featureId || undefined,
63-
formula: tree.formula || undefined,
64-
formulaId: tree.formulaId || undefined,
65-
});
58+
let include = true;
59+
if (range) {
60+
if (tree.unitId !== range.unitId || tree.subUnitId !== range.sheetId) {
61+
include = false;
62+
} else if (tree.row != null && tree.column != null) {
63+
const r = range.range;
64+
if (tree.row < r.startRow || tree.row > r.endRow || tree.column < r.startColumn || tree.column > r.endColumn) {
65+
include = false;
66+
}
67+
}
68+
// For features without row/column, include if unitId and subUnitId match
69+
}
70+
if (include) {
71+
result.push({
72+
unitId: tree.unitId,
73+
subUnitId: tree.subUnitId,
74+
row: tree.row,
75+
column: tree.column,
76+
featureId: tree.featureId || undefined,
77+
formula: tree.formula || undefined,
78+
formulaId: tree.formulaId || undefined,
79+
});
80+
}
6681
}
6782
}
6883
return result;

0 commit comments

Comments
 (0)