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: fix case sensitivity was not ignored when checking for duplicate defined names, table names and sheet names (#6330)
wpxp123456d044bd9426a5
diff --git a/packages/core/src/sheets/workbook.ts b/packages/core/src/sheets/workbook.tsindex 3bf7ffddf5..93f95d520c 100644--- a/packages/core/src/sheets/workbook.ts+++ b/packages/core/src/sheets/workbook.ts@@ -394,13 +394,13 @@ export class Workbook extends UnitModel<IWorkbookData, UniverInstanceType.UNIVER}/**- * Check if sheet name is unique+ * Check if sheet name is unique, ignore case sensitivity* @param name sheet name* @returns True if sheet name is unique*/checkSheetName(name: string): boolean {const sheetsName = this.getSheetsName();- return sheetsName.includes(name);+ return sheetsName.some((sheetName) => sheetName.toLowerCase() === name.toLowerCase());}/**diff --git a/packages/engine-formula/src/services/defined-names.service.ts b/packages/engine-formula/src/services/defined-names.service.tsindex 496bd64986..98c355548d 100644--- a/packages/engine-formula/src/services/defined-names.service.ts+++ b/packages/engine-formula/src/services/defined-names.service.ts@@ -81,7 +81,8 @@ export interface IDefinedNamesService {export class DefinedNamesService extends Disposable implements IDefinedNamesService {// 18.2.6 definedNames (Defined Names)private _definedNameMap: IDefinedNameMap = {};- private _nameCacheMap: { [unitId: string]: { [name: string]: IDefinedNamesServiceParam } } = {}; // Cache for name-to-definition mapping+ // Cache for name-to-definition mapping, here name key is ignored case sensitivity+ private _nameCacheMap: { [unitId: string]: { [name: string]: IDefinedNamesServiceParam } } = {};private readonly _update$ = new Subject();readonly update$ = this._update$.asObservable();@@ -181,7 +182,7 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServ// Check cache firstconst cachedMap = this._nameCacheMap[unitId];if (cachedMap) {- return cachedMap[name] || null;+ return cachedMap[name.toLowerCase()] || null;}// If not in cache, traverse the nameMap@@ -201,7 +202,7 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServ// Cache the result if foundif (result) {this._nameCacheMap[unitId] = this._nameCacheMap[unitId] || {};- this._nameCacheMap[unitId][name] = result;+ this._nameCacheMap[unitId][name.toLowerCase()] = result;}return result;@@ -248,7 +249,7 @@ export class DefinedNamesService extends Disposable implements IDefinedNamesServ// Cache all name mappings for this unitIdfor (const item of Object.values(nameMap)) {- this._nameCacheMap[unitId][item.name] = item;+ this._nameCacheMap[unitId][item.name.toLowerCase()] = item;}}}diff --git a/packages/engine-formula/src/services/super-table.service.ts b/packages/engine-formula/src/services/super-table.service.tsindex 53fcd448dd..fa29a539c2 100644--- a/packages/engine-formula/src/services/super-table.service.ts+++ b/packages/engine-formula/src/services/super-table.service.ts@@ -41,6 +41,8 @@ export interface ISuperTableService {update$: Observable<unknown>;getTable(unitId: string, tableName: string): Nullable<ISuperTable>;++ hasTable(unitId: string, tableName: string): boolean;}export class SuperTableService extends Disposable implements ISuperTableService {@@ -98,6 +100,15 @@ export class SuperTableService extends Disposable implements ISuperTableServicereturn this._tableMap.get(unitId)?.get(tableName);}+ hasTable(unitId: string, tableName: string): boolean {+ const unitIdMap = this._tableMap.get(unitId);+ if (unitIdMap) {+ return unitIdMap.keys().some((name) => name.toLowerCase() === tableName.toLowerCase());+ }++ return false;+ }+private _update() {this._update$.next(null);}diff --git a/packages/sheets-table/src/commands/commands/add-sheet-table.command.ts b/packages/sheets-table/src/commands/commands/add-sheet-table.command.tsindex 06ed9c2668..49ea9d5f54 100644--- a/packages/sheets-table/src/commands/commands/add-sheet-table.command.ts+++ b/packages/sheets-table/src/commands/commands/add-sheet-table.command.ts@@ -52,12 +52,12 @@ export const AddSheetTableCommand: ICommand<IAddSheetTableCommandParams> = {});let tableName = params.name;- if (!tableName || !customNameCharacterCheck(tableName, existingNamesSet)) {+ if (!tableName || !customNameCharacterCheck(tableName.toLowerCase(), existingNamesSet)) {const prefix = localeService.t('sheets-table.tablePrefix');let index = tableManager.getTableList(params.unitId).length + 1;for (const name of existingNamesSet) {- if (name.startsWith(prefix)) {+ if (name.startsWith(prefix.toLowerCase())) {const n = Number(name.slice(prefix.length));if (Number.isInteger(n) && n >= index) {index = n + 1;diff --git a/packages/sheets-table/src/commands/commands/set-sheet-table.command.ts b/packages/sheets-table/src/commands/commands/set-sheet-table.command.tsindex 6e3621b2d2..cc912f4533 100644--- a/packages/sheets-table/src/commands/commands/set-sheet-table.command.ts+++ b/packages/sheets-table/src/commands/commands/set-sheet-table.command.ts@@ -54,7 +54,7 @@ export const SetSheetTableCommand: ICommand<ISetSheetTableCommandParams> = {});if (name) {- const isValidName = customNameCharacterCheck(name, existingNamesSet);+ const isValidName = customNameCharacterCheck(name.toLowerCase(), existingNamesSet);if (!isValidName) {const logService = accessor.get(ILogService);logService.warn(localeService.t('sheets-table.tableNameError'));diff --git a/packages/sheets-table/src/util.ts b/packages/sheets-table/src/util.tsindex 10a0ea1a9a..9aabddd44b 100644--- a/packages/sheets-table/src/util.ts+++ b/packages/sheets-table/src/util.ts@@ -109,13 +109,14 @@ export function getExistingNamesSet(unitId: string, options: {definedNamesService?: IDefinedNamesService;}): Set<string> {const { univerInstanceService, tableManager, definedNamesService } = options;+ // The set to store existing names, case insensitiveconst existingNamesSet = new Set<string>();// The table names can't be duplicate with existing sheet names.const workbook = univerInstanceService?.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET);if (workbook) {workbook.getSheets().forEach((sheet) => {- existingNamesSet.add(sheet.getName());+ existingNamesSet.add(sheet.getName().toLowerCase());});}@@ -123,7 +124,7 @@ export function getExistingNamesSet(unitId: string, options: {const tableList = tableManager?.getTableList(unitId);if (tableList && tableList.length > 0) {tableList.forEach((tableItem) => {- existingNamesSet.add(tableItem.name);+ existingNamesSet.add(tableItem.name.toLowerCase());});}@@ -131,7 +132,7 @@ export function getExistingNamesSet(unitId: string, options: {const definedNames = definedNamesService?.getDefinedNameMap(unitId);if (definedNames) {Object.values(definedNames).forEach((definedName) => {- existingNamesSet.add(definedName.name);+ existingNamesSet.add(definedName.name.toLowerCase());});}diff --git a/packages/sheets-ui/src/views/defined-name/DefinedNameInput.tsx b/packages/sheets-ui/src/views/defined-name/DefinedNameInput.tsxindex 96a9f8b634..6b945d23ef 100644--- a/packages/sheets-ui/src/views/defined-name/DefinedNameInput.tsx+++ b/packages/sheets-ui/src/views/defined-name/DefinedNameInput.tsx@@ -137,7 +137,7 @@ export const DefinedNameInput = (props: IDefinedNameInputProps) => {// The defined name can't be duplicate with existing defined names and table names.if (- (definedNamesService.getValueByName(unitId, nameValue) || superTableService.getTableMap(unitId)?.has(nameValue)) &&+ (definedNamesService.getValueByName(unitId, nameValue) || superTableService.hasTable(unitId, nameValue)) &&(id === null || id === undefined || id.length === 0)) {setValidString(localeService.t('definedName.nameDuplicate'));