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(table): fix table name error (#5874)

VicKun79266070fc35
diff --git a/packages/core/src/shared/name.ts b/packages/core/src/shared/name.ts
index f79d583ffc..3065758b30 100644
--- a/packages/core/src/shared/name.ts
+++ b/packages/core/src/shared/name.ts
@@ -53,33 +53,62 @@ export function nameCharacterCheck(name: string) {
* Checks if the custom table name or custom name is valid based on the following criteria:
* - The name cannot conflict with existing sheet names.
* - The name cannot be empty.
- * - The name must start with a letter or underscore.
+ * - The name must start with a letter or underscore or a unicode letter (e.g., Chinese).
+ * - The name cannot be a simple A1-style reference (e.g., A1, $A$1, AB123).
+ * - The name cannot be an R1C1-style reference (e.g., R1C1).
+ * - The name cannot be purely numeric.
+ * - The name cannot exceed 255 characters in length.
* The name cannot contain spaces or invalid characters (e.g., :, \, /, ?, *, [, ]).
* @param name The name need to check
* @param sheetNameSet The set of existing sheet names to check against
* @returns {boolean} Returns true if the name is valid, false otherwise.
*/
export function customNameCharacterCheck(name: string, sheetNameSet: Set<string>): boolean {
- // Custom table name cannot conflict with existing sheet names
- if (sheetNameSet.has(name)) {
+ // not empty
+ if (!name || name.length === 0) {
return false;
}
- // Custom table name cannot be empty
- if (name.length === 0) {
+ // Excel Define Name maximum length (use 255 to be safe)
+ if (name.length > 255) {
return false;
}
- // Custom table name must start with a letter or underscore
- const validStart = /^[A-Za-z_]/;
- if (!validStart.test(name)) {
+ // cannot conflict with existing sheet names
+ if (sheetNameSet.has(name)) {
return false;
}
- // Custom table name cannot contain spaces or invalid characters
+
+ // cannot contain spaces or invalid chars : \ / ? * [ ]
const invalidChars = /[ :\\\/\?\*\[\]]/;
if (invalidChars.test(name)) {
return false;
}
+ // must start with a letter (including unicode letters, e.g. Chinese) or underscore
+ // use Unicode property escape \p{L} with 'u' flag to allow non-latin letters
+ const validStart = /^[\p{L}_]/u;
+ if (!validStart.test(name)) {
+ return false;
+ }
+
+ // cannot be a simple A1-style reference (e.g. A1, $A$1, AB123)
+ // column letters limited to reasonable length (1-3) and then numbers
+ const a1Ref = /^\$?[A-Za-z]{1,3}\$?[0-9]+$/;
+ if (a1Ref.test(name)) {
+ return false;
+ }
+
+ // cannot be R1C1 style reference like R1C1
+ const r1c1Ref = /^[rR]\d+[cC]\d+$/;
+ if (r1c1Ref.test(name)) {
+ return false;
+ }
+
+ // cannot be purely numeric
+ if (/^\d+$/.test(name)) {
+ return false;
+ }
+
return true;
}
diff --git a/packages/sheets-table/src/commands/commands/set-sheet-table.command.ts b/packages/sheets-table/src/commands/commands/set-sheet-table.command.ts
index dcb765392b..518a9f4879 100644
--- a/packages/sheets-table/src/commands/commands/set-sheet-table.command.ts
+++ b/packages/sheets-table/src/commands/commands/set-sheet-table.command.ts
@@ -55,6 +55,12 @@ export const SetSheetTableCommand: ICommand<ISetSheetTableCommandParams> = {
workbook.getSheets().forEach((sheet) => {
sheetNameSet.add(sheet.getName());
});
+
+ // Add existing table names to the set to ensure uniqueness
+ const tableList = tableManager.getTableList(unitId);
+ tableList.forEach((tableItem) => {
+ sheetNameSet.add(tableItem.name);
+ });
}
if (name) {
diff --git a/packages/sheets-table/src/model/table-manager.ts b/packages/sheets-table/src/model/table-manager.ts
index 5efd2e9e63..364bb07d31 100644
--- a/packages/sheets-table/src/model/table-manager.ts
+++ b/packages/sheets-table/src/model/table-manager.ts
@@ -81,6 +81,8 @@ export class TableManager extends Disposable {
* @param {string} subUnitId The subunit id of the table.
* @param {string} name The table name, it should be unique in the unit or it will be appended with a number.
* @param {ITableRange} range The range of the table, it contains the unit id and subunit id.
+ * @param {string[]} [header] The header of the table, if not provided, it will be generated based on the range.
+ * @param {string} [initId] The initial id of the table, if not provided, a random id will be generated.
* @param {ITableOptions} [options] Other options of the table.
* @returns {string} The table id.
*/