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): ensure unique sheet name when inserting with explicit name (#6887)
白熱628bbf91e660
diff --git a/packages/sheets/src/commands/commands/__tests__/insert-sheet.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/insert-sheet.command.spec.tsnew file mode 100644index 0000000000..b79fa0009c--- /dev/null+++ b/packages/sheets/src/commands/commands/__tests__/insert-sheet.command.spec.ts@@ -0,0 +1,116 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * 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 { Injector, Univer, Workbook } from '@univerjs/core';+import { ICommandService, IUniverInstanceService, LocaleService, UniverInstanceType } from '@univerjs/core';+import { afterEach, beforeEach, describe, expect, it } from 'vitest';+import { InsertSheetMutation } from '../../mutations/insert-sheet.mutation';+import { RemoveSheetMutation } from '../../mutations/remove-sheet.mutation';+import { SetWorksheetActiveOperation } from '../../operations/set-worksheet-active.operation';+import { InsertSheetCommand } from '../insert-sheet.command';+import { SetWorksheetActivateCommand } from '../set-worksheet-activate.command';+import { createCommandTestBed } from './create-command-test-bed';++describe('Test insert worksheet commands', () => {+ let univer: Univer;+ let get: Injector['get'];+ let commandService: ICommandService;++ beforeEach(() => {+ const testBed = createCommandTestBed();+ univer = testBed.univer;+ get = testBed.get;++ commandService = get(ICommandService);+ commandService.registerCommand(InsertSheetCommand);+ commandService.registerCommand(InsertSheetMutation);+ commandService.registerCommand(SetWorksheetActivateCommand);+ commandService.registerCommand(SetWorksheetActiveOperation);+ commandService.registerCommand(RemoveSheetMutation);+ });++ afterEach(() => {+ univer.dispose();+ });++ describe('insert sheet', () => {+ it('should auto-generate incremental names when name is not provided', async () => {+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;+ const localeService = get(LocaleService);+ const sheetPrefix = localeService.t('sheets.tabs.sheet');+ expect(workbook.getSheetSize()).toBe(1);++ await commandService.executeCommand(InsertSheetCommand.id, {+ unitId: 'test',+ sheet: { rowCount: 10, columnCount: 10 },+ });+ expect(workbook.getSheetSize()).toBe(2);+ const sheet2 = workbook.getSheets()[1];+ expect(sheet2.getName()).toBe(`${sheetPrefix}1`);++ await commandService.executeCommand(InsertSheetCommand.id, {+ unitId: 'test',+ sheet: { rowCount: 10, columnCount: 10 },+ });+ expect(workbook.getSheetSize()).toBe(3);+ const sheet3 = workbook.getSheets()[2];+ expect(sheet3.getName()).toBe(`${sheetPrefix}2`);+ });++ it('should use provided name directly when it is unique', async () => {+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;++ await commandService.executeCommand(InsertSheetCommand.id, {+ unitId: 'test',+ sheet: { name: 'MySheet', rowCount: 10, columnCount: 10 },+ });++ const newSheet = workbook.getSheets()[1];+ expect(newSheet.getName()).toBe('MySheet');+ });++ it('should deduplicate name when provided name already exists', async () => {+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;++ await commandService.executeCommand(InsertSheetCommand.id, {+ unitId: 'test',+ sheet: { name: 'sheet1', rowCount: 10, columnCount: 10 },+ });++ const newSheet = workbook.getSheets()[1];+ expect(newSheet.getName()).not.toBe('sheet1');+ });++ it('should not be affected by mergeWorksheetSnapshotWithDefault default name', async () => {+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;+ const initialSize = workbook.getSheetSize();++ await commandService.executeCommand(InsertSheetCommand.id, {+ unitId: 'test',+ sheet: { rowCount: 10, columnCount: 10 },+ });+ const firstNewSheet = workbook.getSheets()[initialSize];++ await commandService.executeCommand(InsertSheetCommand.id, {+ unitId: 'test',+ sheet: { rowCount: 10, columnCount: 10 },+ });+ const secondNewSheet = workbook.getSheets()[initialSize + 1];++ expect(firstNewSheet.getName()).not.toBe(secondNewSheet.getName());+ });+ });+});diff --git a/packages/sheets/src/commands/commands/insert-sheet.command.ts b/packages/sheets/src/commands/commands/insert-sheet.command.tsindex e153083325..0c1a8c460a 100644--- a/packages/sheets/src/commands/commands/insert-sheet.command.ts+++ b/packages/sheets/src/commands/commands/insert-sheet.command.ts@@ -54,12 +54,15 @@ export const InsertSheetCommand: ICommand = {let index = workbook.getSheets().length;const sheet = params?.sheet;const sheetId = sheet?.id;+ const sheetName = sheet?.name;const sheetConfig = mergeWorksheetSnapshotWithDefault(sheet || {});if (params) {index = params.index ?? index;sheetConfig.id = sheetId || generateRandomId();- sheetConfig.name = sheet?.name || workbook.generateNewSheetName(`${localeService.t('sheets.tabs.sheet')}`);+ sheetConfig.name = sheetName+ ? workbook.uniqueSheetName(sheetName)+ : workbook.generateNewSheetName(`${localeService.t('sheets.tabs.sheet')}`);} else {sheetConfig.id = generateRandomId();sheetConfig.name = workbook.generateNewSheetName(`${localeService.t('sheets.tabs.sheet')}`);diff --git a/packages/sheets/src/facade/__tests__/f-workbook.spec.ts b/packages/sheets/src/facade/__tests__/f-workbook.spec.tsindex d9493971e4..2dd497afba 100644--- a/packages/sheets/src/facade/__tests__/f-workbook.spec.ts+++ b/packages/sheets/src/facade/__tests__/f-workbook.spec.ts@@ -116,4 +116,41 @@ describe('Test FWorkbook', () => {await activeSpreadsheet.moveActiveSheet(1);expect(worksheet.getIndex()).toBe(1);});++ it('Workbook insertSheet should auto-generate incremental names when name is not provided', () => {+ const workbook = univerAPI.getActiveWorkbook()!;+ const initialCount = workbook.getNumSheets();++ const sheet1 = workbook.insertSheet();+ expect(workbook.getNumSheets()).toBe(initialCount + 1);++ const sheet2 = workbook.insertSheet();+ expect(workbook.getNumSheets()).toBe(initialCount + 2);++ expect(sheet1.getSheetName()).not.toBe(sheet2.getSheetName());+ });++ it('Workbook insertSheet should use provided unique name directly', () => {+ const workbook = univerAPI.getActiveWorkbook()!;+ const sheet = workbook.insertSheet('MyUniqueSheet');+ expect(sheet.getSheetName()).toBe('MyUniqueSheet');+ });++ it('Workbook insertSheet should deduplicate when provided name already exists', () => {+ const workbook = univerAPI.getActiveWorkbook()!;+ const sheet = workbook.insertSheet('sheet1');+ expect(sheet.getSheetName()).not.toBe('sheet1');+ });++ it('Workbook create should use provided unique name directly', () => {+ const workbook = univerAPI.getActiveWorkbook()!;+ const sheet = workbook.create('MyCreatedSheet', 10, 10);+ expect(sheet.getSheetName()).toBe('MyCreatedSheet');+ });++ it('Workbook create should deduplicate when provided name already exists', () => {+ const workbook = univerAPI.getActiveWorkbook()!;+ const sheet = workbook.create('sheet1', 10, 10);+ expect(sheet.getSheetName()).not.toBe('sheet1');+ });});diff --git a/packages/sheets/src/facade/f-workbook.ts b/packages/sheets/src/facade/f-workbook.tsindex 7546c993c5..b6846f305c 100644--- a/packages/sheets/src/facade/f-workbook.ts+++ b/packages/sheets/src/facade/f-workbook.ts@@ -256,7 +256,7 @@ export class FWorkbook extends FBaseInitialable {*/create(name: string, rows: number, columns: number, options?: { index?: number; sheet?: Partial<IWorksheetData> }): FWorksheet {const newSheet: Partial<IWorksheetData> = mergeWorksheetSnapshotWithDefault(Tools.deepClone(options?.sheet ?? {}));- newSheet.name = this._workbook.uniqueSheetName(name);+ newSheet.name = name;newSheet.rowCount = rows;newSheet.columnCount = columns;newSheet.id = options?.sheet?.id;@@ -386,7 +386,11 @@ export class FWorkbook extends FBaseInitialable {*/insertSheet(sheetName?: string, options?: { index?: number; sheet?: Partial<IWorksheetData> }): FWorksheet {const newSheet: Partial<IWorksheetData> = mergeWorksheetSnapshotWithDefault(Tools.deepClone(options?.sheet ?? {}));- newSheet.name = this._workbook.uniqueSheetName(sheetName);+ if (sheetName !== undefined) {+ newSheet.name = sheetName;+ } else {+ delete (newSheet as Partial<IWorksheetData>).name;+ }newSheet.id = options?.sheet?.id;const newSheetIndex = options?.index ?? this._workbook.getSheets().length;