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.

flush

jerryf01ae31763a3
diff --git a/packages/base-sheets/src/Controller/SheetBarController.ts b/packages/base-sheets/src/Controller/SheetBarController.ts
index 3d69f4cb0d..116f7163b8 100644
--- a/packages/base-sheets/src/Controller/SheetBarController.ts
+++ b/packages/base-sheets/src/Controller/SheetBarController.ts
@@ -1,4 +1,4 @@
-import { NameGen, UIObserver } from '@univerjs/core';
+import { UIObserver } from '@univerjs/core';
import { SheetPlugin } from '../SheetPlugin';
export class SheetBarControl {
@@ -21,7 +21,10 @@ export class SheetBarControl {
case 'copySheet': {
const workbook = this._plugin.getContext().getWorkBook();
const activeSheet = workbook.getActiveSheet();
- const copySheet = activeSheet.copy(NameGen.getSheetName());
+ const sheetContex = this._plugin.getContext();
+ const genname = sheetContex.getGenName();
+ const locale = this._plugin.getGlobalContext().getLocale();
+ const copySheet = activeSheet.copy(genname.onlyName(`${activeSheet.getName()} ${locale.get('BaseSheetLocale.CopyName')}`));
if (workbook) {
workbook.insertSheet(workbook.getActiveSheetIndex() + 1, copySheet.getConfig());
}
diff --git a/packages/base-sheets/src/Locale/en.ts b/packages/base-sheets/src/Locale/en.ts
index ff8b4c5632..2f861b60a4 100644
--- a/packages/base-sheets/src/Locale/en.ts
+++ b/packages/base-sheets/src/Locale/en.ts
@@ -1 +1,5 @@
-export default {};
+export default {
+ BaseSheetLocale: {
+ CopyName: 'Copy',
+ },
+};
diff --git a/packages/base-sheets/src/Locale/zh.ts b/packages/base-sheets/src/Locale/zh.ts
index ff8b4c5632..fd996a67fa 100644
--- a/packages/base-sheets/src/Locale/zh.ts
+++ b/packages/base-sheets/src/Locale/zh.ts
@@ -1 +1,5 @@
-export default {};
+export default {
+ BaseSheetLocale: {
+ CopyName: '副本',
+ },
+};
diff --git a/packages/base-sheets/src/SheetPlugin.ts b/packages/base-sheets/src/SheetPlugin.ts
index cf0cbfc797..077a223231 100644
--- a/packages/base-sheets/src/SheetPlugin.ts
+++ b/packages/base-sheets/src/SheetPlugin.ts
@@ -8,6 +8,7 @@ import { RightMenuController, InfoBarController, SheetBarControl, CellEditorCont
import { DEFAULT_SPREADSHEET_PLUGIN_DATA, install, ISheetPluginConfig } from './Basics';
import { FormulaBarController } from './Controller/FormulaBarController';
import { NamedRangeActionExtensionFactory } from './Basics/Register/NamedRangeActionExtension';
+import { en, zh } from './Locale';
/**
* The main sheet base, construct the sheet container and layout, mount the rendering engine
@@ -48,6 +49,10 @@ export class SheetPlugin extends Plugin<SheetPluginObserve, SheetContext> {
initialize(context: SheetContext): void {
this.context = context;
+ this.getGlobalContext().getLocale().load({
+ en,
+ zh,
+ });
install(this);
this.initConfig();
this.initController();
diff --git a/packages/core/src/Basics/SheetContext.ts b/packages/core/src/Basics/SheetContext.ts
index 18f9c6e44f..a688dcf2b1 100644
--- a/packages/core/src/Basics/SheetContext.ts
+++ b/packages/core/src/Basics/SheetContext.ts
@@ -2,7 +2,7 @@ import { Workbook } from '../Sheets/Domain';
import { WorkBookObserverImpl } from './WorkBookObserverImpl';
import { ContextBase } from './ContextBase';
import { Observable } from '../Observer';
-import { PropsFrom } from '../Shared';
+import { GenName, PropsFrom } from '../Shared';
import { WorkBookObserver } from './WorkBookObserver';
import { IWorkbookConfig } from '../Interfaces';
@@ -12,9 +12,12 @@ import { IWorkbookConfig } from '../Interfaces';
export class SheetContext extends ContextBase {
protected _workbook: Workbook;
+ protected _genname: GenName;
+
constructor(univerSheetData: Partial<IWorkbookConfig> = {}) {
super();
this._setObserver();
+ this._genname = new GenName();
this._workbook = new Workbook(univerSheetData, this);
}
@@ -22,6 +25,10 @@ export class SheetContext extends ContextBase {
return this._workbook;
}
+ getGenName(): GenName {
+ return this._genname;
+ }
+
getContextObserver<Key extends keyof WorkBookObserver>(
value: Key
): Observable<PropsFrom<WorkBookObserver[Key]>> {
diff --git a/packages/core/src/Shared/NameGen.ts b/packages/core/src/Shared/NameGen.ts
index 691691124b..a8d058c322 100644
--- a/packages/core/src/Shared/NameGen.ts
+++ b/packages/core/src/Shared/NameGen.ts
@@ -28,3 +28,39 @@ export class NameGen {
return checked;
}
}
+
+export class GenName {
+ private _include: string[];
+
+ private _count: number;
+
+ constructor() {
+ this._include = [];
+ this._count = 1;
+ }
+
+ checked(name: string): boolean {
+ return this._include.includes(name);
+ }
+
+ onlyName(name: string): string {
+ let output = name;
+ let count = 1;
+ while (this.checked(output)) {
+ output = name + count;
+ count++;
+ }
+ this._include.push(output);
+ return output;
+ }
+
+ sheetName(name: string = 'sheet1'): string {
+ let output = name;
+ while (this.checked(output)) {
+ output = `sheet${this._count}`;
+ this._count++;
+ }
+ this._include.push(output);
+ return output;
+ }
+}
diff --git a/packages/core/src/Shared/index.ts b/packages/core/src/Shared/index.ts
index c6dfb4ae38..7008ed6c6b 100644
--- a/packages/core/src/Shared/index.ts
+++ b/packages/core/src/Shared/index.ts
@@ -11,9 +11,9 @@ export * from './IOSocket';
export * from './KeyboardUtil';
export * from './Locale';
export * from './Logger';
+export * from './NameGen';
export * from './LRU/index';
export * from './Migrate/index';
-export * from './NameGen';
export * from './ObjectArray';
export * from './ObjectMatrix';
export * from './Rectangle';
diff --git a/packages/core/src/Sheets/Domain/Workbook.ts b/packages/core/src/Sheets/Domain/Workbook.ts
index fb2595e181..4813c3deb1 100644
--- a/packages/core/src/Sheets/Domain/Workbook.ts
+++ b/packages/core/src/Sheets/Domain/Workbook.ts
@@ -27,7 +27,7 @@ import {
IWorksheetConfig,
} from '../../Interfaces';
-import { NameGen, Nullable, Tools, Tuples } from '../../Shared';
+import { Nullable, Tools, Tuples } from '../../Shared';
import { RangeList } from './RangeList';
import { Selection } from './Selection';
import { Styles } from './Styles';
@@ -157,6 +157,7 @@ export class Workbook {
insertSheet(index: number, sheet: Worksheet): Nullable<string>;
insertSheet(...argument: any[]): Nullable<string> {
const { _context } = this;
+ const genname = _context.getGenName();
const commandManager = this.getCommandManager();
const before = _context.getContextObserver('onBeforeInsertSheetObservable');
const after = _context.getContextObserver('onAfterInsertSheetObservable');
@@ -164,7 +165,7 @@ export class Workbook {
// insert empty sheet
if (Tools.hasLength(argument, 0)) {
const worksheetConfig = {
- name: NameGen.getSheetName(),
+ name: genname.sheetName(),
status: 0,
id: Tools.generateRandomId(6),
};
@@ -228,7 +229,7 @@ export class Workbook {
if (Tools.isNumber(argument[0])) {
const index = argument[0];
const worksheetConfig = {
- name: NameGen.getSheetName(),
+ name: genname.sheetName(),
status: 0,
id: Tools.generateRandomId(6),
};
@@ -798,6 +799,7 @@ export class Workbook {
private _getDefaultWorkSheet(): void {
const { _context, _config, _worksheets } = this;
const { sheets, sheetOrder } = _config;
+ const genname = _context.getGenName();
// One worksheet by default
if (Tools.isEmptyObject(sheets)) {
@@ -810,7 +812,7 @@ export class Workbook {
for (let sheetId in sheets) {
let config = sheets[sheetId];
- config.name = NameGen.getSheetName(config.name);
+ config.name = genname.sheetName(config.name);
const worksheet = new Worksheet(_context, config);
_worksheets.set(sheetId, worksheet);
if (!sheetOrder.includes(sheetId)) {
diff --git a/packages/core/test/Shared/NameGen.test.ts b/packages/core/test/Shared/NameGen.test.ts
index 354ef8c89d..e69de29bb2 100644
--- a/packages/core/test/Shared/NameGen.test.ts
+++ b/packages/core/test/Shared/NameGen.test.ts
@@ -1,8 +0,0 @@
-import { NameGen } from '../../src';
-
-jest.mock('nanoid', () => ({ nanoid: () => '12345678' }));
-
-test('NameGen getSheetName', () => {
- const name = NameGen.getSheetName();
- expect(name).not.toBeUndefined();
-});