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.

refactor(SSC): remove SSC functionality (#7267)

wpxp123456cd749d617d66
diff --git a/examples/src/sheets-no-worker/main.ts b/examples/src/sheets-no-worker/main.ts
index c53d32ff65..a4bf9e4d79 100644
--- a/examples/src/sheets-no-worker/main.ts
+++ b/examples/src/sheets-no-worker/main.ts
@@ -88,7 +88,7 @@ function createNewInstance() {
univer.registerPlugin(UniverSheetsUIPlugin);
univer.registerPlugin(UniverSheetsNumfmtPlugin);
univer.registerPlugin(UniverFormulaEnginePlugin);
- univer.registerPlugin(UniverSheetsFormulaPlugin, { writeArrayFormulaToSnapshot: true });
+ univer.registerPlugin(UniverSheetsFormulaPlugin);
univer.registerPlugin(UniverSheetsDataValidationPlugin);
univer.registerPlugin(UniverSheetsConditionalFormattingPlugin);
univer.registerPlugin(UniverSheetsFilterPlugin);
diff --git a/packages/sheets-formula/src/config/config.ts b/packages/sheets-formula/src/config/config.ts
index 17574d4213..f863575916 100644
--- a/packages/sheets-formula/src/config/config.ts
+++ b/packages/sheets-formula/src/config/config.ts
@@ -50,11 +50,6 @@ export interface IUniverSheetsFormulaBaseConfig {
* Define the calculation mode during initialization, default is `WHEN_EMPTY`
*/
initialFormulaComputing?: CalculationMode;
-
- /**
- * If true, the plugin will write array formula results to the snapshot. Normally you should not set this value.
- */
- writeArrayFormulaToSnapshot?: boolean;
}
export const defaultPluginBaseConfig: IUniverSheetsFormulaBaseConfig = {};
diff --git a/packages/sheets-formula/src/controllers/__tests__/array-formula-cell-interceptor.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/array-formula-cell-interceptor.controller.spec.ts
index 8982e9beec..80e283e3ac 100644
--- a/packages/sheets-formula/src/controllers/__tests__/array-formula-cell-interceptor.controller.spec.ts
+++ b/packages/sheets-formula/src/controllers/__tests__/array-formula-cell-interceptor.controller.spec.ts
@@ -15,25 +15,18 @@
*/
import type { Dependency, IWorkbookData, Workbook, Worksheet } from '@univerjs/core';
-import type { IFormulaData } from '@univerjs/engine-formula';
import {
CellValueType,
ICommandService,
- IConfigService,
InterceptorEffectEnum,
LocaleType,
} from '@univerjs/core';
import {
FormulaDataModel,
- IDefinedNamesService,
- LexerTreeBuilder,
SetArrayFormulaDataMutation,
- SetDefinedNameMutation,
- SetFormulaCalculationResultMutation,
} from '@univerjs/engine-formula';
-import { INTERCEPTOR_POINT, SetRangeValuesMutation, SheetInterceptorService } from '@univerjs/sheets';
+import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
-import { PLUGIN_CONFIG_KEY_BASE } from '../../config/config';
import { createFacadeTestBed } from '../../facade/__tests__/create-test-bed';
import { ArrayFormulaCellInterceptorController } from '../array-formula-cell-interceptor.controller';
@@ -95,7 +88,6 @@ function getInterceptedCell(worksheet: Worksheet, workbook: Workbook, row: numbe
describe('ArrayFormulaCellInterceptorController', () => {
let testBed: ReturnType<typeof createControllerTestBed>;
let commandService: ICommandService;
- let configService: IConfigService;
let formulaDataModel: FormulaDataModel;
let interceptorService: SheetInterceptorService;
let workbook: Workbook;
@@ -104,16 +96,12 @@ describe('ArrayFormulaCellInterceptorController', () => {
beforeEach(() => {
testBed = createControllerTestBed();
commandService = testBed.injector.get(ICommandService);
- configService = testBed.injector.get(IConfigService);
formulaDataModel = testBed.injector.get(FormulaDataModel);
interceptorService = testBed.injector.get(SheetInterceptorService);
workbook = testBed.sheet as Workbook;
worksheet = workbook.getActiveSheet()!;
commandService.registerCommand(SetArrayFormulaDataMutation);
- commandService.registerCommand(SetDefinedNameMutation);
- commandService.registerCommand(SetFormulaCalculationResultMutation);
- commandService.registerCommand(SetRangeValuesMutation);
testBed.injector.get(ArrayFormulaCellInterceptorController);
});
@@ -123,217 +111,6 @@ describe('ArrayFormulaCellInterceptorController', () => {
testBed?.univer.dispose();
});
- it('should sync array formula data to the model and write snapshot refs when configured', async () => {
- configService.setConfig(PLUGIN_CONFIG_KEY_BASE, { writeArrayFormulaToSnapshot: true });
-
- const executeCommandSpy = vi.spyOn(commandService, 'executeCommand');
-
- const arrayFormulaRange = {
- test: {
- sheet1: {
- 0: {
- 0: {
- startRow: 0,
- endRow: 1,
- startColumn: 0,
- endColumn: 1,
- },
- },
- },
- },
- 'base-unit': {
- tasks: {
- 0: {
- 0: {
- startRow: 0,
- endRow: 0,
- startColumn: 0,
- endColumn: 0,
- },
- },
- },
- },
- };
- const arrayFormulaEmbedded = {
- test: {
- sheet1: {
- 0: {
- 0: {},
- 2: {},
- },
- },
- },
- 'base-unit': {
- tasks: {
- 0: {
- 0: {},
- },
- },
- },
- };
- const arrayFormulaCellData = {
- test: {
- sheet1: {
- 0: {
- 0: { v: 1, t: CellValueType.NUMBER },
- 1: { v: 2, t: CellValueType.NUMBER },
- },
- },
- },
- 'base-unit': {
- tasks: {
- 0: {
- 0: { v: 99, t: CellValueType.NUMBER },
- },
- },
- },
- };
-
- await commandService.executeCommand(SetArrayFormulaDataMutation.id, {
- arrayFormulaRange,
- arrayFormulaEmbedded,
- arrayFormulaCellData,
- });
-
- expect(formulaDataModel.getArrayFormulaRange()).toEqual(arrayFormulaRange);
- expect(formulaDataModel.getArrayFormulaCellData()).toEqual(arrayFormulaCellData);
-
- const setRangeValuesCalls = executeCommandSpy.mock.calls.filter(([id]) => id === SetRangeValuesMutation.id);
-
- expect(setRangeValuesCalls).toHaveLength(3);
- expect(setRangeValuesCalls.map((call) => (call[1] as { unitId?: string }).unitId)).toEqual(['test', 'test', 'test']);
- expect(setRangeValuesCalls[0]?.[1]).toMatchObject({
- unitId: 'test',
- subUnitId: 'sheet1',
- cellValue: {
- 0: {
- 0: { ref: 'A1:B2' },
- },
- },
- });
- expect(setRangeValuesCalls[1]?.[1]).toMatchObject({
- unitId: 'test',
- subUnitId: 'sheet1',
- cellValue: {
- 0: {
- 2: { ref: 'C1' },
- },
- },
- });
- expect(setRangeValuesCalls[2]?.[1]).toMatchObject({
- unitId: 'test',
- subUnitId: 'sheet1',
- cellValue: arrayFormulaCellData.test.sheet1,
- });
- });
-
- it('should add prefixed formulas for workbook cells and defined names when calculation results arrive', async () => {
- configService.setConfig(PLUGIN_CONFIG_KEY_BASE, { writeArrayFormulaToSnapshot: true });
-
- const definedNamesService = testBed.injector.get(IDefinedNamesService);
- const lexerTreeBuilder = testBed.injector.get(LexerTreeBuilder);
- const executeCommandSpy = vi.spyOn(commandService, 'executeCommand');
- const prefixSpy = vi
- .spyOn(lexerTreeBuilder, 'getNewFormulaWithPrefix')
- .mockImplementation((formula) => `PREFIX:${formula}`);
-
- definedNamesService.registerDefinedNames('test', {
- formulaName: {
- id: 'formula-name',
- name: 'FORMULA_NAME',
- formulaOrRefString: '=SUM(A1)',
- localSheetId: 'AllDefaultWorkbook',
- comment: '',
- },
- refName: {
- id: 'ref-name',
- name: 'REF_NAME',
- formulaOrRefString: 'Sheet1!$A$1',
- localSheetId: 'AllDefaultWorkbook',
- comment: '',
- },
- });
-
- await commandService.executeCommand(SetFormulaCalculationResultMutation.id, {
- unitData: {},
- unitOtherData: {},
- });
-
- const setRangeValuesCalls = executeCommandSpy.mock.calls.filter(([id]) => id === SetRangeValuesMutation.id);
- const setDefinedNameCalls = executeCommandSpy.mock.calls.filter(([id]) => id === SetDefinedNameMutation.id);
-
- expect(prefixSpy).toHaveBeenCalledWith('=SUM(A1)', expect.any(Function));
- expect(prefixSpy).toHaveBeenCalledWith('=SKIP()', expect.any(Function));
- expect(setRangeValuesCalls.at(-1)?.[1]).toMatchObject({
- unitId: 'test',
- subUnitId: 'sheet1',
- cellValue: {
- 3: {
- 0: { xf: 'PREFIX:=SUM(A1)' },
- 1: { xf: 'PREFIX:=SUM(A1)' },
- },
- },
- });
- expect(setDefinedNameCalls).toContainEqual([
- SetDefinedNameMutation.id,
- expect.objectContaining({
- unitId: 'test',
- name: 'FORMULA_NAME',
- formulaOrRefStringWithPrefix: 'PREFIX:=SUM(A1)',
- }),
- expect.objectContaining({
- onlyLocal: true,
- fromFormula: true,
- }),
- ]);
- });
-
- it('should write prefixed formulas only for sheet units when calculation data includes other unit types', async () => {
- configService.setConfig(PLUGIN_CONFIG_KEY_BASE, { writeArrayFormulaToSnapshot: true });
-
- const lexerTreeBuilder = testBed.injector.get(LexerTreeBuilder);
- const executeCommandSpy = vi.spyOn(commandService, 'executeCommand');
- const prefixSpy = vi
- .spyOn(lexerTreeBuilder, 'getNewFormulaWithPrefix')
- .mockImplementation((formula) => `PREFIX:${formula}`);
-
- vi.spyOn(formulaDataModel, 'getFormulaData').mockReturnValue({
- test: {
- sheet1: {
- 3: {
- 0: { f: '=SUM(A1)' },
- },
- },
- },
- 'base-unit': {
- tasks: {
- 0: {
- 0: { f: '=SUM(A1)' },
- },
- },
- },
- } satisfies IFormulaData);
-
- await commandService.executeCommand(SetFormulaCalculationResultMutation.id, {
- unitData: {},
- unitOtherData: {},
- });
-
- const setRangeValuesCalls = executeCommandSpy.mock.calls.filter(([id]) => id === SetRangeValuesMutation.id);
-
- expect(prefixSpy).toHaveBeenCalledTimes(1);
- expect(setRangeValuesCalls).toHaveLength(1);
- expect(setRangeValuesCalls[0]?.[1]).toMatchObject({
- unitId: 'test',
- subUnitId: 'sheet1',
- cellValue: {
- 3: {
- 0: { xf: 'PREFIX:=SUM(A1)' },
- },
- },
- });
- });
-
it('should intercept array formula cells with default values, precision cleanup, raw passthrough and array values', () => {
formulaDataModel.setArrayFormulaCellData({
test: {
diff --git a/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts b/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts
index aef193607f..a163bca65b 100644
--- a/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts
+++ b/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts
@@ -14,25 +14,17 @@
* limitations under the License.
*/
-import type { ICellData, ICommandInfo, Nullable, Workbook } from '@univerjs/core';
-import type { IArrayFormulaEmbeddedMap, IArrayFormulaRangeType, IArrayFormulaUnitCellType, ISetArrayFormulaDataMutationParams, ISetDefinedNameMutationParam } from '@univerjs/engine-formula';
-import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';
-import type { IUniverSheetsFormulaBaseConfig } from '../config/config';
-import { CellValueType, Disposable, ICommandService, IConfigService, Inject, InterceptorEffectEnum, isRealNum, IUniverInstanceService, ObjectMatrix, UniverInstanceType } from '@univerjs/core';
-import { FormulaDataModel, IDefinedNamesService, IFunctionService, LexerTreeBuilder, serializeRange, SetArrayFormulaDataMutation, SetDefinedNameMutation, SetFormulaCalculationResultMutation, stripErrorMargin } from '@univerjs/engine-formula';
-import { INTERCEPTOR_POINT, SetRangeValuesMutation, SheetInterceptorService } from '@univerjs/sheets';
-import { PLUGIN_CONFIG_KEY_BASE } from '../config/config';
+import type { ICommandInfo } from '@univerjs/core';
+import type { ISetArrayFormulaDataMutationParams } from '@univerjs/engine-formula';
+import { CellValueType, Disposable, ICommandService, Inject, InterceptorEffectEnum, isRealNum } from '@univerjs/core';
+import { FormulaDataModel, SetArrayFormulaDataMutation, stripErrorMargin } from '@univerjs/engine-formula';
+import { INTERCEPTOR_POINT, SheetInterceptorService } from '@univerjs/sheets';
export class ArrayFormulaCellInterceptorController extends Disposable {
constructor(
@ICommandService private readonly _commandService: ICommandService,
- @IConfigService private readonly _configService: IConfigService,
- @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
@Inject(SheetInterceptorService) private _sheetInterceptorService: SheetInterceptorService,
- @Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel,
- @Inject(LexerTreeBuilder) private readonly _lexerTreeBuilder: LexerTreeBuilder,
- @IFunctionService private readonly _functionService: IFunctionService,
- @IDefinedNamesService private readonly _definedNamesService: IDefinedNamesService
+ @Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel
) {
super();
@@ -49,7 +41,6 @@ export class ArrayFormulaCellInterceptorController extends Disposable {
this.disposeWithMe(
this._commandService.onCommandExecuted((command: ICommandInfo) => {
// Synchronous data from worker
- const isSSC = this._configService.getConfig<IUniverSheetsFormulaBaseConfig>(PLUGIN_CONFIG_KEY_BASE)?.writeArrayFormulaToSnapshot;
if (command.id === SetArrayFormulaDataMutation.id) {
const params = command.params as ISetArrayFormulaDataMutationParams;
@@ -57,179 +48,14 @@ export class ArrayFormulaCellInterceptorController extends Disposable {
return;
}
- const { arrayFormulaRange, arrayFormulaCellData, arrayFormulaEmbedded } = params;
+ const { arrayFormulaRange, arrayFormulaCellData } = params;
this._formulaDataModel.setArrayFormulaRange(arrayFormulaRange);
this._formulaDataModel.setArrayFormulaCellData(arrayFormulaCellData);
-
- // Note the logic should only be executed in exporting and hence shall be set once.
- if (isSSC) {
- this._writeArrayFormulaToSnapshot(arrayFormulaRange, arrayFormulaCellData, arrayFormulaEmbedded);
- }
- } else if (command.id === SetFormulaCalculationResultMutation.id && isSSC) {
- this._addPrefixToFunctionSnapshot();
- this._addPrefixToDefinedNamesFunctionSnapshot();
}
})
);
}
- private _addPrefixToDefinedNamesFunctionSnapshot() {
- const allDefinedNames = this._definedNamesService.getAllDefinedNames();
- Object.entries(allDefinedNames).forEach(([unitId, definedNames]) => {
- definedNames && Array.from(Object.entries(definedNames)).forEach(([_, definedName]) => {
- const { formulaOrRefString } = definedName;
- if (formulaOrRefString.substring(0, 1) === '=') {
- const newFormula = this._lexerTreeBuilder.getNewFormulaWithPrefix(formulaOrRefString, this._functionService.hasExecutor.bind(this._functionService));
- if (newFormula) {
- this._commandService.executeCommand<ISetDefinedNameMutationParam>(SetDefinedNameMutation.id, {
- ...definedName,
- unitId,
- formulaOrRefStringWithPrefix: newFormula,
- }, {
- onlyLocal: true,
- fromFormula: true,
- });
- }
- }
- });
- });
- }
-
- private _addPrefixToFunctionSnapshot() {
- const dataModel = this._formulaDataModel.getFormulaData();
- const cacheMap = new Map<string, string>();
- this._forEachSheetSnapshotData(dataModel, (unitId, subUnitId, formulaDataItem) => {
- // Convert from IObjectMatrixPrimitiveType<IRange> to IObjectMatrixPrimitiveType<ICellData>
- if (!formulaDataItem) {
- return;
- }
- const cellValue = new ObjectMatrix<ICellData>();
- const matrix = new ObjectMatrix(formulaDataItem);
- matrix.forValue((row, col, value) => {
- const functionText = value?.f;
- if (value?.x != null || !functionText || functionText.length === 0) {
- return;
- }
-
- if (cacheMap.has(functionText)) {
- const cachedFormula = cacheMap.get(functionText)!;
- cellValue.setValue(row, col, { xf: cachedFormula });
- return;
- }
-
- const newFormula = this._lexerTreeBuilder.getNewFormulaWithPrefix(functionText, this._functionService.hasExecutor.bind(this._functionService));
- if (newFormula) {
- cellValue.setValue(row, col, { xf: newFormula });
- cacheMap.set(functionText, newFormula);
- }
- });
-
- // Keep this local to avoid triggering re-calculate in the worker.
- this._commandService.executeCommand<ISetRangeValuesMutationParams>(SetRangeValuesMutation.id, {
- unitId,
- subUnitId,
- cellValue: cellValue.getMatrix(),
- }, {
- onlyLocal: true,
- fromFormula: true,
- });
- });
- cacheMap.clear();
- }
-
- private _forEachSheetSnapshotData<T>(
- snapshotData: Record<string, Nullable<Record<string, T>>> | null | undefined,
- iterator: (unitId: string, subUnitId: string, data: T) => void
- ): void {
- if (!snapshotData) {
- return;
- }
-
- const workbooks = this._univerInstanceService.getAllUnitsForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
- for (const workbook of workbooks) {
- const unitId = workbook.getUnitId();
- const subUnitData = snapshotData[unitId];
- if (!subUnitData) {
- continue;
- }
-
- for (const worksheet of workbook.getSheets()) {
- const subUnitId = worksheet.getSheetId();
- const data = subUnitData[subUnitId];
- if (data == null) {
- continue;
- }
-
- iterator(unitId, subUnitId, data);
- }
- }
- }
-
- private _writeArrayFormulaToSnapshot(arrayFormulaRange: IArrayFormulaRangeType, arrayFormulaCellData: IArrayFormulaUnitCellType, arrayFormulaEmbedded: IArrayFormulaEmbeddedMap) {
- // Write values to the `ref` property of the array formula range.
- this._forEachSheetSnapshotData(arrayFormulaRange, (unitId, subUnitId, rangeData) => {
- // Convert from IObjectMatrixPrimitiveType<IRange> to IObjectMatrixPrimitiveType<ICellData>
- const cellValue = new ObjectMatrix<ICellData>();
- const matrix = new ObjectMatrix(rangeData);
- matrix.forValue((row, col, value) => {
- cellValue.setValue(row, col, { ref: serializeRange(value) }); // convert to ref string
- });
-
- // Keep this local to avoid triggering re-calculate in the worker.
- this._commandService.executeCommand<ISetRangeValuesMutationParams>(SetRangeValuesMutation.id, {
- unitId,
- subUnitId,
- cellValue: cellValue.getMatrix(),
- }, {
- onlyLocal: true,
- fromFormula: true,
- });
- });
-
- // <f t="array" ref="E7">SUM(A1:A7*B1:B7)</f> , write ref for embedded array formula cells
- this._forEachSheetSnapshotData(arrayFormulaEmbedded, (unitId, subUnitId, rangeData) => {
- const cellValue = new ObjectMatrix<ICellData>();
- const matrix = new ObjectMatrix(rangeData);
- matrix.forValue((row, col) => {
- const existingArrayRange = arrayFormulaRange?.[unitId]?.[subUnitId]?.[row]?.[col];
- if (existingArrayRange) {
- return; // Skip if there is already an array formula range at this position
- }
- cellValue.setValue(row, col, {
- ref: serializeRange({
- startRow: row,
- endRow: row,
- startColumn: col,
- endColumn: col,
- }),
- }); // convert to ref string
- });
-
- // Keep this local to avoid triggering re-calculate in the worker.
- this._commandService.executeCommand<ISetRangeValuesMutationParams>(SetRangeValuesMutation.id, {
- unitId,
- subUnitId,
- cellValue: cellValue.getMatrix(),
- }, {
- onlyLocal: true,
- fromFormula: true,
- });
- });
-
- // Write values to the 'v' property of the cell data.
- this._forEachSheetSnapshotData(arrayFormulaCellData, (unitId, subUnitId, rowData) => {
- // Keep this local to avoid triggering re-calculate in the worker.
- this._commandService.executeCommand<ISetRangeValuesMutationParams>(SetRangeValuesMutation.id, {
- unitId,
- subUnitId,
- cellValue: rowData,
- }, {
- onlyLocal: true,
- fromFormula: true,
- });
- });
- }
-
private _initInterceptorCellContent() {
this.disposeWithMe(
this._sheetInterceptorService.intercept(INTERCEPTOR_POINT.CELL_CONTENT, {