Skip to content

Commit

Permalink
fix(sheet): sheet name normalize string (#4172)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dushusir authored Nov 30, 2024
1 parent 244511d commit d9b1149
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ describe('Test Reference', () => {
"Sheet'",
'!Sheet',
'!Sheet',
'Sheet1(副本)',
'Sheet4(Copy)',
];
const testFalseCase = ['Sheet1', '工作表1'];

Expand Down
2 changes: 1 addition & 1 deletion packages/engine-formula/src/engine/utils/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand All @@ -78,6 +79,8 @@ export class FormulaAlertRenderController extends Disposable implements IRenderM
const errorType = extractFormulaError(cellData, !!arrayFormulaCellData);

if (!errorType) {
// fix #4002
this._hideAlert();
return;
}

Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
27 changes: 25 additions & 2 deletions packages/sheets-ui/src/controllers/utils/char-tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);

Expand Down

0 comments on commit d9b1149

Please sign in to comment.