fix: fix case sensitivity was not ignored when checking for duplicate defined names, table names and sheet names (#6330)

This commit is contained in:
wpxp123456
2025-12-17 20:34:47 +08:00
committed by GitHub
parent 21b2d38fbd
commit d044bd9426
7 changed files with 26 additions and 13 deletions
+2 -2
View File
@@ -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());
}
/**
@@ -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 first
const 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 found
if (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 unitId
for (const item of Object.values(nameMap)) {
this._nameCacheMap[unitId][item.name] = item;
this._nameCacheMap[unitId][item.name.toLowerCase()] = item;
}
}
}
@@ -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 ISuperTableService
return 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);
}
@@ -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;
@@ -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'));
+4 -3
View File
@@ -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 insensitive
const 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());
});
}
@@ -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'));