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
fix(formula): remove formula when content clear (#1020)
Dushusir4379d6e3a373
diff --git a/packages/engine-formula/src/models/__tests__/formula-data.model.spec.ts b/packages/engine-formula/src/models/__tests__/formula-data.model.spec.tsindex ce8f705bdb..2595279cd4 100644--- a/packages/engine-formula/src/models/__tests__/formula-data.model.spec.ts+++ b/packages/engine-formula/src/models/__tests__/formula-data.model.spec.ts@@ -61,7 +61,7 @@ const TEST_WORKBOOK_DATA_DEMO: IWorkbookData = {};describe('Test formula data model', () => {- describe('updateFormulaData', () => {+ describe('formulaDataModel function', () => {let univer: Univer;let get: Injector['get'];let formulaDataModel: FormulaDataModel;@@ -96,153 +96,259 @@ describe('Test formula data model', () => {univer.dispose();});- it('delete formula with id', () => {- formulaDataModel.initFormulaData();+ describe('updateFormulaData', () => {+ it('delete formula with id', () => {+ formulaDataModel.initFormulaData();- const unitId = 'test';- const sheetId = 'sheet1';- const cellValue = {- '1': {- '3': {- v: null,- p: null,- f: null,- si: null,+ const unitId = 'test';+ const sheetId = 'sheet1';+ const cellValue = {+ '1': {+ '3': {+ v: null,+ p: null,+ f: null,+ si: null,+ },},- },- };+ };- const result = {- [unitId]: {- [sheetId]: {- '0': {- '3': {- f: '=SUM(A1)',- si: '3e4r5t',+ const result = {+ [unitId]: {+ [sheetId]: {+ '0': {+ '3': {+ f: '=SUM(A1)',+ si: '3e4r5t',+ },},- },- '2': {- '3': {- f: '=SUM(A3)',- si: 'OSPtzm',+ '2': {+ '3': {+ f: '=SUM(A3)',+ si: 'OSPtzm',+ },},- },- '3': {'3': {- f: '=SUM(A3)',- si: 'OSPtzm',- x: 0,- y: 1,+ '3': {+ f: '=SUM(A3)',+ si: 'OSPtzm',+ x: 0,+ y: 1,+ },},},},- },- };+ };- formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);+ formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);- const formulaData = formulaDataModel.getFormulaData();- expect(formulaData).toStrictEqual(result);- });+ const formulaData = formulaDataModel.getFormulaData();+ expect(formulaData).toStrictEqual(result);+ });- it('delete formulas with ids and formulas with only ids', () => {- formulaDataModel.initFormulaData();+ it('delete formulas with ids and formulas with only ids', () => {+ formulaDataModel.initFormulaData();- const unitId = 'test';- const sheetId = 'sheet1';- const cellValue = {- '0': {- '3': {- v: null,- p: null,- f: null,- si: null,+ const unitId = 'test';+ const sheetId = 'sheet1';+ const cellValue = {+ '0': {+ '3': {+ v: null,+ p: null,+ f: null,+ si: null,+ },},- },- '1': {- '3': {- v: null,- p: null,- f: null,- si: null,+ '1': {+ '3': {+ v: null,+ p: null,+ f: null,+ si: null,+ },},- },- '2': {- '3': {- v: null,- p: null,- f: null,- si: null,+ '2': {+ '3': {+ v: null,+ p: null,+ f: null,+ si: null,+ },},- },- };+ };- const result = {- [unitId]: {- [sheetId]: {- '3': {+ const result = {+ [unitId]: {+ [sheetId]: {'3': {- f: '=SUM(A4)',- si: 'OSPtzm',+ '3': {+ f: '=SUM(A4)',+ si: 'OSPtzm',+ },},},},- },- };+ };- formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);+ formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);- const formulaData = formulaDataModel.getFormulaData();- expect(formulaData).toStrictEqual(result);- });+ const formulaData = formulaDataModel.getFormulaData();+ expect(formulaData).toStrictEqual(result);+ });- it('delete the formula with only id', () => {- formulaDataModel.initFormulaData();+ it('delete the formula with only id', () => {+ formulaDataModel.initFormulaData();- const unitId = 'test';- const sheetId = 'sheet1';- const cellValue = {- '3': {+ const unitId = 'test';+ const sheetId = 'sheet1';+ const cellValue = {'3': {- v: null,- p: null,- f: null,- si: null,+ '3': {+ v: null,+ p: null,+ f: null,+ si: null,+ },},- },- };+ };- const result = {- [unitId]: {- [sheetId]: {- '0': {- '3': {- f: '=SUM(A1)',- si: '3e4r5t',+ const result = {+ [unitId]: {+ [sheetId]: {+ '0': {+ '3': {+ f: '=SUM(A1)',+ si: '3e4r5t',+ },+ },+ '1': {+ '3': {+ f: '=SUM(A2)',+ si: 'OSPtzm',+ },+ },+ '2': {+ '3': {+ f: '=SUM(A2)',+ si: 'OSPtzm',+ x: 0,+ y: 1,+ },},},- '1': {- '3': {- f: '=SUM(A2)',- si: 'OSPtzm',+ },+ };++ formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);++ const formulaData = formulaDataModel.getFormulaData();+ expect(formulaData).toStrictEqual(result);+ });+ });++ describe('updateArrayFormulaRange', () => {+ it('update array formula range', () => {+ const unitId = 'test';+ const sheetId = 'sheet1';++ formulaDataModel.setArrayFormulaRange({+ [unitId]: {+ [sheetId]: {+ '0': {+ '3': {+ startRow: 0,+ startColumn: 3,+ endRow: 1,+ endColumn: 3,+ },},},- '2': {- '3': {- f: '=SUM(A2)',- si: 'OSPtzm',- x: 0,- y: 1,+ },+ });++ const cellValue = {+ '0': {+ '3': {+ v: null,+ p: null,+ f: null,+ si: null,+ },+ },+ };++ const result = {+ [unitId]: {+ [sheetId]: {},+ },+ };++ formulaDataModel.updateArrayFormulaRange(unitId, sheetId, cellValue);++ const formulaData = formulaDataModel.getArrayFormulaRange();+ expect(formulaData).toStrictEqual(result);+ });+ });+ describe('updateArrayFormulaCellData', () => {+ it('update array formula cell data', () => {+ const unitId = 'test';+ const sheetId = 'sheet1';++ formulaDataModel.setArrayFormulaRange({+ [unitId]: {+ [sheetId]: {+ '0': {+ '3': {+ startRow: 0,+ startColumn: 3,+ endRow: 1,+ endColumn: 3,+ },},},},- },- };+ });- formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);+ formulaDataModel.setArrayFormulaCellData({+ [unitId]: {+ [sheetId]: {+ '0': {+ '3': {+ v: 1,+ },+ },+ '1': {+ '3': {+ v: 2,+ },+ },+ },+ },+ });- const formulaData = formulaDataModel.getFormulaData();- expect(formulaData).toStrictEqual(result);+ const cellValue = {+ '0': {+ '3': {+ v: null,+ p: null,+ f: null,+ si: null,+ },+ },+ };++ const result = {+ [unitId]: {+ [sheetId]: {},+ },+ };++ formulaDataModel.updateArrayFormulaCellData(unitId, sheetId, cellValue);++ const formulaData = formulaDataModel.getArrayFormulaCellData();+ expect(formulaData).toStrictEqual(result);+ });});});diff --git a/packages/engine-formula/src/models/formula-data.model.ts b/packages/engine-formula/src/models/formula-data.model.tsindex fdec60eb91..b549e78ad2 100644--- a/packages/engine-formula/src/models/formula-data.model.ts+++ b/packages/engine-formula/src/models/formula-data.model.ts@@ -375,6 +375,81 @@ export class FormulaDataModel extends Disposable {});}+ updateArrayFormulaRange(+ unitId: string,+ sheetId: string,+ cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>+ ) {+ // remove the array formula range when cell value is null++ const arrayFormulaRange = this._arrayFormulaRange[unitId]?.[sheetId];++ if (!arrayFormulaRange) return;++ const arrayFormulaRangeMatrix = new ObjectMatrix(arrayFormulaRange);++ const cellMatrix = new ObjectMatrix(cellValue);+ cellMatrix.forValue((r, c, cell) => {+ const arrayFormulaRangeValue = arrayFormulaRangeMatrix?.getValue(r, c);+ if (arrayFormulaRangeValue == null) {+ return true;+ }++ const formulaString = cell?.f || '';+ const formulaId = cell?.si || '';++ const checkFormulaString = isFormulaString(formulaString);+ const checkFormulaId = isFormulaId(formulaId);++ if (!checkFormulaString && !checkFormulaId) {+ arrayFormulaRangeMatrix.realDeleteValue(r, c);+ }+ });+ }++ updateArrayFormulaCellData(+ unitId: string,+ sheetId: string,+ cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>+ ) {+ // remove the array formula range when cell value is null++ const arrayFormulaRange = this._arrayFormulaRange[unitId]?.[sheetId];++ if (!arrayFormulaRange) return;++ const arrayFormulaRangeMatrix = new ObjectMatrix(arrayFormulaRange);++ const arrayFormulaCellData = this._arrayFormulaCellData[unitId]?.[sheetId];++ if (!arrayFormulaCellData) return;++ const arrayFormulaCellDataMatrix = new ObjectMatrix(arrayFormulaCellData);++ const cellMatrix = new ObjectMatrix(cellValue);+ cellMatrix.forValue((r, c, cell) => {+ const arrayFormulaRangeValue = arrayFormulaRangeMatrix?.getValue(r, c);+ if (arrayFormulaRangeValue == null) {+ return true;+ }++ const formulaString = cell?.f || '';+ const formulaId = cell?.si || '';++ const checkFormulaString = isFormulaString(formulaString);+ const checkFormulaId = isFormulaId(formulaId);++ if (!checkFormulaString && !checkFormulaId) {+ const { startRow, startColumn, endRow, endColumn } = arrayFormulaRangeValue;+ for (let r = startRow; r <= endRow; r++) {+ for (let c = startColumn; c <= endColumn; c++) {+ arrayFormulaCellDataMatrix.realDeleteValue(r, c);+ }+ }+ }+ });+ }+getFormulaItemBySId(sId: string, sheetId: string, unitId: string): Nullable<IFormulaDataItem> {const formulaData = this._formulaData;if (formulaData[unitId] == null) {diff --git a/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts b/packages/sheets-formula/src/controllers/formula-editor-show.controller.tsindex 0f3e2afd30..45a0bbe130 100644--- a/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts+++ b/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts@@ -29,6 +29,7 @@ import {ErrorType,FormulaDataModel,LexerTreeBuilder,+ SetArrayFormulaDataMutation,SetFormulaCalculationResultMutation,} from '@univerjs/engine-formula';import { IRenderManagerService } from '@univerjs/engine-render';@@ -216,8 +217,11 @@ export class FormulaEditorShowController extends Disposable {private _commandExecutedListener() {this.disposeWithMe(- this._commandService.onCommandExecuted((command: ICommandInfo) => {- if (command.id === SetFormulaCalculationResultMutation.id) {+ this._commandService.onCommandExecuted((command: ICommandInfo, options) => {+ if (+ command.id === SetFormulaCalculationResultMutation.id ||+ (command.id === SetArrayFormulaDataMutation.id && options && options.remove)+ ) {this._removeArrayFormulaRangeShape();}})diff --git a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts b/packages/sheets-formula/src/controllers/trigger-calculation.controller.tsindex 79287ecae5..c440858df2 100644--- a/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts+++ b/packages/sheets-formula/src/controllers/trigger-calculation.controller.ts@@ -102,7 +102,7 @@ export class TriggerCalculationController extends Disposable {...this._generateDirty(this._waitingCommandQueue),},{- local: true,+ onlyLocal: true,});@@ -253,7 +253,7 @@ export class TriggerCalculationController extends Disposable {forceCalculation: true,},{- local: true,+ onlyLocal: true,});}diff --git a/packages/sheets-formula/src/controllers/update-formula.controller.ts b/packages/sheets-formula/src/controllers/update-formula.controller.tsindex 8e826ed531..dacae1e530 100644--- a/packages/sheets-formula/src/controllers/update-formula.controller.ts+++ b/packages/sheets-formula/src/controllers/update-formula.controller.ts@@ -188,6 +188,8 @@ export class UpdateFormulaController extends Disposable {}this._formulaDataModel.updateFormulaData(unitId, sheetId, cellValue);+ this._formulaDataModel.updateArrayFormulaCellData(unitId, sheetId, cellValue);+ this._formulaDataModel.updateArrayFormulaRange(unitId, sheetId, cellValue);this._commandService.executeCommand(SetFormulaDataMutation.id,@@ -195,7 +197,19 @@ export class UpdateFormulaController extends Disposable {formulaData: this._formulaDataModel.getFormulaData(),},{- local: true,+ onlyLocal: true,+ }+ );++ this._commandService.executeCommand(+ SetArrayFormulaDataMutation.id,+ {+ arrayFormulaRange: this._formulaDataModel.getArrayFormulaRange(),+ arrayFormulaCellData: this._formulaDataModel.getArrayFormulaCellData(),+ },+ {+ onlyLocal: true,+ remove: true, // remove array formula range shape});}@@ -212,13 +226,25 @@ export class UpdateFormulaController extends Disposable {const arrayFormulaCellData = this._formulaDataModel.getArrayFormulaCellData();removeFormulaData(arrayFormulaCellData, unitId, sheetId);- this._commandService.executeCommand(SetFormulaDataMutation.id, {- formulaData,- });- this._commandService.executeCommand(SetArrayFormulaDataMutation.id, {- arrayFormulaRange,- arrayFormulaCellData,- });+ this._commandService.executeCommand(+ SetFormulaDataMutation.id,+ {+ formulaData,+ },+ {+ onlyLocal: true,+ }+ );+ this._commandService.executeCommand(+ SetArrayFormulaDataMutation.id,+ {+ arrayFormulaRange,+ arrayFormulaCellData,+ },+ {+ onlyLocal: true,+ }+ );}private _handleInsertSheetMutation(params: IInsertSheetMutationParams) {@@ -229,9 +255,15 @@ export class UpdateFormulaController extends Disposable {const cellMatrix = new ObjectMatrix(cellData);initSheetFormulaData(formulaData, unitId, sheetId, cellMatrix);- this._commandService.executeCommand(SetFormulaDataMutation.id, {- formulaData,- });+ this._commandService.executeCommand(+ SetFormulaDataMutation.id,+ {+ formulaData,+ },+ {+ onlyLocal: true,+ }+ );}private _getUpdateFormula(command: ICommandInfo) {@@ -311,7 +343,7 @@ export class UpdateFormulaController extends Disposable {arrayFormulaCellData: offsetArrayFormulaCellData,},{- local: true,+ onlyLocal: true,});diff --git a/packages/sheets-ui/src/controllers/editor/__tests__/create-test-bed.ts b/packages/sheets-ui/src/controllers/editor/__tests__/create-test-bed.tsnew file mode 100644index 0000000000..0d304d2ad8--- /dev/null+++ b/packages/sheets-ui/src/controllers/editor/__tests__/create-test-bed.ts@@ -0,0 +1,84 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { IWorkbookData, Workbook } from '@univerjs/core';+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';+import { SelectionManagerService, SheetInterceptorService } from '@univerjs/sheets';+import type { Dependency } from '@wendellhu/redi';+import { Inject, Injector } from '@wendellhu/redi';++function getTestWorkbookDataDemo(): IWorkbookData {+ return {+ id: 'test',+ appVersion: '3.0.0-alpha',+ sheets: {+ sheet1: {+ id: 'sheet1',+ cellData: {},+ },+ },+ locale: LocaleType.ZH_CN,+ name: '',+ sheetOrder: [],+ styles: {},+ };+}++export interface ITestBed {+ univer: Univer;+ get: Injector['get'];+ sheet: Workbook;+}++export function createTestBed(workbookConfig?: IWorkbookData, dependencies?: Dependency[]): ITestBed {+ const univer = new Univer();+ const injector = univer.__getInjector();++ class TestSpyPlugin extends Plugin {+ static override type = PluginType.Sheet;++ constructor(+ _config: undefined,+ @Inject(Injector) override readonly _injector: Injector+ ) {+ super('test-plugin');++ this._injector = _injector;+ }++ override onStarting(injector: Injector): void {+ injector.add([SelectionManagerService]);+ injector.add([SheetInterceptorService]);++ dependencies?.forEach((d) => injector.add(d));+ }+ }++ univer.registerPlugin(TestSpyPlugin);+ const sheet = univer.createUniverSheet(workbookConfig || getTestWorkbookDataDemo());++ const univerInstanceService = injector.get(IUniverInstanceService);+ univerInstanceService.focusUniverInstance('test');+ const logService = injector.get(ILogService);++ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs++ return {+ univer,+ get: injector.get.bind(injector),+ sheet,+ };+}diff --git a/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts b/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.tsnew file mode 100644index 0000000000..0973bdaa3f--- /dev/null+++ b/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts@@ -0,0 +1,178 @@+/**+ * Copyright 2023-present DreamNum Inc.+ *+ * Licensed under the Apache License, Version 2.0 (the "License");+ * you may not use this file except in compliance with the License.+ * You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import type { IDocumentData, Univer, Workbook } from '@univerjs/core';+import { LocaleService } from '@univerjs/core';+import { LexerTreeBuilder } from '@univerjs/engine-formula';+import { SpreadsheetSkeleton } from '@univerjs/engine-render';+import type { Injector } from '@wendellhu/redi';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';++import { getCellDataByInput } from '../end-edit.controller';+import { createTestBed } from './create-test-bed';++const richTextDemo: IDocumentData = {+ id: 'd',+ body: {+ dataStream: `Instructions:\f①Project division - Fill in the specific division of labor after the project is disassembled:\f②Responsible Person - Enter the responsible person's name here:\f③Date-The specific execution time of the project (detailed to the date of a certain month), and the gray color block marks the planned real-time time of the division of labor of the project (for example,\fthe specific execution time of [regional scene model arrangement and construction] is the 2 days marked in gray. \r\n`,+ textRuns: [+ {+ st: 0,+ ed: 488,+ ts: {+ cl: {+ rgb: 'rgb(92,92,92)',+ },+ },+ },+ ],+ paragraphs: [+ {+ startIndex: 489,+ paragraphStyle: {+ horizontalAlign: 0,+ spaceAbove: 10,+ lineSpacing: 1.2,+ },+ },+ ],+ },+ documentStyle: {+ pageSize: {+ width: Infinity,+ height: Infinity,+ },+ renderConfig: {+ centerAngle: 0,+ horizontalAlign: 0,+ vertexAngle: 0,+ verticalAlign: 0,+ wrapStrategy: 0,+ },+ marginTop: 0,+ marginBottom: 0,+ marginRight: 2,+ marginLeft: 2,+ },+};++describe('Test EndEditController', () => {+ let univer: Univer;+ let workbook: Workbook;+ let get: Injector['get'];+ let localeService: LocaleService;+ let lexerTreeBuilder: LexerTreeBuilder;+ let spreadsheetSkeleton: SpreadsheetSkeleton;++ beforeEach(() => {+ const testBed = createTestBed();++ univer = testBed.univer;+ workbook = testBed.sheet;+ get = testBed.get;++ localeService = get(LocaleService);+ lexerTreeBuilder = new LexerTreeBuilder();++ const worksheet = workbook.getActiveSheet();+ const config = worksheet.getConfig();+ spreadsheetSkeleton = new SpreadsheetSkeleton(+ worksheet,+ config,+ worksheet.getCellMatrix(),+ workbook.getStyles(),+ localeService+ );+ });++ afterEach(() => {+ univer.dispose();+ });++ describe('getCellDataByInput', () => {+ it('normal cell', () => {+ const cell = {+ v: 1,+ };++ const inputCell = {+ v: 2,+ };++ const documentLayoutObject = spreadsheetSkeleton.getCellDocumentModelWithFormula(inputCell);+ if (!documentLayoutObject) {+ expect(documentLayoutObject).not.toBeUndefined();+ return;+ }++ const cellData = getCellDataByInput(cell, documentLayoutObject, lexerTreeBuilder);+ expect(cellData).toEqual({ v: '2', f: null, si: null, p: null });+ });+ it('rich text cell', () => {+ const cell = {+ v: 1,+ };+ const inputCell = {+ p: richTextDemo,+ };++ const documentLayoutObject = spreadsheetSkeleton.getCellDocumentModelWithFormula(inputCell);+ if (!documentLayoutObject) {+ expect(documentLayoutObject).not.toBeUndefined();+ return;+ }++ const cellData = getCellDataByInput(cell, documentLayoutObject, lexerTreeBuilder);+ expect(cellData).toEqual({ v: null, f: null, si: null, p: richTextDemo });+ });+ it('formula cell', () => {+ const cell = {+ v: 1,+ };+ const inputCell = {+ f: '=SUM(1)',+ };++ const documentLayoutObject = spreadsheetSkeleton.getCellDocumentModelWithFormula(inputCell);+ if (!documentLayoutObject) {+ expect(documentLayoutObject).not.toBeUndefined();+ return;+ }++ const cellData = getCellDataByInput(cell, documentLayoutObject, lexerTreeBuilder);+ expect(cellData).toEqual({ v: null, f: '=SUM(1)', p: null });+ });+ it('clear formula cell', () => {+ const cell = {+ f: '=H18:H25',+ v: 0,+ t: 2,+ };+ const inputCell = {+ v: '',+ };++ const documentLayoutObject = spreadsheetSkeleton.getCellDocumentModelWithFormula(inputCell);+ if (!documentLayoutObject) {+ expect(documentLayoutObject).not.toBeUndefined();+ return;+ }++ const cellData = getCellDataByInput(cell, documentLayoutObject, lexerTreeBuilder);+ expect(cellData).toEqual({ v: '', f: null, si: null, p: null, t: 2 });+ });+ });+});diff --git a/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts b/packages/sheets-ui/src/controllers/editor/end-edit.controller.tsindex 6a0166f504..d4f8691b62 100644--- a/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts+++ b/packages/sheets-ui/src/controllers/editor/end-edit.controller.ts@@ -35,7 +35,7 @@ import {} from '@univerjs/core';import { MoveCursorOperation, MoveSelectionOperation } from '@univerjs/docs';import { LexerTreeBuilder, matchToken } from '@univerjs/engine-formula';-import type { IMouseEvent, IPointerEvent } from '@univerjs/engine-render';+import type { IDocumentLayoutObject, IMouseEvent, IPointerEvent } from '@univerjs/engine-render';import { DeviceInputEventType, IRenderManagerService } from '@univerjs/engine-render';import {SelectionManagerService,@@ -195,62 +195,13 @@ export class EndEditController extends Disposable {return;}- const cellData: ICellData = Tools.deepClone(worksheet.getCellRaw(row, column) || {});+ const cellData: Nullable<ICellData> = getCellDataByInput(+ worksheet.getCellRaw(row, column) || {},+ documentLayoutObject,+ this._lexerTreeBuilder+ );- const documentModel = documentLayoutObject.documentModel as DocumentDataModel;-- const snapshot = documentModel.getSnapshot();-- const body = snapshot.body;-- if (body == null) {- return;- }-- const data = body.dataStream;-- const lastString = data.substring(data.length - 2, data.length);- let newDataStream = lastString === DEFAULT_EMPTY_DOCUMENT_VALUE ? data.substring(0, data.length - 2) : data;-- if (isFormulaString(newDataStream)) {- if (cellData.f === newDataStream) {- return;- }- const bracketCount = this._lexerTreeBuilder.checkIfAddBracket(newDataStream);- for (let i = 0; i < bracketCount; i++) {- newDataStream += matchToken.CLOSE_BRACKET;- }-- cellData.f = newDataStream;- cellData.v = null;- cellData.p = null;- } else if (isRichText(body)) {- if (body.dataStream === '\r\n') {- cellData.v = '';- cellData.f = null;- cellData.si = null;- cellData.p = null;- } else {- cellData.p = snapshot;- cellData.v = null;- cellData.f = null;- cellData.si = null;- }- } else {- // add `cellData.p == null` to fix https://github.com/dream-num/univer/issues/1087- if (- // eslint-disable-next-line- (newDataStream == cellData.v || (newDataStream == '' && cellData.v == null)) &&- cellData.p == null- ) {- return;- }-- cellData.v = newDataStream;- cellData.f = null;- cellData.si = null;- cellData.p = null;- }+ if (cellData == null) return;const context = {subUnitId: sheetId,@@ -416,3 +367,62 @@ export class EndEditController extends Disposable {return getEditorObject(this._editorBridgeService.getCurrentEditorId(), this._renderManagerService);}}++export function getCellDataByInput(+ cellData: ICellData,+ documentLayoutObject: IDocumentLayoutObject,+ lexerTreeBuilder: LexerTreeBuilder+) {+ cellData = Tools.deepClone(cellData);++ const documentModel = documentLayoutObject.documentModel as DocumentDataModel;++ const snapshot = documentModel.getSnapshot();++ const body = snapshot.body;++ if (body == null) {+ return null;+ }++ const data = body.dataStream;+ const lastString = data.substring(data.length - 2, data.length);+ let newDataStream = lastString === DEFAULT_EMPTY_DOCUMENT_VALUE ? data.substring(0, data.length - 2) : data;++ if (isFormulaString(newDataStream)) {+ if (cellData.f === newDataStream) {+ return null;+ }+ const bracketCount = lexerTreeBuilder.checkIfAddBracket(newDataStream);+ for (let i = 0; i < bracketCount; i++) {+ newDataStream += matchToken.CLOSE_BRACKET;+ }++ cellData.f = newDataStream;+ cellData.v = null;+ cellData.p = null;+ } else if (isRichText(body)) {+ if (body.dataStream === '\r\n') {+ cellData.v = '';+ cellData.f = null;+ cellData.si = null;+ cellData.p = null;+ } else {+ cellData.p = snapshot;+ cellData.v = null;+ cellData.f = null;+ cellData.si = null;+ }+ } else {+ // If the data is empty, the data is set to null.+ if ((newDataStream === cellData.v || (newDataStream === '' && cellData.v == null)) && cellData.p == null) {+ return null;+ }+ cellData.v = newDataStream;+ cellData.f = null;+ cellData.si = null;+ cellData.p = null;+ }++ return cellData;+}