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: fix issue with inserting rows and columns (#5711)

wpxp12345615917b36cea6
diff --git a/packages/core/src/shared/object-matrix.ts b/packages/core/src/shared/object-matrix.ts
index efd78eb290..6d482c812b 100644
--- a/packages/core/src/shared/object-matrix.ts
+++ b/packages/core/src/shared/object-matrix.ts
@@ -62,6 +62,10 @@ export function getArrayLength<T>(o: IObjectArrayPrimitiveType<T> | IObjectMatri
return maxIndex + 1;
}
+/**
+ * This function has bug of undefined value to be inserted.
+ * @deprecated
+ */
export function insertMatrixArray<T>(
index: number,
value: T,
@@ -410,19 +414,40 @@ export class ObjectMatrix<T> {
}
insertRows(start: number, count: number): void {
- for (let r = start; r < start + count; r++) {
- const initial: IObjectArrayPrimitiveType<T> = {};
- insertMatrixArray(r, initial, this._matrix);
+ const rowKeys = Object.keys(this._matrix);
+
+ // move all items after start in forward order
+ for (let i = rowKeys.length - 1; i >= 0; i--) {
+ const rowIndex = Number(rowKeys[i]);
+
+ if (rowIndex >= start) {
+ const rowObject = this._matrix[rowIndex];
+ // move the row to the next position
+ delete this._matrix[rowIndex];
+ this._matrix[rowIndex + count] = rowObject;
+ }
}
}
insertColumns(start: number, count: number): void {
- for (let c = start; c < start + count; c++) {
- this.forEach((row, data) => {
- if (data) {
- insertMatrixArray(c, undefined, data);
+ const rowKeys = Object.keys(this._matrix);
+
+ for (let i = 0; i < rowKeys.length; i++) {
+ const rowIndex = Number(rowKeys[i]);
+ const rowObject = this._matrix[rowIndex];
+ const columnKeys = Object.keys(rowObject);
+
+ // move all items after start in backward order
+ for (let j = columnKeys.length - 1; j >= 0; j--) {
+ const columnIndex = Number(columnKeys[j]);
+
+ if (columnIndex >= start) {
+ const value = rowObject[columnIndex];
+ // move the column to the next position
+ delete rowObject[columnIndex];
+ rowObject[columnIndex + count] = value;
}
- });
+ }
}
}
diff --git a/packages/sheets/src/commands/commands/move-rows-cols.command.ts b/packages/sheets/src/commands/commands/move-rows-cols.command.ts
index 7ddfca263e..a7657551cc 100644
--- a/packages/sheets/src/commands/commands/move-rows-cols.command.ts
+++ b/packages/sheets/src/commands/commands/move-rows-cols.command.ts
@@ -337,7 +337,7 @@ export const MoveColsCommand: ICommand<IMoveColsCommandParams> = {
return true;
}
- return true;
+ return false;
},
};
diff --git a/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts b/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts
index c834a5e30d..1f12d2419b 100644
--- a/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts
+++ b/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts
@@ -15,14 +15,13 @@
*/
import type { IAccessor, IMutation } from '@univerjs/core';
-import { CommandType, insertMatrixArray, IUniverInstanceService } from '@univerjs/core';
-
import type {
IInsertColMutationParams,
IInsertRowMutationParams,
IRemoveColMutationParams,
IRemoveRowsMutationParams,
} from '../../basics/interfaces/mutation-interface';
+import { CommandType, insertMatrixArray, IUniverInstanceService } from '@univerjs/core';
export const InsertRowMutationUndoFactory = (
accessor: IAccessor,
@@ -78,7 +77,7 @@ export const InsertRowMutation: IMutation<IInsertRowMutationParams> = {
}
}
- worksheet.setRowCount(worksheet.getRowCount() + range.endRow - range.startRow + 1);
+ worksheet.setRowCount(worksheet.getRowCount() + rowCount);
// remove cells contents by directly mutating worksheetCellMatrix
const cellMatrix = worksheet.getCellMatrix();