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(sheets): correct subUnitId usage in SetStyleCommand (#3586)
#FF0000b03711d294fb
diff --git a/packages/sheets/src/commands/commands/__tests__/set-style.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-style.command.spec.tsindex f91374f785..c3d8a68488 100644--- a/packages/sheets/src/commands/commands/__tests__/set-style.command.spec.ts+++ b/packages/sheets/src/commands/commands/__tests__/set-style.command.spec.ts@@ -14,7 +14,8 @@* limitations under the License.*/-import type { IColorStyle, Injector, ITextDecoration, ITextRotation, Univer } from '@univerjs/core';+import type { IColorStyle, Injector, ITextDecoration, ITextRotation, Univer, Workbook } from '@univerjs/core';+import type { ISetStyleCommandParams } from '../set-style.command';import {BooleanNumber,FontItalic,@@ -25,14 +26,16 @@ import {RANGE_TYPE,RedoCommand,UndoCommand,+ UniverInstanceType,VerticalAlign,WrapStrategy,} from '@univerjs/core';-import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';import { SheetsSelectionsService } from '../../../services/selections/selection-manager.service';+import { InsertSheetMutation } from '../../mutations/insert-sheet.mutation';import { SetRangeValuesMutation } from '../../mutations/set-range-values.mutation';-import type { ISetStyleCommandParams } from '../set-style.command';+import { InsertSheetCommand } from '../insert-sheet.command';import {SetBackgroundColorCommand,SetBoldCommand,@@ -77,6 +80,8 @@ describe("Test commands used for updating cells' styles", () => {commandService.registerCommand(SetTextRotationCommand);commandService.registerCommand(SetStyleCommand);commandService.registerCommand(SetRangeValuesMutation);+ commandService.registerCommand(InsertSheetCommand);+ commandService.registerCommand(InsertSheetMutation);});afterEach(() => univer.dispose());@@ -755,4 +760,52 @@ describe("Test commands used for updating cells' styles", () => {});});});++ describe('set style with specific range', () => {+ it('should use the correct unitId and subUnitId when range is provided', async () => {+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;++ // Insert a new sheet+ expect(await commandService.executeCommand(InsertSheetCommand.id)).toBeTruthy();++ const newSheet = workbook.getSheets()[1]; // Get the newly inserted sheet+ const unitId = workbook.getUnitId();+ const subUnitId = newSheet.getSheetId();++ const range = { startRow: 0, startColumn: 0, endColumn: 1, endRow: 1, rangeType: RANGE_TYPE.NORMAL };++ const commandParams: ISetStyleCommandParams<IColorStyle> = {+ range,+ unitId,+ subUnitId,+ style: {+ type: 'bg',+ value: { rgb: '#FF0000' },+ },+ };++ expect(await commandService.executeCommand(SetStyleCommand.id, commandParams)).toBeTruthy();++ // Verify that the background color was set correctly+ const getBackgroundColor = (row: number, col: number) =>+ workbook.getSheetBySheetId(subUnitId)!+ .getRange(row, col)+ .getBackground();++ expect(getBackgroundColor(0, 0)).toBe('#FF0000');+ expect(getBackgroundColor(0, 1)).toBe('#FF0000');+ expect(getBackgroundColor(1, 0)).toBe('#FF0000');+ expect(getBackgroundColor(1, 1)).toBe('#FF0000');++ // undo+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();+ expect(getBackgroundColor(0, 0)).toBe('#fff');+ expect(getBackgroundColor(1, 1)).toBe('#fff');++ // redo+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();+ expect(getBackgroundColor(0, 0)).toBe('#FF0000');+ expect(getBackgroundColor(1, 1)).toBe('#FF0000');+ });+ });});diff --git a/packages/sheets/src/commands/commands/set-style.command.ts b/packages/sheets/src/commands/commands/set-style.command.tsindex dfaa59a254..d508a755f6 100644--- a/packages/sheets/src/commands/commands/set-style.command.ts+++ b/packages/sheets/src/commands/commands/set-style.command.ts@@ -26,6 +26,9 @@ import type {VerticalAlign,WrapStrategy,} from '@univerjs/core';+import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';++import type { ISheetCommandSharedParams } from '../utils/interface';import {BooleanNumber,CommandType,@@ -38,22 +41,18 @@ import {sequenceExecute,Tools,} from '@univerjs/core';-import { SheetsSelectionsService } from '../../services/selections/selection-manager.service';import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';-import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';-import { getSheetCommandTarget } from './utils/target-util';import { createRangeIteratorWithSkipFilteredRows } from './utils/selection-utils';+import { getSheetCommandTarget } from './utils/target-util';export interface IStyleTypeValue<T> {type: keyof IStyleData;value: T | T[][];}-interface ISetStyleCommonParams {- subUnitId?: string;- unitId?: string;+interface ISetStyleCommonParams extends Partial<ISheetCommandSharedParams> {range?: IRange;}@@ -72,7 +71,7 @@ export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {handler: async <T> (accessor: IAccessor, params: ISetStyleCommandParams<T>) => {const univerInstanceService = accessor.get(IUniverInstanceService);- const target = getSheetCommandTarget(univerInstanceService);+ const target = getSheetCommandTarget(univerInstanceService, params);if (!target) return false;const { unitId, subUnitId, worksheet } = target;@@ -136,7 +135,7 @@ export const SetStyleCommand: ICommand<ISetStyleCommandParams<unknown>> = {if (setRangeValuesResult && result.result) {undoRedoService.pushUndoRedo({- unitID: unitId,+ unitID: setRangeValuesMutationParams.unitId,undoMutations: [{ id: SetRangeValuesMutation.id, params: undoSetRangeValuesMutationParams }, ...undos],redoMutations: [{ id: SetRangeValuesMutation.id, params: setRangeValuesMutationParams }, ...redos],});@@ -230,7 +229,8 @@ export const SetUnderlineCommand: ICommand = {if (selection.primary) {currentlyUnderline = !!worksheet.getRange(selection.primary.startRow, selection.primary.startColumn)- .getUnderline().s;+ .getUnderline()+ .s;}const setStyleParams: ISetStyleCommandParams<{ s: number }> = {@@ -265,7 +265,8 @@ export const SetStrikeThroughCommand: ICommand = {if (selection.primary) {currentlyStrokeThrough = !!worksheet.getRange(selection.primary.actualRow, selection.primary.actualColumn)- .getStrikeThrough().s;+ .getStrikeThrough()+ .s;}const setStyleParams: ISetStyleCommandParams<{ s: number }> = {@@ -297,7 +298,8 @@ export const SetOverlineCommand: ICommand = {if (selection.primary) {currentlyOverline = !!worksheet.getRange(selection.primary.startRow, selection.primary.startColumn)- .getOverline().s;+ .getOverline()+ .s;}const setStyleParams: ISetStyleCommandParams<{ s: number }> = {