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(formula): preserve formulas when using format painter (#7145)

wpxp1234561378fce978ea
diff --git a/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts
index 25192272a9..56cb241bb4 100644
--- a/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts
+++ b/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts
@@ -702,6 +702,28 @@ describe('UpdateFormulaController', () => {
});
expect(executeCommandSpy.mock.calls.filter(([id]) => id === SetFormulaDataMutation.id)).toHaveLength(formulaSyncCallCount);
+
+ await commandService.executeCommand(SetRangeValuesMutation.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ cellValue: {
+ 6: {
+ 0: { s: { bg: { rgb: '#ff0000' } } },
+ },
+ },
+ });
+
+ const cellFormula = testBed.injector
+ .get(IUniverInstanceService)
+ .getUnit<Workbook>('test')
+ ?.getSheetBySheetId('sheet1')
+ ?.getRange(6, 0, 6, 0)
+ .getValue()
+ ?.f;
+
+ expect(formulaDataModel.getFormulaData().test?.sheet1?.[6]?.[0]).toMatchObject({ f: '=A1' });
+ expect(cellFormula).toBe('=A1');
+ expect(executeCommandSpy.mock.calls.filter(([id]) => id === SetFormulaDataMutation.id)).toHaveLength(formulaSyncCallCount);
});
it('should sync array formula data when editing a spill cell without formula data changes', async () => {
diff --git a/packages/sheets-formula/src/controllers/update-formula.controller.ts b/packages/sheets-formula/src/controllers/update-formula.controller.ts
index 17ce000cbb..25327f0e99 100644
--- a/packages/sheets-formula/src/controllers/update-formula.controller.ts
+++ b/packages/sheets-formula/src/controllers/update-formula.controller.ts
@@ -18,6 +18,7 @@ import type {
ICellData,
ICommandInfo,
IExecutionOptions,
+ IObjectMatrixPrimitiveType,
IUnitRange,
Nullable,
Workbook,
@@ -112,17 +113,10 @@ export class UpdateFormulaController extends Disposable {
if (command.id === SetRangeValuesMutation.id) {
const params = command.params as ISetRangeValuesMutationParams;
- if (
- (options && options.onlyLocal === true) ||
- (options && options.syncOnly === true) ||
- (options && options.fromChangeset === true) ||
- params.trigger === SetStyleCommand.id ||
- params.trigger === SetBorderCommand.id ||
- params.trigger === ClearSelectionFormatCommand.id ||
- params.trigger === SetRangeCustomMetadataCommand.id
- ) {
+ if (shouldSkipFormulaUpdateForSetRangeValues(params, options)) {
return;
}
+
this._handleSetRangeValuesMutation(params as ISetRangeValuesMutationParams);
}
})
@@ -686,6 +680,63 @@ export class UpdateFormulaController extends Disposable {
}
}
+/**
+ * Whether to skip the formula update when the setRangeValues mutation is executed.
+ * The style-only cell value does not affect the formula calculation, so it can be skipped.
+ */
+function shouldSkipFormulaUpdateForSetRangeValues(params: ISetRangeValuesMutationParams, options?: IExecutionOptions): boolean {
+ if (
+ options &&
+ (options.onlyLocal === true || options.syncOnly === true || options.fromChangeset === true)
+ ) {
+ return true;
+ }
+
+ const { cellValue, trigger } = params;
+
+ if (
+ trigger &&
+ [
+ SetStyleCommand.id,
+ SetBorderCommand.id,
+ ClearSelectionFormatCommand.id,
+ SetRangeCustomMetadataCommand.id,
+ ].includes(trigger)
+ ) {
+ return true;
+ }
+
+ if (!cellValue) {
+ return true;
+ }
+
+ return isStyleOnlyCellValue(cellValue);
+}
+
+function isStyleOnlyCellValue(cellValue: IObjectMatrixPrimitiveType<Nullable<ICellData>>): boolean {
+ const matrix = new ObjectMatrix(cellValue);
+
+ let hasCell = false;
+ let styleOnly = true;
+
+ matrix.forValue((_row, _col, cell) => {
+ hasCell = true;
+
+ if (!cell) {
+ styleOnly = false;
+ return false;
+ }
+
+ const keys = Object.keys(cell);
+ if (keys.length !== 1 || keys[0] !== 's') {
+ styleOnly = false;
+ return false;
+ }
+ });
+
+ return hasCell && styleOnly;
+}
+
function tableReferenceContainsColumn(columnStruct: string, columnNames: string[] | undefined): boolean {
if (!columnNames?.length || columnStruct.length === 0) {
return false;
diff --git a/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts b/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts
index 2a54192c93..b65e045642 100644
--- a/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts
+++ b/packages/sheets-ui/src/commands/commands/__tests__/set-format-painter.command.spec.ts
@@ -34,6 +34,7 @@ import {
SetRangeValuesCommand,
SetRangeValuesMutation,
SetSelectionsOperation,
+ SetStyleCommand,
} from '@univerjs/sheets';
import { BehaviorSubject } from 'rxjs';
import { beforeEach, describe, expect, it } from 'vitest';
@@ -350,6 +351,49 @@ describe('Test format painter rules in controller', () => {
});
describe('format painter to single cell', () => {
+ it('marks generated set range values mutations as style updates', async () => {
+ expect(await commandService.executeCommand(SetSelectionsOperation.id, {
+ unitId: 'workbook-01',
+ subUnitId: 'sheet-0011',
+
+ selections: [
+ {
+ range: {
+ startRow: 0,
+ endRow: 0,
+ startColumn: 0,
+ endColumn: 0,
+ },
+ },
+ ],
+ })).toBeTruthy();
+
+ expect(await commandService.executeCommand(SetOnceFormatPainterCommand.id)).toBeTruthy();
+
+ const setRangeValuesParams: Array<{ trigger?: string }> = [];
+ const disposable = commandService.onCommandExecuted((command) => {
+ if (command.id === SetRangeValuesMutation.id) {
+ setRangeValuesParams.push(command.params as { trigger?: string });
+ }
+ });
+
+ expect(await commandService.executeCommand(ApplyFormatPainterCommand.id, {
+ range: {
+ startRow: 5,
+ endRow: 5,
+ startColumn: 0,
+ endColumn: 0,
+ },
+ unitId: 'workbook-01',
+ subUnitId: 'sheet-0011',
+ })).toBeTruthy();
+
+ disposable.dispose();
+
+ expect(setRangeValuesParams.length).toBeGreaterThan(0);
+ expect(setRangeValuesParams.every((params) => params.trigger === SetStyleCommand.id)).toBe(true);
+ });
+
it('will copy whole original styles', async () => {
expect(await commandService.executeCommand(SetSelectionsOperation.id, {
unitId: 'workbook-01',
diff --git a/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts b/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts
index 33ce197fdf..23ccceade4 100644
--- a/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts
+++ b/packages/sheets-ui/src/controllers/format-painter/format-painter.controller.ts
@@ -42,6 +42,7 @@ import {
SetRangeValuesMutation,
SetRangeValuesUndoMutationFactory,
SetSelectionsOperation,
+ SetStyleCommand,
SheetInterceptorService,
SheetsSelectionsService,
} from '@univerjs/sheets';
@@ -205,11 +206,13 @@ export class FormatPainterController extends Disposable {
const clearStyleMutationParams: ISetRangeValuesMutationParams = {
subUnitId,
unitId,
+ trigger: SetStyleCommand.id,
cellValue: clearCellValue.getMatrix(),
};
const setRangeValuesMutationParams: ISetRangeValuesMutationParams = {
subUnitId,
unitId,
+ trigger: SetStyleCommand.id,
cellValue: cellValue.getMatrix(),
};
const undoSetRangeValuesMutationParams: ISetRangeValuesMutationParams = this._injector.invoke(