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(sheets): fix shallow copy bugs of the sheet snapshot (#1742)

karl83d910c328e9
diff --git a/packages/core/src/docs/data-model/document-data-model.ts b/packages/core/src/docs/data-model/document-data-model.ts
index 002c3e180e..6dac3f6709 100644
--- a/packages/core/src/docs/data-model/document-data-model.ts
+++ b/packages/core/src/docs/data-model/document-data-model.ts
@@ -207,10 +207,7 @@ export class DocumentDataModel extends DocumentDataModelSimple {
footerModelMap: Map<string, DocumentDataModel> = new Map();
constructor(snapshot: Partial<IDocumentData>) {
- if (Tools.isEmptyObject(snapshot)) {
- snapshot = getEmptySnapshot();
- }
- super(snapshot);
+ super(Tools.isEmptyObject(snapshot) ? getEmptySnapshot() : snapshot);
const UNIT_ID_LENGTH = 6;
diff --git a/packages/core/src/sheets/empty-snapshot.ts b/packages/core/src/sheets/empty-snapshot.ts
new file mode 100644
index 0000000000..64b80db686
--- /dev/null
+++ b/packages/core/src/sheets/empty-snapshot.ts
@@ -0,0 +1,38 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { LocaleType } from '../types/enum/locale-type';
+import type { IWorkbookData } from '../types/interfaces';
+import { version } from '../../package.json';
+
+export function getEmptySnapshot(
+ unitID = '',
+ locale = LocaleType.ZH_CN,
+ name = ''
+): IWorkbookData {
+ const DEFAULT_WORKBOOK_DATA: IWorkbookData = {
+ id: unitID,
+ sheetOrder: [],
+ name,
+ appVersion: version,
+ locale,
+ styles: {},
+ sheets: {},
+ resources: [],
+ };
+
+ return DEFAULT_WORKBOOK_DATA;
+}
diff --git a/packages/core/src/sheets/workbook.ts b/packages/core/src/sheets/workbook.ts
index d5cf25e5bf..51ab7a90ef 100644
--- a/packages/core/src/sheets/workbook.ts
+++ b/packages/core/src/sheets/workbook.ts
@@ -20,7 +20,7 @@ import { ILogService } from '../services/log/log.service';
import type { Nullable } from '../shared';
import { Tools } from '../shared';
import { Disposable } from '../shared/lifecycle';
-import { DEFAULT_RANGE_ARRAY, DEFAULT_WORKBOOK } from '../types/const';
+import { DEFAULT_RANGE_ARRAY } from '../types/const';
import { BooleanNumber } from '../types/enum';
import type {
IColumnStartEndData,
@@ -34,6 +34,7 @@ import type {
} from '../types/interfaces';
import { Styles } from './styles';
import { Worksheet } from './worksheet';
+import { getEmptySnapshot } from './empty-snapshot';
export function getWorksheetUID(workbook: Workbook, worksheet: Worksheet): string {
return `${workbook.getUnitId()}|${worksheet.getSheetId()}`;
@@ -87,7 +88,12 @@ export class Workbook extends Disposable {
) {
super();
- this._snapshot = Tools.commonExtend(DEFAULT_WORKBOOK, workbookData);
+ const DEFAULT_WORKBOOK = getEmptySnapshot();
+ if (Tools.isEmptyObject(workbookData)) {
+ this._snapshot = DEFAULT_WORKBOOK;
+ } else {
+ this._snapshot = Tools.commonExtend(DEFAULT_WORKBOOK, workbookData);
+ }
const { styles } = this._snapshot;
if (this._snapshot.id == null || this._snapshot.id.length === 0) {
diff --git a/packages/core/src/types/const/const.ts b/packages/core/src/types/const/const.ts
index 55afc5e502..3d20c10b6f 100644
--- a/packages/core/src/types/const/const.ts
+++ b/packages/core/src/types/const/const.ts
@@ -17,13 +17,10 @@
import {
BooleanNumber,
HorizontalAlign,
- LocaleType,
TextDirection,
VerticalAlign,
WrapStrategy,
} from '../enum';
-import type { IWorkbookData } from '../interfaces';
-import { version } from '../../../package.json';
/**
* Used as an illegal range array return value
@@ -65,20 +62,6 @@ export const DEFAULT_CELL = {
column: 0,
};
-/**
- * Used as an init workbook return value
- */
-export const DEFAULT_WORKBOOK: IWorkbookData = {
- id: '',
- sheetOrder: [],
- name: '',
- appVersion: version,
- locale: LocaleType.ZH_CN,
- styles: {},
- sheets: {},
- resources: [],
-};
-
/**
* Default styles.
*/