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(sheet): remove the last col error (#826)

Gpound.liue194bca6086d
diff --git a/packages/core/src/shared/object-matrix.ts b/packages/core/src/shared/object-matrix.ts
index a94678ec15..d93d8869cd 100644
--- a/packages/core/src/shared/object-matrix.ts
+++ b/packages/core/src/shared/object-matrix.ts
@@ -55,6 +55,32 @@ export function insertMatrixArray<T>(
array[index] = value;
}
+export function spliceArray<T>(start: number, count: number, o: IObjectArrayPrimitiveType<T>) {
+ const end = start + count;
+ const length = getArrayLength(o);
+ const array = o;
+ let effective = 0;
+ const splice: IObjectArrayPrimitiveType<T> = {};
+ for (let i = start; i < end; i++) {
+ const item = array[i];
+ if (item !== undefined) {
+ delete array[i];
+ splice[effective] = item;
+ effective++;
+ }
+ }
+ const diff = end - start;
+ const last = length;
+
+ for (let i = end; i < last; i++) {
+ const item = array[i];
+ if (item !== undefined) {
+ array[i - diff] = array[i];
+ delete array[i];
+ }
+ }
+}
+
export function concatMatrixArray<T>(
source: IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T>,
target: IObjectArrayPrimitiveType<T> | IObjectMatrixPrimitiveType<T>
diff --git a/packages/sheets/src/basics/const/command-listener-const.ts b/packages/sheets/src/basics/const/command-listener-const.ts
index 2b168ee5fd..aa4fabffa4 100644
--- a/packages/sheets/src/basics/const/command-listener-const.ts
+++ b/packages/sheets/src/basics/const/command-listener-const.ts
@@ -16,8 +16,10 @@
import { DeleteRangeMutation } from '../../commands/mutations/delete-range.mutation';
import { InsertRangeMutation } from '../../commands/mutations/insert-range.mutation';
+import { InsertColMutation, InsertRowMutation } from '../../commands/mutations/insert-row-col.mutation';
import { MoveRangeMutation } from '../../commands/mutations/move-range.mutation';
import { MoveColsMutation, MoveRowsMutation } from '../../commands/mutations/move-rows-cols.mutation';
+import { RemoveColMutation, RemoveRowMutation } from '../../commands/mutations/remove-row-col.mutation';
import { SetColHiddenMutation, SetColVisibleMutation } from '../../commands/mutations/set-col-visible.mutation';
import { SetRangeValuesMutation } from '../../commands/mutations/set-range-values.mutation';
import { SetRowHiddenMutation, SetRowVisibleMutation } from '../../commands/mutations/set-row-visible.mutation';
@@ -43,6 +45,10 @@ export const COMMAND_LISTENER_SKELETON_CHANGE = [
SetRowVisibleMutation.id,
InsertRangeMutation.id,
DeleteRangeMutation.id,
+ InsertColMutation.id,
+ InsertRowMutation.id,
+ RemoveColMutation.id,
+ RemoveRowMutation.id,
];
export const COMMAND_LISTENER_VALUE_CHANGE = [SetRangeValuesMutation.id, MoveRangeMutation.id];
diff --git a/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts b/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts
index 399cd0f62a..f48ae7c7f8 100644
--- a/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts
+++ b/packages/sheets/src/commands/mutations/remove-row-col.mutation.ts
@@ -15,7 +15,7 @@
*/
import type { IColumnData, IMutation, IObjectArrayPrimitiveType, IRowData, Worksheet } from '@univerjs/core';
-import { CommandType, concatMatrixArray, IUniverInstanceService, sliceMatrixArray } from '@univerjs/core';
+import { CommandType, concatMatrixArray, IUniverInstanceService, sliceMatrixArray, spliceArray } from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';
import type {
@@ -59,20 +59,14 @@ export const RemoveRowMutation: IMutation<IRemoveRowsMutationParams> = {
const worksheet = universheet.getSheetBySheetId(params.subUnitId);
if (!worksheet) return false;
-
+ const range = params.range;
const manager = worksheet.getRowManager();
const rowPrimitive = manager.getRowData();
- let rowWrapper = rowPrimitive;
-
- const range = params.range;
- const start = range.startRow;
- const end = range.endRow;
- for (let j = start; j <= end; j++) {
- rowWrapper = sliceMatrixArray(j, 1, rowWrapper);
- }
+ const rowCount = range.endRow - range.startRow + 1;
+ spliceArray(range.startRow, rowCount, rowPrimitive);
- worksheet.setRowCount(worksheet.getRowCount() - range.endRow - range.startRow + 1);
+ worksheet.setRowCount(worksheet.getRowCount() - rowCount);
return true;
},
@@ -129,8 +123,11 @@ export const RemoveColMutation: IMutation<IRemoveColMutationParams> = {
if (!worksheet) return false;
const range = params.range;
-
- worksheet.setColumnCount(worksheet.getColumnCount() - range.endColumn - range.startColumn + 1);
+ const manager = worksheet.getColumnManager();
+ const colPrimitive = manager.getColumnData();
+ const colCount = range.endColumn - range.startColumn + 1;
+ spliceArray(range.startColumn, colCount, colPrimitive);
+ worksheet.setColumnCount(worksheet.getColumnCount() - colCount);
return true;
},