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: optimize the storage of worksheet rowData and columnData (#5317)

wpxp12345646279d4fe0df
diff --git a/packages/core/src/sheets/column-manager.ts b/packages/core/src/sheets/column-manager.ts
index 7b3a486eca..14e9aa9a12 100644
--- a/packages/core/src/sheets/column-manager.ts
+++ b/packages/core/src/sheets/column-manager.ts
@@ -37,7 +37,7 @@ export class ColumnManager {
/**
* Get width and hidden status of columns in the sheet
- * @returns
+ * @returns {IObjectArrayPrimitiveType<Partial<IColumnData>>} Column data, including width, hidden status, etc.
*/
getColumnData(): IObjectArrayPrimitiveType<Partial<IColumnData>> {
return this._columnData;
@@ -159,8 +159,11 @@ export class ColumnManager {
const columnData: IObjectArrayPrimitiveType<Partial<IColumnData>> = {};
let index = 0;
for (let i = columnPos; i < columnPos + numColumns; i++) {
- const data = this.getColumnOrCreate(i);
- columnData[index] = data;
+ const data = this.getColumn(i);
+ columnData[index] = data ?? {
+ w: this._config.defaultColumnWidth,
+ hd: BooleanNumber.FALSE,
+ };
index++;
}
return columnData;
@@ -168,7 +171,7 @@ export class ColumnManager {
/**
* Get count of column in the sheet
- * @returns
+ * @returns {number} count of column
*/
getSize(): number {
return getArrayLength(this._columnData);
@@ -177,31 +180,40 @@ export class ColumnManager {
/**
* Get the width of column
* @param columnPos column index
- * @returns
+ * @returns {number} width of column
*/
getColumnWidth(columnPos: number): number {
- const { _columnData } = this;
- const config = this._config;
- let width: number = 0;
+ return this._columnData[columnPos]?.w ?? this._config.defaultColumnWidth;
+ }
+
+ /**
+ * Set the width of column
+ * @param columnPos column index
+ * @param width width of column
+ */
+ setColumnWidth(columnPos: number, width: number) {
+ const column = this._columnData[columnPos];
- const column = _columnData[columnPos] || {
- hd: BooleanNumber.FALSE,
- w: config.defaultColumnWidth,
- };
- width = column.w || config.defaultColumnWidth;
+ // To prevent data redundancy, we only set the width when it is different from the default column width.
+ if (width === this._config.defaultColumnWidth) {
+ if (column) {
+ delete column.w;
- return width;
+ if (Object.keys(column).length === 0) {
+ delete this._columnData[columnPos];
+ }
+ }
+ } else {
+ this._columnData[columnPos] = column ? { ...column, w: width } : { w: width };
+ }
}
/**
- * get given column data
+ * Get given column data
* @param columnPos column index
*/
getColumn(columnPos: number): Nullable<Partial<IColumnData>> {
- const column = this._columnData[columnPos];
- if (column) {
- return column;
- }
+ return this._columnData[columnPos];
}
/**
@@ -213,7 +225,9 @@ export class ColumnManager {
}
/**
- * get given column data or create a column data when it's null
+ * Get given column data or create a column data when it's null
+ * This method is used to ensure that the column data should not be null when setting column properties.
+ * To prevent data redundancy, if is not setting column properties, you can use `getColumn` method to get column data. don't use this method.
* @param columnPos column index
* @returns {Partial<IColumnData>} columnData
*/
diff --git a/packages/core/src/sheets/row-manager.ts b/packages/core/src/sheets/row-manager.ts
index 536d70a321..2370463afb 100644
--- a/packages/core/src/sheets/row-manager.ts
+++ b/packages/core/src/sheets/row-manager.ts
@@ -14,14 +14,17 @@
* limitations under the License.
*/
+import type { IObjectArrayPrimitiveType } from '../shared/object-matrix';
import type { Nullable } from '../shared/types';
import type { IStyleData } from '../types/interfaces';
import type { CustomData, IRange, IRowData, IWorksheetData } from './typedef';
import type { SheetViewModel } from './view-model';
-import { getArrayLength, type IObjectArrayPrimitiveType } from '../shared/object-matrix';
+import { getArrayLength } from '../shared/object-matrix';
import { BooleanNumber } from '../types/enum';
import { RANGE_TYPE } from './typedef';
+const MAXIMUM_ROW_HEIGHT = 2000;
+
/**
* Manage configuration information of all rows, get row height, row length, set row height, etc.
*/
@@ -38,7 +41,7 @@ export class RowManager {
/**
* Get height and hidden status of columns in the sheet
- * @returns
+ * @returns {IObjectArrayPrimitiveType<Partial<IRowData>>} Row data, including height, hidden status, etc.
*/
getRowData(): IObjectArrayPrimitiveType<Partial<IRowData>> {
return this._rowData;
@@ -67,8 +70,11 @@ export class RowManager {
const rowData: IObjectArrayPrimitiveType<Partial<IRowData>> = {};
let index = 0;
for (let i = rowPos; i < rowPos + numRows; i++) {
- const data = this.getRowOrCreate(i);
- rowData[index] = data;
+ const data = this.getRow(i);
+ rowData[index] = data ?? {
+ h: this._config.defaultRowHeight,
+ hd: BooleanNumber.FALSE,
+ };
index++;
}
return rowData;
@@ -94,6 +100,29 @@ export class RowManager {
return height;
}
+ /**
+ * Set row height of given row
+ * @param rowPos row index
+ * @param height row height
+ */
+ setRowHeight(rowPos: number, height: number) {
+ const row = this._rowData[rowPos];
+
+ // To prevent data redundancy, we only set the height when it is different from the default row height.
+ if (height === this._config.defaultRowHeight) {
+ if (row) {
+ delete row.h;
+
+ if (Object.keys(row).length === 0) {
+ delete this._rowData[rowPos];
+ }
+ }
+ } else {
+ const _height = Math.min(height, MAXIMUM_ROW_HEIGHT);
+ this._rowData[rowPos] = row ? { ...row, h: _height } : { h: _height };
+ }
+ }
+
/**
* Get row data of given row
* @param rowPos row index
@@ -113,6 +142,8 @@ export class RowManager {
/**
* Get given row data or create a row data when it's null
+ * This method is used to ensure that the row data should not be null when setting row properties.
+ * To prevent data redundancy, if is not setting row properties, you can use `getRow` method to get row data. don't use this method.
* @param rowPos row index
* @returns {Partial<IRowData>} rowData
*/
diff --git a/packages/sheets-ui/src/controllers/auto-width.controller.ts b/packages/sheets-ui/src/controllers/auto-width.controller.ts
index c413875955..3b4bd824cf 100644
--- a/packages/sheets-ui/src/controllers/auto-width.controller.ts
+++ b/packages/sheets-ui/src/controllers/auto-width.controller.ts
@@ -41,8 +41,7 @@ export const createAutoColWidthUndoMutationsByRedos = (
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i];
for (let j = range.startColumn; j < range.endColumn + 1; j++) {
- const col = manager.getColumnOrCreate(j);
- colWidthObj[j] = col.w;
+ colWidthObj[j] = manager.getColumnWidth(j);
}
}
diff --git a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.ts b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.ts
index a2e092babd..d59e3e3947 100644
--- a/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.ts
+++ b/packages/sheets-ui/src/services/clipboard/__tests__/clipboard-service.spec.ts
@@ -14,6 +14,8 @@
* limitations under the License.
*/
+import type { ICellData, Injector, IRange, IStyleData, Nullable, Univer } from '@univerjs/core';
+import type { IClipboardItem } from './mock-clipboard';
import { ICommandService, IUniverInstanceService, RANGE_TYPE, Rectangle, RedoCommand, UndoCommand } from '@univerjs/core';
import {
AddWorksheetMergeMutation,
@@ -25,15 +27,13 @@ import {
SetWorksheetRowHeightMutation,
SheetsSelectionsService,
} from '@univerjs/sheets';
-import { afterEach, beforeEach, describe, expect, it } from 'vitest';
-import type { ICellData, Injector, IRange, IStyleData, Nullable, Univer } from '@univerjs/core';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { discreteRangeToRange } from '../../../controllers/utils/range-tools';
import { ISheetClipboardService, PREDEFINED_HOOK_NAME } from '../clipboard.service';
import { COPY_TYPE } from '../type';
import { clipboardTestBed } from './clipboard-test-bed';
import { MockClipboard } from './mock-clipboard';
-import type { IClipboardItem } from './mock-clipboard';
describe('Test clipboard', () => {
let univer: Univer;
@@ -154,7 +154,7 @@ describe('Test clipboard', () => {
const mergedCells = getMergedCells(startRow, startColumn, endRow, endColumn);
const rowManager = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1')?.getRowManager();
const columnManager = get(IUniverInstanceService).getUniverSheetInstance('test')?.getSheetBySheetId('sheet1')?.getColumnManager();
- const columnWidth = columnManager?.getColumnData()?.[0]?.w;
+ const columnWidth = columnManager?.getColumnWidth(0);
expect(columnWidth).toBe(88);
expect(values && values[0][0]?.v).toBe('row1col2');
expect(styles && styles[0][0]).toStrictEqual({
diff --git a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
index c24e80c85c..08e4b2ce80 100644
--- a/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
+++ b/packages/sheets-ui/src/services/clipboard/clipboard.service.ts
@@ -691,22 +691,16 @@ export class SheetClipboardService extends Disposable implements ISheetClipboard
const colManager = pasteFromWorksheet.getColumnManager();
const rowManager = pasteFromWorksheet.getRowManager();
- const defaultColumnWidth = pasteFromWorksheet.getConfig().defaultColumnWidth;
- const defaultRowHeight = pasteFromWorksheet.getConfig().defaultRowHeight;
const colProperties: IClipboardPropertyItem[] = [];
const rowProperties: IClipboardPropertyItem[] = [];
range.cols.forEach((i) => {
- const column = colManager.getColumnOrCreate(i);
- colProperties.push({ width: `${column.w || defaultColumnWidth}` });
+ colProperties.push({ width: `${colManager.getColumnWidth(i)}` });
});
range.rows.forEach((j) => {
- const row = rowManager.getRowOrCreate(j);
- const { ah = defaultRowHeight, h = defaultRowHeight } = row;
- const height = Math.max(ah, h);
- rowProperties.push({ height: `${height}` });
+ rowProperties.push({ height: `${rowManager.getRowHeight(j)}` });
});
if (cachedData.copyType === COPY_TYPE.CUT) {
diff --git a/packages/sheets/src/commands/mutations/set-worksheet-col-width.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-col-width.mutation.ts
index b0692eed54..e5814ab21e 100644
--- a/packages/sheets/src/commands/mutations/set-worksheet-col-width.mutation.ts
+++ b/packages/sheets/src/commands/mutations/set-worksheet-col-width.mutation.ts
@@ -15,7 +15,7 @@
*/
import type { IMutation, IObjectArrayPrimitiveType, IRange, Nullable, Worksheet } from '@univerjs/core';
-import { CommandType, IUniverInstanceService } from '@univerjs/core';
+import { CommandType, IUniverInstanceService, Tools } from '@univerjs/core';
import { getSheetCommandTarget } from '../commands/utils/target-util';
@@ -43,8 +43,7 @@ export const SetWorksheetColWidthMutationFactory = (
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i];
for (let j = range.startColumn; j < range.endColumn + 1; j++) {
- const column = manager.getColumnOrCreate(j);
- colWidth[j] = column.w;
+ colWidth[j] = manager.getColumnWidth(j);
}
}
@@ -68,19 +67,18 @@ export const SetWorksheetColWidthMutation: IMutation<ISetWorksheetColWidthMutati
if (!target) return false;
const { worksheet } = target;
- const defaultColumnWidth = worksheet.getConfig().defaultColumnWidth;
const manager = worksheet.getColumnManager();
const ranges = params.ranges;
+
for (let i = 0; i < ranges.length; i++) {
const range = ranges[i];
for (let j = range.startColumn; j < range.endColumn + 1; j++) {
const visible = worksheet.getColVisible(j);
if (!visible) continue;
- const column = manager.getColumnOrCreate(j);
if (typeof params.colWidth === 'number') {
- column.w = params.colWidth;
- } else {
- column.w = params.colWidth[j] ?? defaultColumnWidth;
+ manager.setColumnWidth(j, params.colWidth);
+ } else if (Tools.isDefine(params.colWidth[j])) {
+ manager.setColumnWidth(j, params.colWidth[j] as number);
}
}
}
diff --git a/packages/sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts
index a48f70e577..4b83db8343 100644
--- a/packages/sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts
+++ b/packages/sheets/src/commands/mutations/set-worksheet-row-height.mutation.ts
@@ -15,11 +15,9 @@
*/
import type { BooleanNumber, IMutation, IObjectArrayPrimitiveType, IRange, IRowAutoHeightInfo, Nullable, Worksheet } from '@univerjs/core';
-import { CommandType, IUniverInstanceService } from '@univerjs/core';
+import { CommandType, IUniverInstanceService, Tools } from '@univerjs/core';
import { getSheetCommandTarget } from '../commands/utils/target-util';
-const MAXIMUM_ROW_HEIGHT = 2000;
-
export interface ISetWorksheetRowHeightMutationParams {
unitId: string;
subUnitId: string;
@@ -51,8 +49,7 @@ export const SetWorksheetRowHeightMutationFactory = (
for (const { startRow, endRow } of ranges) {
for (let rowIndex = startRow; rowIndex < endRow + 1; rowIndex++) {
- const row = manager.getRowOrCreate(rowIndex);
- rowHeight[rowIndex] = row.h;
+ rowHeight[rowIndex] = manager.getRow(rowIndex)?.h ?? worksheet.getConfig().defaultRowHeight;
}
}
@@ -75,9 +72,7 @@ export const SetWorksheetRowIsAutoHeightMutationFactory = (
for (const { startRow, endRow } of ranges) {
for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
- const row = manager.getRowOrCreate(rowIndex);
-
- autoHeightHash[rowIndex] = row.ia;
+ autoHeightHash[rowIndex] = manager.getRow(rowIndex)?.ia;
}
}
@@ -99,9 +94,10 @@ export const SetWorksheetRowAutoHeightMutationFactory = (
for (const rowInfo of rowsAutoHeightInfo) {
const { row } = rowInfo;
- const { ah } = manager.getRowOrCreate(row);
-
- results.push({ row, autoHeight: ah! });
+ results.push({
+ row,
+ autoHeight: manager.getRow(row)?.ah ?? worksheet.getConfig().defaultRowHeight,
+ });
}
return {
@@ -122,18 +118,14 @@ export const SetWorksheetRowHeightMutation: IMutation<ISetWorksheetRowHeightMuta
const { worksheet } = target;
const manager = worksheet.getRowManager();
- const defaultRowHeight = worksheet.getConfig().defaultRowHeight;
+
for (const { startRow, endRow } of ranges) {
for (let rowIndex = startRow; rowIndex <= endRow; rowIndex++) {
- const row = manager.getRowOrCreate(rowIndex);
-
if (typeof rowHeight === 'number') {
- row.h = rowHeight;
- } else {
- row.h = rowHeight[rowIndex] ?? defaultRowHeight; // Start from startRow
+ manager.setRowHeight(rowIndex, rowHeight);
+ } else if (Tools.isDefine(rowHeight[rowIndex])) {
+ manager.setRowHeight(rowIndex, rowHeight[rowIndex] as number);
}
-
- row.h = Math.min(MAXIMUM_ROW_HEIGHT, row.h);
}
}