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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix: optimize the performance of frequent row and column deletion operations (#6493)
wpxp12345600fe317c0bac
diff --git a/packages/core/src/services/command/command.service.ts b/packages/core/src/services/command/command.service.tsindex 8af14eda4f..333f6c02cd 100644--- a/packages/core/src/services/command/command.service.ts+++ b/packages/core/src/services/command/command.service.ts@@ -210,6 +210,7 @@ export const ICommandService = createIdentifier<ICommandService>('univer.core.co* The service to register and execute commands.*/export interface ICommandService {+ disposed(): boolean;/*** Check if a command is already registered at the current command service.* @param commandId The id of the command.@@ -350,6 +351,10 @@ export class CommandService extends Disposable implements ICommandService {this._collabMutationListeners.length = 0;}+ disposed(): boolean {+ return this._disposed;+ }+hasCommand(commandId: string): boolean {return this._commandRegistry.hasCommand(commandId);}diff --git a/packages/core/src/shared/index.ts b/packages/core/src/shared/index.tsindex fff5fc7e41..0fb9b36d58 100644--- a/packages/core/src/shared/index.ts+++ b/packages/core/src/shared/index.ts@@ -28,6 +28,7 @@ export * from './dayjs';export * from './doc-tool';export * from './generate';export * from './hash-algorithm';+export * from './intervals';export * from './lifecycle';export * from './locale';export { LRUHelper, LRUMap } from './lru/lru-map';diff --git a/packages/core/src/shared/intervals.ts b/packages/core/src/shared/intervals.tsnew file mode 100644index 0000000000..f86f477875--- /dev/null+++ b/packages/core/src/shared/intervals.ts@@ -0,0 +1,88 @@+/**+ * Copyright 2023-present DreamNum Co., Ltd.+ *+ * 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.+ */++export function generateIntervalsByPoints(points: Array<number | [number, number]>): [number, number][] {+ if (points.length === 0) {+ return [];+ }++ // Sort points and ranges by their starting number+ const sortedPoints = points.slice().sort((a, b) => (Array.isArray(a) ? a[0] : a) - (Array.isArray(b) ? b[0] : b));++ const intervals: [number, number][] = [];+ let currentStart = Array.isArray(sortedPoints[0]) ? sortedPoints[0][0] : sortedPoints[0];+ let currentEnd = currentStart;++ for (let i = 1; i < sortedPoints.length; i++) {+ const point = sortedPoints[i];+ let nextStart: number;+ let nextEnd: number;++ if (Array.isArray(point)) {+ nextStart = point[0];+ nextEnd = point[1];+ } else {+ nextStart = point;+ nextEnd = point;+ }++ if (nextStart <= currentEnd + 1) {+ // Extend the current interval+ currentEnd = Math.max(currentEnd, nextEnd);+ } else {+ // Save the current interval and start a new one+ intervals.push([currentStart, currentEnd]);+ currentStart = nextStart;+ currentEnd = nextEnd;+ }+ }++ // Push the last interval+ intervals.push([currentStart, currentEnd]);++ return intervals;+}++export function mergeIntervals(intervals: [number, number][]): [number, number][] {+ if (intervals.length === 0) {+ return [];+ }++ // Sort intervals based on the start number+ const sortedIntervals = intervals.slice().sort((a, b) => a[0] - b[0]);+ const merged: [number, number][] = [];++ let [currentStart, currentEnd] = sortedIntervals[0];++ for (let i = 1; i < sortedIntervals.length; i++) {+ const [nextStart, nextEnd] = sortedIntervals[i];++ if (nextStart <= currentEnd + 1) {+ // Overlap or contiguous intervals, merge them+ currentEnd = Math.max(currentEnd, nextEnd);+ } else {+ // No overlap, push the current interval and move to the next+ merged.push([currentStart, currentEnd]);+ currentStart = nextStart;+ currentEnd = nextEnd;+ }+ }++ // Push the last interval+ merged.push([currentStart, currentEnd]);++ return merged;+}diff --git a/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/highlight-cell-calculate-unit.ts b/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/highlight-cell-calculate-unit.tsindex 0ca4d94ca6..3275dc01dd 100644--- a/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/highlight-cell-calculate-unit.ts+++ b/packages/sheets-conditional-formatting/src/models/calculate-unit-v2/highlight-cell-calculate-unit.ts@@ -22,7 +22,7 @@ import { ERROR_TYPE_SET, FormulaResultStatus } from '@univerjs/engine-formula';import { CFNumberOperator, CFSubRuleType, CFTextOperator, CFTimePeriodOperator } from '../../base/const';import { ConditionalFormattingFormulaService } from '../../services/conditional-formatting-formula.service';import { BaseCalculateUnit, CalculateEmitStatus } from './base-calculate-unit';-import { compareWithNumber, getCellValue, isFloatsEqual, isNullable, serialTimeToTimestamp } from './utils';+import { compareWithNumber, filterRange, getCellValue, isFloatsEqual, isNullable, serialTimeToTimestamp } from './utils';;@@ -34,7 +34,7 @@ export class HighlightCellCalculateUnit extends BaseCalculateUnit<Nullable<IConf// eslint-disable-next-line max-lines-per-functionoverride preComputing(row: number, col: number, context: IContext): void {const ruleConfig = context.rule.rule as IHighlightCell;- const ranges = context.rule.ranges;+ const ranges = filterRange(context.rule.ranges, context.worksheet.getMaxRows() - 1, context.worksheet.getMaxColumns() - 1);// eslint-disable-next-line max-lines-per-function, complexityconst getCache = () => {switch (ruleConfig.subType) {diff --git a/packages/sheets-formula-ui/src/controllers/__tests__/update-defined-name.controller.spec.ts b/packages/sheets-formula-ui/src/controllers/__tests__/update-defined-name.controller.spec.tsindex 72e2b11424..5b191d0a2c 100644--- a/packages/sheets-formula-ui/src/controllers/__tests__/update-defined-name.controller.spec.ts+++ b/packages/sheets-formula-ui/src/controllers/__tests__/update-defined-name.controller.spec.ts@@ -15,7 +15,7 @@*/import type { ICellData, Injector, IWorkbookData, Nullable, Univer } from '@univerjs/core';-import type { IDeleteRangeMoveLeftCommandParams, IDeleteRangeMoveUpCommandParams, IInsertColCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, InsertRangeMoveDownCommandParams, InsertRangeMoveRightCommandParams, IRemoveRowColCommandParams, IRemoveSheetCommandParams, ISetWorksheetNameCommandParams } from '@univerjs/sheets';+import type { IDeleteRangeMoveLeftCommandParams, IDeleteRangeMoveUpCommandParams, IInsertColCommandParams, IInsertRangeMoveDownCommandParams, IInsertRangeMoveRightCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, IRemoveRowColCommandParams, IRemoveSheetCommandParams, ISetWorksheetNameCommandParams } from '@univerjs/sheets';import { Direction, ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE, RedoCommand, UndoCommand } from '@univerjs/core';import { IDefinedNamesService, RemoveDefinedNameMutation, SetArrayFormulaDataMutation, SetDefinedNameMutation, SetFormulaDataMutation } from '@univerjs/engine-formula';import { DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, InsertColByRangeCommand, InsertColCommand, InsertColMutation, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, InsertRowByRangeCommand, InsertRowCommand, InsertRowMutation, MoveColsCommand, MoveColsMutation, MoveRangeCommand, MoveRangeMutation, MoveRowsCommand, MoveRowsMutation, RemoveColByRangeCommand, RemoveColCommand, RemoveColMutation, RemoveRowByRangeCommand, RemoveRowCommand, RemoveRowMutation, RemoveSheetCommand, RemoveSheetMutation, SetDefinedNameCommand, SetRangeValuesCommand, SetRangeValuesMutation, SetSelectionsOperation, SetWorksheetNameCommand, SetWorksheetNameMutation, SheetsSelectionsService } from '@univerjs/sheets';@@ -699,7 +699,7 @@ describe('Test update defined name', () => {});it('Insert move down, update reference', async () => {- const params: InsertRangeMoveDownCommandParams = {+ const params: IInsertRangeMoveDownCommandParams = {range: {startRow: 1,startColumn: 0,@@ -720,7 +720,7 @@ describe('Test update defined name', () => {});it('Insert move right, update reference', async () => {- const params: InsertRangeMoveRightCommandParams = {+ const params: IInsertRangeMoveRightCommandParams = {range: {startRow: 0,startColumn: 1,diff --git a/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts b/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.tsindex 039cb7c5c1..4e65482375 100644--- a/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts+++ b/packages/sheets-formula-ui/src/controllers/__tests__/update-formula.controller.spec.ts@@ -16,7 +16,7 @@import type { ICellData, Injector, IWorkbookData, Nullable, Univer, Workbook } from '@univerjs/core';import type { ISetDefinedNameMutationParam } from '@univerjs/engine-formula';-import type { IDeleteRangeMoveLeftCommandParams, IDeleteRangeMoveUpCommandParams, IInsertColCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, InsertRangeMoveDownCommandParams, InsertRangeMoveRightCommandParams, IRemoveRowColCommandParams, IRemoveSheetCommandParams, ISetRangeValuesCommandParams, ISetWorksheetNameCommandParams } from '@univerjs/sheets';+import type { IDeleteRangeMoveLeftCommandParams, IDeleteRangeMoveUpCommandParams, IInsertColCommandParams, IInsertRangeMoveDownCommandParams, IInsertRangeMoveRightCommandParams, IInsertRowCommandParams, IMoveColsCommandParams, IMoveRangeCommandParams, IMoveRowsCommandParams, IRemoveRowColCommandParams, IRemoveSheetCommandParams, ISetRangeValuesCommandParams, ISetWorksheetNameCommandParams } from '@univerjs/sheets';import type { ISetRowHiddenCommandParams } from '@univerjs/sheets/commands/commands/set-row-visible.command.js';import { CellValueType, Direction, ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE, RedoCommand, UndoCommand } from '@univerjs/core';import { RemoveDefinedNameMutation, SetArrayFormulaDataMutation, SetDefinedNameMutation, SetFormulaDataMutation } from '@univerjs/engine-formula';@@ -1771,7 +1771,7 @@ describe('Test update formula ', () => {});it('Insert move down, value on the top', async () => {- const params: InsertRangeMoveDownCommandParams = {+ const params: IInsertRangeMoveDownCommandParams = {range: {startRow: 1,startColumn: 2,@@ -1795,7 +1795,7 @@ describe('Test update formula ', () => {});it('Insert move down, value on the bottom', async () => {- const params: InsertRangeMoveDownCommandParams = {+ const params: IInsertRangeMoveDownCommandParams = {range: {startRow: 4,startColumn: 2,@@ -1819,7 +1819,7 @@ describe('Test update formula ', () => {});it('Insert move down, update reference', async () => {- const params: InsertRangeMoveDownCommandParams = {+ const params: IInsertRangeMoveDownCommandParams = {range: {startRow: 1,startColumn: 0,@@ -1843,7 +1843,7 @@ describe('Test update formula ', () => {});it('Insert move right, value on the left', async () => {- const params: InsertRangeMoveRightCommandParams = {+ const params: IInsertRangeMoveRightCommandParams = {range: {startRow: 2,startColumn: 1,@@ -1867,7 +1867,7 @@ describe('Test update formula ', () => {});it('Insert move right, value on the right', async () => {- const params: InsertRangeMoveRightCommandParams = {+ const params: IInsertRangeMoveRightCommandParams = {range: {startRow: 5,startColumn: 1,@@ -1891,7 +1891,7 @@ describe('Test update formula ', () => {});it('Insert move right, update reference', async () => {- const params: InsertRangeMoveRightCommandParams = {+ const params: IInsertRangeMoveRightCommandParams = {range: {startRow: 0,startColumn: 1,diff --git a/packages/sheets-formula/src/controllers/utils/ref-range-param.ts b/packages/sheets-formula/src/controllers/utils/ref-range-param.tsindex 8254e8224b..6561ad29d0 100644--- a/packages/sheets-formula/src/controllers/utils/ref-range-param.ts+++ b/packages/sheets-formula/src/controllers/utils/ref-range-param.ts@@ -20,12 +20,12 @@ import type {IDeleteRangeMoveLeftCommandParams,IDeleteRangeMoveUpCommandParams,IInsertColCommandParams,+ IInsertRangeMoveDownCommandParams,+ IInsertRangeMoveRightCommandParams,IInsertRowCommandParams,IMoveColsCommandParams,IMoveRangeCommandParams,IMoveRowsCommandParams,- InsertRangeMoveDownCommandParams,- InsertRangeMoveRightCommandParams,IRemoveRowColCommandParams,IRemoveSheetCommandParams,ISetWorksheetNameCommandParams,@@ -72,10 +72,10 @@ export function getReferenceMoveParams(workbook: Workbook, command: ICommandInforesult = handleRefInsertCol(command as ICommandInfo<IInsertColCommandParams>);break;case InsertRangeMoveRightCommand.id:- result = handleRefInsertRangeMoveRight(command as ICommandInfo<InsertRangeMoveRightCommandParams>, workbook);+ result = handleRefInsertRangeMoveRight(command as ICommandInfo<IInsertRangeMoveRightCommandParams>, workbook);break;case InsertRangeMoveDownCommand.id:- result = handleRefInsertRangeMoveDown(command as ICommandInfo<InsertRangeMoveDownCommandParams>, workbook);+ result = handleRefInsertRangeMoveDown(command as ICommandInfo<IInsertRangeMoveDownCommandParams>, workbook);break;case RemoveRowCommand.id:result = handleRefRemoveRow(command as ICommandInfo<IRemoveRowColCommandParams>, workbook);@@ -238,7 +238,7 @@ function handleRefInsertCol(command: ICommandInfo<IInsertColCommandParams>) {};}-function handleRefInsertRangeMoveRight(command: ICommandInfo<InsertRangeMoveRightCommandParams>, workbook: Workbook) {+function handleRefInsertRangeMoveRight(command: ICommandInfo<IInsertRangeMoveRightCommandParams>, workbook: Workbook) {const { params } = command;if (!params) return null;@@ -253,7 +253,7 @@ function handleRefInsertRangeMoveRight(command: ICommandInfo<InsertRangeMoveRigh};}-function handleRefInsertRangeMoveDown(command: ICommandInfo<InsertRangeMoveDownCommandParams>, workbook: Workbook) {+function handleRefInsertRangeMoveDown(command: ICommandInfo<IInsertRangeMoveDownCommandParams>, workbook: Workbook) {const { params } = command;if (!params) return null;diff --git a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts b/packages/sheets/src/commands/commands/insert-range-move-down.command.tsindex c51c0c8e69..dca85bec9a 100644--- a/packages/sheets/src/commands/commands/insert-range-move-down.command.ts+++ b/packages/sheets/src/commands/commands/insert-range-move-down.command.ts@@ -41,7 +41,7 @@ import { getInsertRangeMutations } from '../utils/handle-range-mutation';import { followSelectionOperation } from './utils/selection-utils';import { getSheetCommandTarget } from './utils/target-util';-export interface InsertRangeMoveDownCommandParams {+export interface IInsertRangeMoveDownCommandParams {range: IRange;}@@ -54,7 +54,7 @@ export const InsertRangeMoveDownCommand: ICommand = {id: 'sheet.command.insert-range-move-down',// eslint-disable-next-line max-lines-per-function- handler: async (accessor: IAccessor, params?: InsertRangeMoveDownCommandParams) => {+ handler: async (accessor: IAccessor, params?: IInsertRangeMoveDownCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);@@ -151,7 +151,7 @@ export const InsertRangeMoveDownCommand: ICommand = {const sheetInterceptor = sheetInterceptorService.onCommandExecute({id: InsertRangeMoveDownCommand.id,- params: { range } as InsertRangeMoveDownCommandParams,+ params: { range } as IInsertRangeMoveDownCommandParams,});redoMutations.push(...sheetInterceptor.redos);redoMutations.push(followSelectionOperation(range, workbook, worksheet));@@ -165,7 +165,7 @@ export const InsertRangeMoveDownCommand: ICommand = {if (result) {const afterInterceptors = sheetInterceptorService.afterCommandExecute({id: InsertRangeMoveDownCommand.id,- params: { range } as InsertRangeMoveDownCommandParams,+ params: { range } as IInsertRangeMoveDownCommandParams,});sequenceExecute(afterInterceptors.redos, commandService);undoMutations.push(...afterInterceptors.undos);diff --git a/packages/sheets/src/commands/commands/insert-range-move-right.command.ts b/packages/sheets/src/commands/commands/insert-range-move-right.command.tsindex 6ba7cf7b7b..e5d08c50db 100644--- a/packages/sheets/src/commands/commands/insert-range-move-right.command.ts+++ b/packages/sheets/src/commands/commands/insert-range-move-right.command.ts@@ -41,7 +41,7 @@ import { getInsertRangeMutations } from '../utils/handle-range-mutation';import { followSelectionOperation } from './utils/selection-utils';import { getSheetCommandTarget } from './utils/target-util';-export interface InsertRangeMoveRightCommandParams {+export interface IInsertRangeMoveRightCommandParams {range: IRange;}export const InsertRangeMoveRightCommandId = 'sheet.command.insert-range-move-right';@@ -53,7 +53,7 @@ export const InsertRangeMoveRightCommand: ICommand = {id: InsertRangeMoveRightCommandId,// eslint-disable-next-line max-lines-per-function- handler: async (accessor: IAccessor, params?: InsertRangeMoveRightCommandParams) => {+ handler: async (accessor: IAccessor, params?: IInsertRangeMoveRightCommandParams) => {const commandService = accessor.get(ICommandService);const undoRedoService = accessor.get(IUndoRedoService);const univerInstanceService = accessor.get(IUniverInstanceService);@@ -151,7 +151,7 @@ export const InsertRangeMoveRightCommand: ICommand = {const sheetInterceptor = sheetInterceptorService.onCommandExecute({id: InsertRangeMoveRightCommand.id,- params: { range } as InsertRangeMoveRightCommandParams,+ params: { range } as IInsertRangeMoveRightCommandParams,});redoMutations.push(...sheetInterceptor.redos);redoMutations.push(followSelectionOperation(range, workbook, worksheet));@@ -165,7 +165,7 @@ export const InsertRangeMoveRightCommand: ICommand = {if (result.result) {const afterInterceptors = sheetInterceptorService.afterCommandExecute({id: InsertRangeMoveRightCommand.id,- params: { range } as InsertRangeMoveRightCommandParams,+ params: { range } as IInsertRangeMoveRightCommandParams,});sequenceExecute(afterInterceptors.redos, commandService);undoMutations.push(...afterInterceptors.undos);diff --git a/packages/sheets/src/commands/commands/remove-row-col.command.ts b/packages/sheets/src/commands/commands/remove-row-col.command.tsindex e56b5be4b4..7f607a213c 100644--- a/packages/sheets/src/commands/commands/remove-row-col.command.ts+++ b/packages/sheets/src/commands/commands/remove-row-col.command.ts@@ -14,14 +14,13 @@* limitations under the License.*/-import type { IAccessor, ICommand, IMutationInfo, IRange } from '@univerjs/core';+import type { IAccessor, ICommand, IMutationInfo, IRange, Workbook, Worksheet } from '@univerjs/core';import type {IInsertColMutationParams,IInsertRowMutationParams,IRemoveColMutationParams,IRemoveRowsMutationParams,} from '../../basics/interfaces/mutation-interface';-import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';import {CommandType,@@ -29,6 +28,7 @@ import {IUndoRedoService,IUniverInstanceService,sequenceExecute,+ throttle,} from '@univerjs/core';import { getVisibleRanges } from '../../basics/utils';import { SheetsSelectionsService } from '../../services/selections/selection.service';@@ -64,11 +64,22 @@ export interface IRemoveColByRangeCommandParams {subUnitId: string;}+/**+ * Set selection after remove row/col through throttle to avoid frequent set selection operation+ */+const setSelection = throttle((range: IRange, workbook: Workbook, worksheet: Worksheet, commandService: ICommandService) => {+ if (!commandService.disposed) {+ const command = followSelectionOperation(range, workbook, worksheet);+ commandService.executeCommand(command.id, command.params);+ };+}, 300);+export const RemoveRowCommandId = 'sheet.command.remove-row';export const RemoveRowByRangeCommand: ICommand<IRemoveRowByRangeCommandParams> = {type: CommandType.COMMAND,id: 'sheet.command.remove-row-by-range',+ // eslint-disable-next-line max-lines-per-functionhandler: (accessor, params) => {if (!params) {return false;@@ -125,12 +136,13 @@ export const RemoveRowByRangeCommand: ICommand<IRemoveRowByRangeCommandParams> =...(intercepted.preRedos ?? []),...redoMutations,...intercepted.redos,- followSelectionOperation(range, workbook, worksheet),],commandService);if (result.result) {+ setSelection(range, workbook, worksheet, commandService);+const afterInterceptors = sheetInterceptorService.afterCommandExecute({id: RemoveRowCommandId,params: { range } as IRemoveRowColCommandParams,@@ -245,12 +257,13 @@ export const RemoveColByRangeCommand: ICommand<IRemoveColByRangeCommandParams> =...(intercepted.preRedos ?? []),{ id: RemoveColMutation.id, params: removeColParams },...intercepted.redos,- followSelectionOperation(range, workbook, worksheet),],commandService);if (result.result) {+ setSelection(range, workbook, worksheet, commandService);+const afterInterceptors = sheetInterceptorService.afterCommandExecute({id: RemoveColCommandId,params: { range } as IRemoveRowColCommandParams,diff --git a/packages/sheets/src/controllers/merge-cell.controller.ts b/packages/sheets/src/controllers/merge-cell.controller.tsindex 0603d8d001..18e0bd1889 100644--- a/packages/sheets/src/controllers/merge-cell.controller.ts+++ b/packages/sheets/src/controllers/merge-cell.controller.ts@@ -32,8 +32,8 @@ import type {import type { IDeleteRangeMoveLeftCommandParams } from '../commands/commands/delete-range-move-left.command';import type { IDeleteRangeMoveUpCommandParams } from '../commands/commands/delete-range-move-up.command';-import type { InsertRangeMoveDownCommandParams } from '../commands/commands/insert-range-move-down.command';-import type { InsertRangeMoveRightCommandParams } from '../commands/commands/insert-range-move-right.command';+import type { IInsertRangeMoveDownCommandParams } from '../commands/commands/insert-range-move-down.command';+import type { IInsertRangeMoveRightCommandParams } from '../commands/commands/insert-range-move-right.command';import type { IInsertColCommandParams, IInsertRowCommandParams } from '../commands/commands/insert-row-col.command';import type { IMoveRangeCommandParams } from '../commands/commands/move-range.command';import type { IMoveColsCommandParams, IMoveRowsCommandParams } from '../commands/commands/move-rows-cols.command';@@ -270,11 +270,11 @@ export class MergeCellController extends Disposable {return this._handleMoveRangeCommand(params, unitId, subUnitId);}case InsertRangeMoveRightCommand.id: {- const params = config.params as unknown as InsertRangeMoveRightCommandParams;+ const params = config.params as unknown as IInsertRangeMoveRightCommandParams;return this._handleInsertRangeMoveRightCommand(params, unitId, subUnitId);}case InsertRangeMoveDownCommand.id: {- const params = config.params as unknown as InsertRangeMoveDownCommandParams;+ const params = config.params as unknown as IInsertRangeMoveDownCommandParams;return this._handleInsertRangeMoveDownCommand(params, unitId, subUnitId);}case DeleteRangeMoveUpCommand.id: {@@ -759,7 +759,7 @@ export class MergeCellController extends Disposable {}private _handleInsertRangeMoveRightCommand(- config: InsertRangeMoveRightCommandParams,+ config: IInsertRangeMoveRightCommandParams,unitId: string,subUnitId: string) {@@ -850,7 +850,7 @@ export class MergeCellController extends Disposable {}private _handleInsertRangeMoveDownCommand(- config: InsertRangeMoveDownCommandParams,+ config: IInsertRangeMoveDownCommandParams,unitId: string,subUnitId: string) {diff --git a/packages/sheets/src/facade/f-worksheet.ts b/packages/sheets/src/facade/f-worksheet.tsindex 997344d65f..c1bcd2471a 100644--- a/packages/sheets/src/facade/f-worksheet.ts+++ b/packages/sheets/src/facade/f-worksheet.ts@@ -18,7 +18,7 @@ import type { CellValue, CustomData, ICellData, IColumnData, IColumnRange, IDispimport type { ISetColDataCommandParams, ISetGridlinesColorCommandParams, ISetRangeValuesMutationParams, ISetRowDataCommandParams, ISetTextWrapCommandParams, IToggleGridlinesCommandParams } from '@univerjs/sheets';import type { FDefinedName } from './f-defined-name';import type { FWorkbook } from './f-workbook';-import { BooleanNumber, covertCellValue, Direction, ICommandService, ILogService, Inject, Injector, ObjectMatrix, RANGE_TYPE, WrapStrategy } from '@univerjs/core';+import { BooleanNumber, covertCellValue, Direction, generateIntervalsByPoints, ICommandService, ILogService, Inject, Injector, ObjectMatrix, RANGE_TYPE, WrapStrategy } from '@univerjs/core';import { FBaseInitialable } from '@univerjs/core/facade';import { deserializeRangeWithSheet } from '@univerjs/engine-formula';import { AppendRowCommand, CancelFrozenCommand, ClearSelectionAllCommand, ClearSelectionContentCommand, ClearSelectionFormatCommand, copyRangeStyles, InsertColByRangeCommand, InsertRowByRangeCommand, MoveColsCommand, MoveRowsCommand, RemoveColByRangeCommand, RemoveRowByRangeCommand, SetColDataCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetGridlinesColorCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetTabColorCommand, SetTextWrapCommand, SetWorksheetColumnCountCommand, SetWorksheetDefaultStyleMutation, SetWorksheetHideCommand, SetWorksheetNameCommand, SetWorksheetRowCountCommand, SetWorksheetRowIsAutoHeightCommand, SetWorksheetRowIsAutoHeightMutation, SetWorksheetShowCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets';@@ -634,6 +634,25 @@ export class FWorksheet extends FBaseInitialable {return this;}+ /**+ * Deletes the rows specified by the given row points. Each point can be a single row index or a tuple representing a range of rows.+ * @param {Array<number | [number, number]>} rowPoints - An array of row points to delete. Each point can be a single row index or a tuple representing a range of rows.+ * @returns {FWorksheet} This sheet, for chaining.+ * @example+ * ```typescript+ * const fWorksheet = univerAPI.getActiveWorkbook().getActiveSheet();+ * // Delete rows at index 2, and range from index 4 to 6 (rows 3, 5-7)+ * fWorksheet.deleteRowsByPoints([2, [4, 6]]);+ * ```+ */+ deleteRowsByPoints(rowPoints: Array<number | [number, number]>): FWorksheet {+ const rowIntervals = generateIntervalsByPoints(rowPoints);+ rowIntervals.reverse().forEach((interval) => {+ this.deleteRows(interval[0], interval[1] - interval[0] + 1);+ });+ return this;+ }+/*** Moves the rows selected by the given range to the position indicated by the destinationIndex. The rowSpec itself does not have to exactly represent an entire row or group of rows to move—it selects all rows that the range spans.* @param {FRange} rowSpec - A range spanning the rows that should be moved.@@ -1244,6 +1263,25 @@ export class FWorksheet extends FBaseInitialable {return this;}+ /**+ * Deletes the columns specified by the given column points. Each point can be a single column index or a tuple representing a range of columns.+ * @param {Array<number | [number, number]>} columnPoints - An array of column points to delete. Each point can be a single column index or a tuple representing a range of columns.+ * @returns {FWorksheet} This sheet, for chaining+ * @example+ * ```typescript+ * const fWorksheet = univerAPI.getActiveWorkbook().getActiveSheet();+ * // Delete columns at index 2, and range from index 4 to 6 (columns C, E-G)+ * fWorksheet.deleteColumnsByPoints([2, [4, 6]]);+ * ```+ */+ deleteColumnsByPoints(columnPoints: Array<number | [number, number]>): FWorksheet {+ const columnIntervals = generateIntervalsByPoints(columnPoints);+ columnIntervals.reverse().forEach((interval) => {+ this.deleteColumns(interval[0], interval[1] - interval[0] + 1);+ });+ return this;+ }+/*** Moves the columns selected by the given range to the position indicated by the destinationIndex. The columnSpec itself does not have to exactly represent an entire column or group of columns to move—it selects all columns that the range spans.* @param {FRange} columnSpec - A range spanning the columns that should be moveddiff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.tsindex 76dbe58bd4..b6c8ecd922 100644--- a/packages/sheets/src/index.ts+++ b/packages/sheets/src/index.ts@@ -80,8 +80,8 @@ export { DeleteRangeProtectionCommand, type IDeleteRangeProtectionCommandParamsexport { DeleteWorksheetProtectionCommand } from './commands/commands/delete-worksheet-protection.command';export { DeleteWorksheetRangeThemeStyleCommand } from './commands/commands/delete-worksheet-range-theme.command';export { InsertDefinedNameCommand } from './commands/commands/insert-defined-name.command';-export { InsertRangeMoveDownCommand, type InsertRangeMoveDownCommandParams } from './commands/commands/insert-range-move-down.command';-export { InsertRangeMoveRightCommand, type InsertRangeMoveRightCommandParams } from './commands/commands/insert-range-move-right.command';+export { type IInsertRangeMoveDownCommandParams, InsertRangeMoveDownCommand } from './commands/commands/insert-range-move-down.command';+export { type IInsertRangeMoveRightCommandParams, InsertRangeMoveRightCommand } from './commands/commands/insert-range-move-right.command';export {type IInsertColCommandParams,type IInsertRowCommandParams,diff --git a/packages/sheets/src/services/ref-range/type.ts b/packages/sheets/src/services/ref-range/type.tsindex 81322d4024..0f7dec780d 100644--- a/packages/sheets/src/services/ref-range/type.ts+++ b/packages/sheets/src/services/ref-range/type.ts@@ -17,22 +17,22 @@import type { ICommandInfo, IRange } from '@univerjs/core';import type { IDeleteRangeMoveLeftCommandParams } from '../../commands/commands/delete-range-move-left.command';-import { DeleteRangeMoveLeftCommandId } from '../../commands/commands/delete-range-move-left.command';import type { IDeleteRangeMoveUpCommandParams } from '../../commands/commands/delete-range-move-up.command';+import type { IInsertRangeMoveDownCommandParams } from '../../commands/commands/insert-range-move-down.command';+import type { IInsertRangeMoveRightCommandParams } from '../../commands/commands/insert-range-move-right.command';+import type { IInsertColCommandParams, IInsertRowCommandParams } from '../../commands/commands/insert-row-col.command';+import type { IMoveRangeCommandParams } from '../../commands/commands/move-range.command';+import type { IMoveColsCommandParams, IMoveRowsCommandParams } from '../../commands/commands/move-rows-cols.command';+import type { IRemoveRowColCommandParams } from '../../commands/commands/remove-row-col.command';+import type { IReorderRangeCommandParams } from '../../commands/commands/reorder-range.command';+import { DeleteRangeMoveLeftCommandId } from '../../commands/commands/delete-range-move-left.command';import { DeleteRangeMoveUpCommandId } from '../../commands/commands/delete-range-move-up.command';-import type { InsertRangeMoveDownCommandParams } from '../../commands/commands/insert-range-move-down.command';import { InsertRangeMoveDownCommandId } from '../../commands/commands/insert-range-move-down.command';-import type { InsertRangeMoveRightCommandParams } from '../../commands/commands/insert-range-move-right.command';import { InsertRangeMoveRightCommandId } from '../../commands/commands/insert-range-move-right.command';-import type { IInsertColCommandParams, IInsertRowCommandParams } from '../../commands/commands/insert-row-col.command';import { InsertColCommandId, InsertRowCommandId } from '../../commands/commands/insert-row-col.command';-import type { IMoveRangeCommandParams } from '../../commands/commands/move-range.command';import { MoveRangeCommandId } from '../../commands/commands/move-range.command';-import type { IMoveColsCommandParams, IMoveRowsCommandParams } from '../../commands/commands/move-rows-cols.command';import { MoveColsCommandId, MoveRowsCommandId } from '../../commands/commands/move-rows-cols.command';-import type { IRemoveRowColCommandParams } from '../../commands/commands/remove-row-col.command';import { RemoveColCommandId, RemoveRowCommandId } from '../../commands/commands/remove-row-col.command';-import type { IReorderRangeCommandParams } from '../../commands/commands/reorder-range.command';import { ReorderRangeCommandId } from '../../commands/commands/reorder-range.command';export type IMoveRowsCommand = ICommandInfo<IMoveRowsCommandParams> & { id: typeof MoveRowsCommandId };@@ -50,10 +50,10 @@ export type IDeleteRangeMoveLeftCommand = ICommandInfo<IDeleteRangeMoveLeftCommaexport type IDeleteRangeMoveUpCommand = ICommandInfo<IDeleteRangeMoveUpCommandParams> & {id: typeof DeleteRangeMoveUpCommandId;};-export type IInsertRangeMoveDownCommand = ICommandInfo<InsertRangeMoveDownCommandParams> & {+export type IInsertRangeMoveDownCommand = ICommandInfo<IInsertRangeMoveDownCommandParams> & {id: typeof InsertRangeMoveDownCommandId;};-export type IInsertRangeMoveRightCommand = ICommandInfo<InsertRangeMoveRightCommandParams> & {+export type IInsertRangeMoveRightCommand = ICommandInfo<IInsertRangeMoveRightCommandParams> & {id: typeof InsertRangeMoveRightCommandId;};diff --git a/packages/sheets/src/services/ref-range/util.ts b/packages/sheets/src/services/ref-range/util.tsindex 190415524b..8c592c01b9 100644--- a/packages/sheets/src/services/ref-range/util.ts+++ b/packages/sheets/src/services/ref-range/util.ts@@ -36,7 +36,7 @@ import type {IRemoveRowColCommand,IReorderRangeCommand,} from './type';-import { Direction, IUniverInstanceService, ObjectMatrix, queryObjectMatrix, Range, RANGE_TYPE, Rectangle } from '@univerjs/core';+import { Direction, IUniverInstanceService, mergeIntervals, ObjectMatrix, queryObjectMatrix, Range, RANGE_TYPE, Rectangle } from '@univerjs/core';import { DeleteRangeMoveLeftCommand } from '../../commands/commands/delete-range-move-left.command';import { DeleteRangeMoveUpCommand } from '../../commands/commands/delete-range-move-up.command';import { InsertRangeMoveDownCommand } from '../../commands/commands/insert-range-move-down.command';@@ -961,21 +961,46 @@ export const handleDeleteRangeMoveUpCommon = (param: IDeleteRangeMoveUpCommand,export const handleRemoveRowCommon = (param: IRemoveRowColCommandInterceptParams, targetRange: IRange) => {const ranges = param.ranges ?? [param.range];- const matrix = new ObjectMatrix();+ const removed: [number, number][] = ranges.map((range) => [range.startRow, range.endRow]);+ const mergedRemoved = mergeIntervals(removed);- Range.foreach(targetRange, (row, col) => {- matrix.setValue(row, col, 1);- });+ let targetStartRow = targetRange.startRow;+ let targetEndRow = targetRange.endRow;- ranges.forEach((range) => {- const startRow = range.startRow;- const endRow = range.endRow;- const count = endRow - startRow + 1;- matrix.removeRows(startRow, count);- });+ for (let i = mergedRemoved.length - 1; i >= 0; i--) {+ const [startRow, endRow] = mergedRemoved[i];- // TODO@zhangw try to remove queryObjectMatrix, this could case memory out of use in large range.- return queryObjectMatrix(matrix, (value) => value === 1);+ if (startRow <= targetRange.startRow && endRow >= targetRange.endRow) {+ return [];+ }++ if (endRow < targetStartRow) {+ const count = endRow - startRow + 1;+ targetStartRow -= count;+ targetEndRow -= count;+ } else if (startRow > targetEndRow) {+ // do nothing+ } else {+ const intersectStart = Math.max(startRow, targetStartRow);+ const intersectEnd = Math.min(endRow, targetEndRow);+ const intersectCount = intersectEnd - intersectStart + 1;++ targetEndRow -= intersectCount;++ if (startRow <= targetStartRow) {+ const beforeCount = intersectStart - targetStartRow;+ targetStartRow -= beforeCount;+ }+ }+ }++ return [+ {+ ...targetRange,+ startRow: targetStartRow,+ endRow: targetEndRow,+ },+ ];};export const handleInsertRowCommon = (info: ICommandInfo<IInsertRowCommandParams>, targetRange: IRange) => {diff --git a/packages/sheets/src/services/sheet-interceptor/sheet-interceptor.service.ts b/packages/sheets/src/services/sheet-interceptor/sheet-interceptor.service.tsindex f4526e8b86..a2839c2da4 100644--- a/packages/sheets/src/services/sheet-interceptor/sheet-interceptor.service.ts+++ b/packages/sheets/src/services/sheet-interceptor/sheet-interceptor.service.ts@@ -386,7 +386,7 @@ export class SheetInterceptorService extends Disposable {const key = _key ?? byNamesKey;let composed = this._composedInterceptorByKey.get(key);- if (!composed || this._interceptorsDirty) {+ if (!composed || !this._interceptorsDirty) {let interceptors = this._interceptorsByName.get(byNamesKey) as unknown as Array<IInterceptor<any, any>> | undefined;if (interceptors && filter) {interceptors = interceptors.filter(filter);