Skip to content

Commit ed0141f

Browse files
author
Chenyun Yang
committed
[FIX] sheet: duplicate sheet name problem when renaming
This commit fixes the problem that when renaming the sheet and the new sheet name is the same as the old one, but the case is different, an error will occur. The sheet name should be case insensitive. When detecting duplicate names, the sheet itself will be excluded. task 3370632 closes #2610 Signed-off-by: Lucas Lefèvre (lul) <lul@odoo.com>
1 parent 869050f commit ed0141f

File tree

5 files changed

+36
-6
lines changed

5 files changed

+36
-6
lines changed

src/helpers/ui/sheet_interactive.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ export function interactiveRenameSheet(
88
name: string,
99
errorCallback: () => void
1010
) {
11-
const originalSheetName = env.model.getters.getSheetName(sheetId);
12-
if (name === null || name === originalSheetName) return;
13-
1411
const result = env.model.dispatch("RENAME_SHEET", { sheetId, name });
1512
if (result.reasons.includes(CommandResult.MissingSheetName)) {
1613
env.raiseError(_lt("The sheet name cannot be empty."), errorCallback);

src/plugins/core/sheet.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,16 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
639639
}
640640

641641
private checkSheetName(cmd: RenameSheetCommand | CreateSheetCommand): CommandResult {
642+
const originalSheetName = this.getters.tryGetSheetName(cmd.sheetId);
643+
if (originalSheetName !== undefined && cmd.name === originalSheetName) {
644+
return CommandResult.UnchangedSheetName;
645+
}
646+
642647
const { orderedSheetIds, sheets } = this;
643648
const name = cmd.name && cmd.name.trim().toLowerCase();
644-
645-
if (orderedSheetIds.find((id) => sheets[id]?.name.toLowerCase() === name)) {
649+
if (
650+
orderedSheetIds.find((id) => sheets[id]?.name.toLowerCase() === name && id !== cmd.sheetId)
651+
) {
646652
return CommandResult.DuplicatedSheetName;
647653
}
648654
if (FORBIDDEN_IN_EXCEL_REGEX.test(name!)) {
@@ -703,8 +709,8 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
703709
const oldName = sheet.name;
704710
this.history.update("sheets", sheet.id, "name", name.trim());
705711
const sheetIdsMapName = Object.assign({}, this.sheetIdsMapName);
706-
sheetIdsMapName[name] = sheet.id;
707712
delete sheetIdsMapName[oldName];
713+
sheetIdsMapName[name] = sheet.id;
708714
this.history.update("sheetIdsMapName", sheetIdsMapName);
709715
}
710716

src/types/commands.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,6 +1096,7 @@ export const enum CommandResult {
10961096
NotEnoughElements,
10971097
NotEnoughSheets,
10981098
MissingSheetName,
1099+
UnchangedSheetName,
10991100
DuplicatedSheetName,
11001101
DuplicatedSheetId,
11011102
ForbiddenCharactersInSheetName,

tests/components/bottom_bar.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,17 @@ describe("BottomBar component", () => {
321321
expect(window.getSelection()?.toString()).toEqual("ThisIsASheet");
322322
expect(document.activeElement).toEqual(sheetName);
323323
});
324+
325+
test("Sheet name is case insensitive", async () => {
326+
const sheetName = fixture.querySelector<HTMLElement>(".o-sheet-name")!;
327+
expect(sheetName.textContent).toEqual("Sheet1");
328+
triggerMouseEvent(sheetName, "dblclick");
329+
await nextTick();
330+
sheetName.textContent = "SHEET1";
331+
await keyDown({ key: "Enter" });
332+
expect(raiseError).not.toHaveBeenCalled();
333+
expect(sheetName.textContent).toEqual("SHEET1");
334+
});
324335
});
325336

326337
test("Can duplicate a sheet", async () => {

tests/plugins/sheets.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,21 @@ describe("sheets", () => {
154154
);
155155
});
156156

157+
test("Rename command won't be dispatched if the name is unchanged (case sensitive)", () => {
158+
const model = new Model({ sheets: [{ id: "11", name: "Sheet1" }] });
159+
expect(renameSheet(model, "11", "Sheet1")).toBeCancelledBecause(
160+
CommandResult.UnchangedSheetName
161+
);
162+
});
163+
164+
test("Can change sheet name case", () => {
165+
const sheetId = "11";
166+
const model = new Model({ sheets: [{ id: sheetId, name: "Sheet1" }] });
167+
expect(model.getters.getSheetName(sheetId)).toBe("Sheet1");
168+
renameSheet(model, "11", "SHEET1");
169+
expect(model.getters.getSheetName(sheetId)).toBe("SHEET1");
170+
});
171+
157172
test("Cannot create a sheet with a position > length of sheets", () => {
158173
const model = new Model();
159174
expect(model.dispatch("CREATE_SHEET", { sheetId: "42", position: 54 })).toBeCancelledBecause(

0 commit comments

Comments
 (0)