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(formula): fix formula sheet name uppercase bug (#2908)

wpxp123456b4122b163557
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 75d2e9a34d..b31b33b09d 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
@@ -15,7 +15,7 @@
*/
import type { ICellData, IDocumentData, Injector, Univer, Workbook } from '@univerjs/core';
-import { CellValueType, IContextService, IResourceLoaderService, LocaleService } from '@univerjs/core';
+import { CellValueType, IContextService, IResourceLoaderService, LocaleService, LocaleType } from '@univerjs/core';
import { LexerTreeBuilder } from '@univerjs/engine-formula';
import { SpreadsheetSkeleton } from '@univerjs/engine-render';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
@@ -130,7 +130,7 @@ describe('Test EndEditController', () => {
normalizeStringByLexer = (str: string) => {
// @ts-ignore
- return normalizeString(str, lexerTreeBuilder, get(IMockFunctionService));
+ return normalizeString(str, lexerTreeBuilder, LocaleType.ZH_CN, get(IMockFunctionService));
};
});
@@ -235,6 +235,7 @@ describe('Test EndEditController', () => {
// formula
expect(normalizeStringByLexer('=w')).toEqual('=w');
expect(normalizeStringByLexer('=a1')).toEqual('=A1');
+ expect(normalizeStringByLexer('=tan(sheet001!a1')).toEqual('=TAN(sheet001!A1');
expect(normalizeStringByLexer('="1"')).toEqual('="1"');
expect(normalizeStringByLexer('='1'')).toEqual("='1'"); // invalid in Excel
diff --git a/packages/sheets-ui/src/controllers/editor/__tests__/mock-function.service.ts b/packages/sheets-ui/src/controllers/editor/__tests__/mock-function.service.ts
index ef3b7add54..d725bb751d 100644
--- a/packages/sheets-ui/src/controllers/editor/__tests__/mock-function.service.ts
+++ b/packages/sheets-ui/src/controllers/editor/__tests__/mock-function.service.ts
@@ -22,6 +22,7 @@ export class MockFunctionService {
getDescriptions(): Map<IFunctionNames, IFunctionInfo> {
const map = new Map<IFunctionNames, IFunctionInfo>();
map.set('IF', { functionName: 'IF', functionType: FunctionType.Logical, description: 'IF function', abstract: 'IF function', functionParameter: [] });
+ map.set('TAN', { functionName: 'TAN', functionType: FunctionType.Logical, description: 'TAN function', abstract: 'TAN function', functionParameter: [] });
return map;
}
diff --git a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
index 9f452f5184..8ccb50d163 100644
--- a/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
+++ b/packages/sheets-ui/src/controllers/editor/editing.render-controller.ts
@@ -76,7 +76,7 @@ import styles from '../../views/sheet-container/index.module.less';
import { MoveSelectionCommand, MoveSelectionEnterAndTabCommand } from '../../commands/commands/set-selection.command';
import { MOVE_SELECTION_KEYCODE_LIST } from '../shortcuts/editor.shortcut';
import { extractStringFromForceString, isForceString } from '../utils/cell-tools';
-import { isCJKLocale, normalizeString } from '../utils/char-tools';
+import { normalizeString } from '../utils/char-tools';
const HIDDEN_EDITOR_POSITION = -1000;
@@ -1042,9 +1042,7 @@ export function getCellDataByInput(
let newDataStream = lastString === DEFAULT_EMPTY_DOCUMENT_VALUE ? data.substring(0, data.length - 2) : data;
const currentLocale = localeService.getCurrentLocale();
- if (isCJKLocale(currentLocale)) {
- newDataStream = normalizeString(newDataStream, lexerTreeBuilder, functionService);
- }
+ newDataStream = normalizeString(newDataStream, lexerTreeBuilder, currentLocale, functionService);
if (isFormulaString(newDataStream)) {
if (cellData.f === newDataStream) {
diff --git a/packages/sheets-ui/src/controllers/utils/char-tools.ts b/packages/sheets-ui/src/controllers/utils/char-tools.ts
index eff5bcc3ed..dd201858b2 100644
--- a/packages/sheets-ui/src/controllers/utils/char-tools.ts
+++ b/packages/sheets-ui/src/controllers/utils/char-tools.ts
@@ -69,16 +69,20 @@ export function isCJKLocale(locale: LocaleType) {
* @param lexerTreeBuilder
* @returns
*/
-export function normalizeString(str: string, lexerTreeBuilder: LexerTreeBuilder, functionService: IFunctionService) {
- // Check if it is a mandatory string with single quotes
- if (str.startsWith(''') || str.startsWith("'")) {
- return `'${str.slice(1)}`;
- }
+export function normalizeString(str: string, lexerTreeBuilder: LexerTreeBuilder, currentLocale: LocaleType, functionService: IFunctionService) {
+ let normalStr = str;
+
+ if (isCJKLocale(currentLocale)) {
+ // Check if it is a mandatory string with single quotes
+ if (str.startsWith(''') || str.startsWith("'")) {
+ return `'${str.slice(1)}`;
+ }
- // covert all full width characters to normal characters
- const normalStr = str.split('').map(toHalfWidth).join('');
+ // covert all full width characters to normal characters
+ normalStr = str.split('').map(toHalfWidth).join('');
+ }
- // Check if it is a formula
+ // Check if it is a formula
if (normalStr.startsWith('=')) {
return normalizeFormulaString(str, normalStr, lexerTreeBuilder, functionService);
}
@@ -100,32 +104,47 @@ function normalizeFormulaString(str: string, normalStr: string, lexerTreeBuilder
if (!nodes) return str;
+ let _normalStr = normalStr;
+
nodes.forEach((node, index) => {
if (typeof node === 'object') {
const token = node.token;
- const lowerCaseToken = token.toLowerCase();
// boolean or function name
- if (booleanMap[lowerCaseToken] || (node.nodeType === sequenceNodeType.FUNCTION && hasFunctionName(token, functionService, nodes, index)) || node.nodeType === sequenceNodeType.REFERENCE) {
+ if (booleanMap[token.toLowerCase()]) {
const startIndex = node.startIndex + 1;
const endIndex = node.endIndex + 2;
- normalStr = replaceString(token.toLocaleUpperCase(), normalStr, startIndex, endIndex);
+ _normalStr = replaceString(token.toLocaleUpperCase(), _normalStr, startIndex, endIndex);
+ } else if ((node.nodeType === sequenceNodeType.FUNCTION && hasFunctionName(token, functionService, nodes, index)) || node.nodeType === sequenceNodeType.REFERENCE) {
+ const sheetNameIndex = token.indexOf('!');
+
+ if (sheetNameIndex > -1) {
+ const refBody = token.substring(sheetNameIndex + 1);
+ const startIndex = node.startIndex + (sheetNameIndex + 1) + 1;
+ const endIndex = node.endIndex + 2;
+ _normalStr = replaceString(refBody.toLocaleUpperCase(), _normalStr, startIndex, endIndex);
+ } else {
+ const startIndex = node.startIndex + 1;
+ const endIndex = node.endIndex + 2;
+ _normalStr = replaceString(token.toLocaleUpperCase(), _normalStr, startIndex, endIndex);
+ }
} else if ((token.startsWith('"') && token.endsWith('"')) || (token.startsWith("'") && token.endsWith("'"))) {
const startIndex = node.startIndex + 2;
const endIndex = node.endIndex + 1;
- normalStr = replaceString(str.slice(startIndex, endIndex), normalStr, startIndex, endIndex);
+ _normalStr = replaceString(str.slice(startIndex, endIndex), _normalStr, startIndex, endIndex);
} else if (node.nodeType !== sequenceNodeType.ARRAY) {
const parsedValue = numfmt.parseValue(token);
if (parsedValue == null) {
const startIndex = node.startIndex + 1;
const endIndex = node.endIndex + 2;
- normalStr = replaceString(str.slice(startIndex, endIndex), normalStr, startIndex, endIndex);
+ _normalStr = replaceString(str.slice(startIndex, endIndex), _normalStr, startIndex, endIndex);
}
}
}
});
- return normalStr;
+
+ return _normalStr;
}
function hasFunctionName(name: string, functionService: IFunctionService, nodes: (string | ISequenceNode)[], index: number) {