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(sheet): insert range (#1093)
Dushusirc50012b97425
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 e6831b328b..ca90d6e461 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@@ -116,53 +116,32 @@ export const InsertRangeMoveDownCommand: ICommand = {undoMutations.push({ id: DeleteRangeMutation.id, params: deleteRangeMutationParams });- // 2. insert row- // Only required when the last row has a value, and the row height is set to the row height of the last row- let hasValueInLastRow = false;- let lastRowHeight = 0;- const lastRowIndex = worksheet.getMaxRows() - 1;- const { startRow, endRow, startColumn, endColumn } = range;- for (let column = startColumn; column <= endColumn; column++) {- const lastCell = worksheet.getCell(lastRowIndex, column);- const lastCellValue = lastCell?.v;- if (lastCellValue && lastCellValue !== '') {- hasValueInLastRow = true;- lastRowHeight = worksheet.getRowHeight(lastRowIndex);- break;- }- }+ const { startRow, endRow } = range;- // There may be overlap and deduplication is required- const rowsCount = endRow - startRow + 1;- if (hasValueInLastRow) {- const lastRowRange = {- startRow: lastRowIndex,- endRow: lastRowIndex,- startColumn: range.startColumn,- endColumn: range.endColumn,- };- const insertRowParams: IInsertRowMutationParams = {- unitId,- subUnitId,- range: lastRowRange,- rowInfo: new Array(rowsCount).fill(undefined).map(() => ({- h: lastRowHeight,- hd: BooleanNumber.FALSE,- })),- };-- redoMutations.push({- id: InsertRowMutation.id,- params: insertRowParams,- });+ const anchorRow = startRow - 1;+ const height = worksheet.getRowHeight(anchorRow);- const undoRowInsertionParams: IRemoveRowsMutationParams = InsertRowMutationUndoFactory(- accessor,- insertRowParams- );+ const insertRowParams: IInsertRowMutationParams = {+ unitId,+ subUnitId,+ range,+ rowInfo: new Array(endRow - startRow + 1).fill(undefined).map(() => ({+ h: height,+ hd: BooleanNumber.FALSE,+ })),+ };- undoMutations.push({ id: RemoveRowMutation.id, params: undoRowInsertionParams });- }+ redoMutations.push({+ id: InsertRowMutation.id,+ params: insertRowParams,+ });++ const undoRowInsertionParams: IRemoveRowsMutationParams = InsertRowMutationUndoFactory(+ accessor,+ insertRowParams+ );++ undoMutations.push({ id: RemoveRowMutation.id, params: undoRowInsertionParams });const sheetInterceptor = sheetInterceptorService.onCommandExecute({id: InsertRangeMoveDownCommand.id,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 660bb296b5..fcc2a25aaf 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@@ -111,54 +111,32 @@ export const InsertRangeMoveRightCommand: ICommand = {undoMutations.push({ id: DeleteRangeMutation.id, params: deleteRangeMutationParams });- // 2. insert column- // Only required when the last column has a value, and the column width is set to the column width of the last column- let hasValueInLastColumn = false;- let lastColumnWidth = 0;- const lastColumnIndex = worksheet.getMaxColumns() - 1;- const { startRow, endRow, startColumn, endColumn } = range;-- for (let row = startRow; row <= endRow; row++) {- const lastCell = worksheet.getCell(row, lastColumnIndex);- const lastCellValue = lastCell?.v;- if (lastCellValue && lastCellValue !== '') {- hasValueInLastColumn = true;- lastColumnWidth = worksheet.getColumnWidth(lastColumnIndex);- break;- }- }+ const { startColumn, endColumn } = range;+ const anchorCol = startColumn - 1;+ const width = worksheet.getColumnWidth(anchorCol);- // There may be overlap and deduplication is required- const columnsCount = endColumn - startColumn + 1;- if (hasValueInLastColumn) {- const lastColumnRange = {- startRow: range.startRow,- endRow: range.endRow,- startColumn: lastColumnIndex,- endColumn: lastColumnIndex,- };- const insertColParams: IInsertColMutationParams = {- unitId,- subUnitId,- range: lastColumnRange,- colInfo: new Array(columnsCount).fill(undefined).map(() => ({- w: lastColumnWidth,- hd: BooleanNumber.FALSE,- })),- };-- redoMutations.push({- id: InsertColMutation.id,- params: insertColParams,- });+ const insertColParams: IInsertColMutationParams = {+ unitId,+ subUnitId,+ range,+ colInfo: new Array(endColumn - startColumn + 1).fill(undefined).map(() => ({+ w: width,+ hd: BooleanNumber.FALSE,+ })),+ };- const undoColInsertionParams: IRemoveColMutationParams = InsertColMutationUndoFactory(- accessor,- insertColParams- );+ redoMutations.push({+ id: InsertColMutation.id,+ params: insertColParams,+ });++ const undoColInsertionParams: IRemoveColMutationParams = InsertColMutationUndoFactory(+ accessor,+ insertColParams+ );++ undoMutations.push({ id: RemoveColMutation.id, params: undoColInsertionParams });- undoMutations.push({ id: RemoveColMutation.id, params: undoColInsertionParams });- }const sheetInterceptor = sheetInterceptorService.onCommandExecute({id: InsertRangeMoveRightCommand.id,params: { range } as InsertRangeMoveRightCommandParams,