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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(sheets): add biz info on other formula (#6296)
WEI ZHANG23f6ae22b40e
diff --git a/packages-experimental/uni-formula-ui/src/services/uni-formula.service.ts b/packages-experimental/uni-formula-ui/src/services/uni-formula.service.tsindex 785ffcbf53..92b0aae567 100644--- a/packages-experimental/uni-formula-ui/src/services/uni-formula.service.ts+++ b/packages-experimental/uni-formula-ui/src/services/uni-formula.service.ts@@ -43,7 +43,7 @@ import {import { replaceSelectionFactory } from '@univerjs/docs';import { RichText } from '@univerjs/engine-render';import { DataSyncPrimaryController } from '@univerjs/rpc';-import { RegisterOtherFormulaService } from '@univerjs/sheets-formula';+import { OtherFormulaBizType, RegisterOtherFormulaService } from '@univerjs/sheets-formula';import { CanvasView } from '@univerjs/slides-ui';import { DumbUniFormulaService, IUniFormulaService } from '@univerjs/uni-formula';import { take } from 'rxjs';@@ -237,7 +237,7 @@ export class UniFormulaService extends DumbUniFormulaService implements IUniFormconst pseudoId = getPseudoUnitKey(unitId);this._checkSyncingUnit(pseudoId);- const id = this._registerOtherFormulaSrv.registerFormulaWithRange(pseudoId, PSEUDO_SUBUNIT, f);+ const id = this._registerOtherFormulaSrv.registerFormulaWithRange(pseudoId, PSEUDO_SUBUNIT, f, undefined, undefined, OtherFormulaBizType.DOC, rangeId);this._docFormulas.set(key, { unitId, rangeId, f, formulaId: id, v, t });this._formulaIdToKey.set(id, key);@@ -267,7 +267,7 @@ export class UniFormulaService extends DumbUniFormulaService implements IUniFormconst pseudoId = getPseudoUnitKey(unitId);this._checkSyncingUnit(pseudoId);- const id = this._registerOtherFormulaSrv.registerFormulaWithRange(pseudoId, PSEUDO_SUBUNIT, f);+ const id = this._registerOtherFormulaSrv.registerFormulaWithRange(pseudoId, PSEUDO_SUBUNIT, f, undefined, undefined, OtherFormulaBizType.SLIDE, `${pageId}_${elementId}_${rangeId}`);this._slideFormulas.set(key, { unitId, pageId, elementId, rangeId, f, formulaId: id, v, t });this._formulaIdToKey.set(id, key);@@ -318,11 +318,11 @@ export class UniFormulaService extends DumbUniFormulaService implements IUniForm// are registered formulas but not added to the formula system.this._docFormulas.forEach((value, key) => {if (!value.formulaId) {- const { unitId, f } = value;+ const { unitId, f, rangeId } = value;const pseudoId = getPseudoUnitKey(unitId);this._checkSyncingUnit(pseudoId);- const id = this._registerOtherFormulaSrv.registerFormulaWithRange(pseudoId, PSEUDO_SUBUNIT, f);+ const id = this._registerOtherFormulaSrv.registerFormulaWithRange(pseudoId, PSEUDO_SUBUNIT, f, undefined, undefined, OtherFormulaBizType.DOC, rangeId);value.formulaId = id;this._formulaIdToKey.set(id, key);}diff --git a/packages/engine-formula/src/engine/ast-node/reference-node.ts b/packages/engine-formula/src/engine/ast-node/reference-node.tsindex ba2b949573..5230c9f63e 100644--- a/packages/engine-formula/src/engine/ast-node/reference-node.ts+++ b/packages/engine-formula/src/engine/ast-node/reference-node.ts@@ -34,12 +34,12 @@ import { ISuperTableService } from '../../services/super-table.service';import { LexerNode } from '../analysis/lexer-node';import { TableReferenceObject } from '../reference-object/table-reference-object';import { prefixHandler } from '../utils/prefix-handler';+import { splitTableStructuredRef } from '../utils/reference';import { getReferenceObjectFromCache, ReferenceObjectType } from '../utils/value-object';import { ErrorValueObject } from '../value-object/base-value-object';import { BaseAstNode } from './base-ast-node';import { BaseAstNodeFactory, DEFAULT_AST_NODE_FACTORY_Z_INDEX } from './base-ast-node-factory';import { NODE_ORDER_MAP, NodeType } from './node-type';-import { splitTableStructuredRef } from '../utils/reference';export class ReferenceNode extends BaseAstNode {private _refOffsetX = 0;diff --git a/packages/sheets-conditional-formatting/src/plugin.ts b/packages/sheets-conditional-formatting/src/plugin.tsindex f7a1f1f146..3c06861a2c 100644--- a/packages/sheets-conditional-formatting/src/plugin.ts+++ b/packages/sheets-conditional-formatting/src/plugin.ts@@ -84,6 +84,9 @@ export class UniverSheetsConditionalFormattingPlugin extends Plugin {override onStarting(): void {this._injector.get(ConditionalFormattingService);- touchDependencies(this._injector, [[ConditionalFormattingService], [ConditionalFormattingViewModel]]);+ touchDependencies(this._injector, [+ [ConditionalFormattingService],+ [ConditionalFormattingViewModel],+ ]);}}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.tsindex 33226d334f..f8a72f0269 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@@ -16,12 +16,12 @@import type { ICellData, IRange, Nullable } from '@univerjs/core';import type { IOtherFormulaResult } from '@univerjs/sheets-formula';-import type { IConditionalFormattingRuleConfig } from '../models/type';+import type { IConditionalFormattingRuleConfig, IConditionFormattingRule } from '../models/type';import { BooleanNumber, CellValueType, Disposable, Inject, ObjectMatrix, RefAlias } from '@univerjs/core';-import { FormulaResultStatus, RegisterOtherFormulaService } from '@univerjs/sheets-formula';+import { FormulaResultStatus, OtherFormulaBizType, RegisterOtherFormulaService } from '@univerjs/sheets-formula';import { Subject } from 'rxjs';-import { CFRuleType, CFValueType } from '../base/const';+import { CFRuleType, CFSubRuleType, CFValueType } from '../base/const';import { ConditionalFormattingRuleModel } from '../models/conditional-formatting-rule-model';// eslint-disable-next-line ts/consistent-type-definitions@@ -75,9 +75,36 @@ export class ConditionalFormattingFormulaService extends Disposable {this._removeFormulaByCfId(unitId, subUnitId, rule.cfId);}}+ if (option.type === 'add') {+ this._registerRuleFormulas(unitId, subUnitId, rule);+ }}));}+ /**+ * Register formulas for a specific rule based on its type+ */+ private _registerRuleFormulas(unitId: string, subUnitId: string, rule: IConditionFormattingRule): void {+ switch (rule.rule.type) {+ case CFRuleType.highlightCell: {+ // For highlight cell with formula subtype+ if (rule.rule.subType === CFSubRuleType.formula) {+ const formulaText = rule.rule.value;+ if (formulaText) {+ this.registerFormulaWithRange(+ unitId,+ subUnitId,+ rule.cfId,+ formulaText,+ rule.ranges+ );+ }+ }+ break;+ }+ }+ }+private _initFormulaResultChange() {// Subscribe to formula calculation resultsthis.disposeWithMe(this._registerOtherFormulaService.formulaResult$.subscribe((results: Record<string, Record<string, IOtherFormulaResult[]>>) => {@@ -126,7 +153,7 @@ export class ConditionalFormattingFormulaService extends Disposable {if (formulaMap.getValue(cfFormulaId, ['id'])) {return;}- const formulaId = this._registerOtherFormulaService.registerFormulaWithRange(unitId, subUnitId, formulaText, ranges);+ const formulaId = this._registerOtherFormulaService.registerFormulaWithRange(unitId, subUnitId, formulaText, ranges, undefined, OtherFormulaBizType.CONDITIONAL_FORMATTING, cfId);formulaMap.addValue({formulaText,unitId,diff --git a/packages/sheets-data-validation/src/services/dv-custom-formula.service.ts b/packages/sheets-data-validation/src/services/dv-custom-formula.service.tsindex 457e690087..949ca5f757 100644--- a/packages/sheets-data-validation/src/services/dv-custom-formula.service.ts+++ b/packages/sheets-data-validation/src/services/dv-custom-formula.service.ts@@ -17,7 +17,7 @@import type { IRange, ISheetDataValidationRule } from '@univerjs/core';import { Disposable, Inject, isFormulaString, IUniverInstanceService, Rectangle, UniverInstanceType } from '@univerjs/core';import { DataValidationModel, DataValidatorRegistryService } from '@univerjs/data-validation';-import { RegisterOtherFormulaService } from '@univerjs/sheets-formula';+import { OtherFormulaBizType, RegisterOtherFormulaService } from '@univerjs/sheets-formula';import { getFormulaCellData, shouldOffsetFormulaByRange } from '../utils/formula';import { DataValidationCacheService } from './dv-cache.service';@@ -111,7 +111,7 @@ export class DataValidationCustomFormulaService extends Disposable {};private _registerFormula(unitId: string, subUnitId: string, ruleId: string, formulaString: string, ranges: IRange[]) {- return this._registerOtherFormulaService.registerFormulaWithRange(unitId, subUnitId, formulaString, ranges, { ruleId });+ return this._registerOtherFormulaService.registerFormulaWithRange(unitId, subUnitId, formulaString, ranges, { ruleId }, OtherFormulaBizType.DATA_VALIDATION_CUSTOM, ruleId);};private _handleDirtyRanges(unitId: string, subUnitId: string, ranges: IRange[]) {diff --git a/packages/sheets-data-validation/src/services/dv-formula.service.ts b/packages/sheets-data-validation/src/services/dv-formula.service.tsindex a3f7e9d684..3d3c0ac2f5 100644--- a/packages/sheets-data-validation/src/services/dv-formula.service.ts+++ b/packages/sheets-data-validation/src/services/dv-formula.service.ts@@ -18,7 +18,7 @@ import type { ISheetDataValidationRule, Nullable } from '@univerjs/core';import type { IFormulaInfo, IOtherFormulaResult } from '@univerjs/sheets-formula';import { DataValidationType, Disposable, Inject, isFormulaString, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';import { DataValidationModel, DataValidatorRegistryService } from '@univerjs/data-validation';-import { RegisterOtherFormulaService } from '@univerjs/sheets-formula';+import { OtherFormulaBizType, RegisterOtherFormulaService } from '@univerjs/sheets-formula';import { shouldOffsetFormulaByRange } from '../utils/formula';import { DataValidationCacheService } from './dv-cache.service';import { DataValidationListCacheService } from './dv-list-cache.service';@@ -89,7 +89,7 @@ export class DataValidationFormulaService extends Disposable {private _registerSingleFormula(unitId: string, subUnitId: string, formula: string, ruleId: string) {const ranges = [{ startColumn: 0, endColumn: 0, startRow: 0, endRow: 0 }];- return this._registerOtherFormulaService.registerFormulaWithRange(unitId, subUnitId, formula, ranges, { ruleId });+ return this._registerOtherFormulaService.registerFormulaWithRange(unitId, subUnitId, formula, ranges, { ruleId }, OtherFormulaBizType.DATA_VALIDATION, ruleId);}addRule(unitId: string, subUnitId: string, rule: ISheetDataValidationRule) {diff --git a/packages/sheets-formula/src/index.ts b/packages/sheets-formula/src/index.tsindex 7480151efe..f5a3e60875 100644--- a/packages/sheets-formula/src/index.ts+++ b/packages/sheets-formula/src/index.ts@@ -33,6 +33,6 @@ export type { IRegisterAsyncFunction, IRegisterFunction, ISingleFunctionRegisterexport type { IRegisterFunctionParams, IUnregisterFunctionParams } from './services/register-function.service';export { RegisterFunctionService } from './services/register-function.service';export { IRegisterFunctionService } from './services/register-function.service';-export { RegisterOtherFormulaService } from './services/register-other-formula.service';+export { OtherFormulaBizType, RegisterOtherFormulaService } from './services/register-other-formula.service';export { IRemoteRegisterFunctionService, RemoteRegisterFunctionService } from './services/remote/remote-register-function.service';export { calculateFormula } from './util/calculate';diff --git a/packages/sheets-formula/src/services/register-other-formula.service.ts b/packages/sheets-formula/src/services/register-other-formula.service.tsindex cd5cdb9c60..dca1fadbe0 100644--- a/packages/sheets-formula/src/services/register-other-formula.service.ts+++ b/packages/sheets-formula/src/services/register-other-formula.service.ts@@ -24,6 +24,15 @@ import { BehaviorSubject, bufferWhen, filter, Subject } from 'rxjs';import { OtherFormulaMarkDirty } from '../commands/mutations/formula.mutation';import { FormulaResultStatus } from './formula-common';+export enum OtherFormulaBizType {+ DEFAULT = 'default',+ DATA_VALIDATION = 'dv',+ DATA_VALIDATION_CUSTOM = 'dv-custom',+ CONDITIONAL_FORMATTING = 'cf',+ DOC = 'doc',+ SLIDE = 'slide',+}+export class RegisterOtherFormulaService extends Disposable {private _formulaCacheMap: Map<string, Map<string, Map<string, IOtherFormulaResult>>> = new Map();@@ -72,8 +81,8 @@ export class RegisterOtherFormulaService extends Disposable {return subUnitMap;}- private _createFormulaId(unitId: string, subUnitId: string) {- return `formula.${unitId}_${subUnitId}_${generateRandomId(8)}`;+ private _createFormulaId(unitId: string, subUnitId: string, bizType: OtherFormulaBizType, bizId: string) {+ return `formula.${unitId}_${subUnitId}_${bizType}_${bizId}_${generateRandomId(8)}`;}private _initFormulaRegister() {@@ -182,8 +191,8 @@ export class RegisterOtherFormulaService extends Disposable {}));}- registerFormulaWithRange(unitId: string, subUnitId: string, formulaText: string, ranges: IRange[] = [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 }], extra?: Record<string, any>) {- const formulaId = this._createFormulaId(unitId, subUnitId);+ registerFormulaWithRange(unitId: string, subUnitId: string, formulaText: string, ranges: IRange[] = [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 }], extra?: Record<string, any>, bizType: OtherFormulaBizType = OtherFormulaBizType.DEFAULT, bizId: string = '') {+ const formulaId = this._createFormulaId(unitId, subUnitId, bizType, bizId);const cacheMap = this._ensureCacheMap(unitId, subUnitId);cacheMap.set(formulaId, {