Skip to content

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.

fix(sheet): sheet name normalize string (#4172)

Dushusird9b1149ffcdb
diff --git a/packages/engine-formula/src/engine/utils/__tests__/reference.spec.ts b/packages/engine-formula/src/engine/utils/__tests__/reference.spec.ts
index 0e8b22b6f3..5d225aefcd 100644
--- a/packages/engine-formula/src/engine/utils/__tests__/reference.spec.ts
+++ b/packages/engine-formula/src/engine/utils/__tests__/reference.spec.ts
@@ -262,6 +262,8 @@ describe('Test Reference', () => {
"Sheet'",
'!Sheet',
'!Sheet',
+ 'Sheet1(副本)',
+ 'Sheet4(Copy)',
];
const testFalseCase = ['Sheet1', '工作表1'];
diff --git a/packages/engine-formula/src/engine/utils/reference.ts b/packages/engine-formula/src/engine/utils/reference.ts
index cddf3bff9f..31d0b06e51 100644
--- a/packages/engine-formula/src/engine/utils/reference.ts
+++ b/packages/engine-formula/src/engine/utils/reference.ts
@@ -419,7 +419,7 @@ export function needsQuoting(name: string) {
// Check for spaces, punctuation and special characters
- if (/[\s!$%^&*()+\-=\[\]{};':"\\|,.<>\/?]/.test(name)) {
+ if (/[\s!$%^&*()+\-=\[\]{};':"\\|,.<>\/?()]/.test(name)) {
return true;
}
diff --git a/packages/sheets-formula-ui/src/controllers/formula-alert-render.controller.ts b/packages/sheets-formula-ui/src/controllers/formula-alert-render.controller.ts
index 0e087f951b..666e8ade89 100644
--- a/packages/sheets-formula-ui/src/controllers/formula-alert-render.controller.ts
+++ b/packages/sheets-formula-ui/src/controllers/formula-alert-render.controller.ts
@@ -20,6 +20,7 @@ import { Disposable, Inject, isICellData, LocaleService } from '@univerjs/core';
import { ErrorType, FormulaDataModel } from '@univerjs/engine-formula';
import { CellAlertManagerService, CellAlertType, HoverManagerService } from '@univerjs/sheets-ui';
import { IZenZoneService } from '@univerjs/ui';
+import { debounceTime } from 'rxjs';
import { extractFormulaError } from './utils/utils';
const ALERT_KEY = 'SHEET_FORMULA_ALERT';
@@ -58,7 +59,7 @@ export class FormulaAlertRenderController extends Disposable implements IRenderM
}
private _initCellAlertPopup() {
- this.disposeWithMe(this._hoverManagerService.currentCell$.subscribe((cellPos) => {
+ this.disposeWithMe(this._hoverManagerService.currentCell$.pipe(debounceTime(100)).subscribe((cellPos) => {
if (cellPos) {
const workbook = this._context.unit;
const worksheet = workbook.getActiveSheet();
@@ -78,6 +79,8 @@ export class FormulaAlertRenderController extends Disposable implements IRenderM
const errorType = extractFormulaError(cellData, !!arrayFormulaCellData);
if (!errorType) {
+ // fix #4002
+ this._hideAlert();
return;
}
@@ -106,15 +109,19 @@ export class FormulaAlertRenderController extends Disposable implements IRenderM
}
}
- this._cellAlertManagerService.removeAlert(ALERT_KEY);
+ this._hideAlert();
}));
}
private _initZenService() {
this.disposeWithMe(this._zenZoneService.visible$.subscribe((visible) => {
if (visible) {
- this._cellAlertManagerService.removeAlert(ALERT_KEY);
+ this._hideAlert();
}
}));
}
+
+ private _hideAlert() {
+ this._cellAlertManagerService.removeAlert(ALERT_KEY);
+ }
}
diff --git a/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts b/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
index 00eff87ce8..73f3b5c78a 100644
--- a/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
+++ b/packages/sheets-ui/src/controllers/editor/__tests__/end-edit.controller.spec.ts
@@ -395,6 +395,9 @@ describe('Test EndEditController', () => {
expect(normalizeStringByLexer('$w')).toEqual('$w');
expect(normalizeStringByLexer('true+1')).toEqual('true+1');
+ // sheet name
+ expect(normalizeStringByLexer("='Sheet1(副本)'!F20:H29")).toEqual("='Sheet1(副本)'!F20:H29");
+
// TODO@Dushusir: Differences from Excel, pending,
// '=@@if@s'
// '=@@if+@s'
diff --git a/packages/sheets-ui/src/controllers/utils/char-tools.ts b/packages/sheets-ui/src/controllers/utils/char-tools.ts
index ad6211e63d..567b258d06 100644
--- a/packages/sheets-ui/src/controllers/utils/char-tools.ts
+++ b/packages/sheets-ui/src/controllers/utils/char-tools.ts
@@ -77,8 +77,8 @@ export function normalizeString(str: string, lexerTreeBuilder: LexerTreeBuilder,
return `'${str.slice(1)}`;
}
- // covert all full width characters to normal characters
- normalStr = str.split('').map(toHalfWidth).join('');
+ // Handle quoted parts separately (e.g., sheet names)
+ normalStr = handleQuotedParts(normalStr);
}
// Check if it is a formula
@@ -98,6 +98,29 @@ export function normalizeString(str: string, lexerTreeBuilder: LexerTreeBuilder,
return parsedValue == null ? str : normalStr;
}
+// Helper function to handle sheet names with quotes
+function handleQuotedParts(str: string): string {
+ const sheetNamePattern = /['"].*?['"]/g; // Pattern to match quoted parts (sheet names)
+ const quotedParts: string[] = [];
+ const parts = str.split(sheetNamePattern);
+
+ // Save quoted parts and remove them from the string temporarily
+ str.replace(sheetNamePattern, (match) => {
+ quotedParts.push(match);
+ return ''; // Remove quoted parts for now
+ });
+
+ // Convert all full-width characters to half-width characters in non-quoted parts
+ let normalStr = parts.join('').split('').map(toHalfWidth).join('');
+
+ // Reinserting the quoted parts into the final string
+ quotedParts.forEach((part, idx) => {
+ normalStr = normalStr.slice(0, idx * 2) + part + normalStr.slice((idx + 1) * 2);
+ });
+
+ return normalStr;
+}
+
function normalizeFormulaString(str: string, normalStr: string, lexerTreeBuilder: LexerTreeBuilder, functionService: IFunctionService) {
const nodes = lexerTreeBuilder.sequenceNodesBuilder(normalStr);