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: add validity range protection to the insert row/column command (#6553)
wpxp12345658449302f8b7
diff --git a/packages/sheets-filter/src/models/__tests__/filter-interceptor.spec.ts b/packages/sheets-filter/src/models/__tests__/filter-interceptor.spec.tsindex 4b92a12738..4e680c77a6 100644--- a/packages/sheets-filter/src/models/__tests__/filter-interceptor.spec.ts+++ b/packages/sheets-filter/src/models/__tests__/filter-interceptor.spec.ts@@ -102,7 +102,7 @@ describe('Test "Filter Interceptor"', () => {endRow: 4,type: RANGE_TYPE.COLUMN,},- direction: Direction.RIGHT,+ direction: Direction.LEFT,} as IInsertColCommandParams;await commandService.executeCommand(InsertColCommand.id, insertColCommandParams);expect(sheetsFilterService.getFilterModel('workbookId', 'worksheetId')!.getRange()).toStrictEqual({ startColumn: 2, endColumn: 3, startRow: 1, endRow: 2 });@@ -138,7 +138,7 @@ describe('Test "Filter Interceptor"', () => {endRow: 0,type: RANGE_TYPE.ROW,},- direction: Direction.DOWN,+ direction: Direction.UP,} as IInsertRowCommandParams;await commandService.executeCommand(InsertRowCommand.id, insertRowCommandParams);expect(sheetsFilterService.getFilterModel('workbookId', 'worksheetId')!.getRange()).toStrictEqual({ startColumn: 1, endColumn: 2, startRow: 2, endRow: 3 });diff --git a/packages/sheets-formula/src/services/__test__/formula-ref.spec.ts b/packages/sheets-formula/src/services/__test__/formula-ref.spec.tsindex 28001c150d..538d48bc70 100644--- a/packages/sheets-formula/src/services/__test__/formula-ref.spec.ts+++ b/packages/sheets-formula/src/services/__test__/formula-ref.spec.ts@@ -16,7 +16,7 @@import type { IRange } from '@univerjs/core';import type { ITestBed } from './util';-import { ICommandService } from '@univerjs/core';+import { Direction, ICommandService } from '@univerjs/core';import { InsertColCommand, MoveRangeCommand, RemoveColCommand } from '@univerjs/sheets';import { beforeEach, describe, expect, it } from 'vitest';import { FormulaRefRangeService } from '../formula-ref-range.service';@@ -285,6 +285,7 @@ describe('FormulaRefRangeService', () => {startRow: 0,endRow: 9999,},+ direction: Direction.LEFT,});diff --git a/packages/sheets/src/commands/commands/insert-row-col.command.ts b/packages/sheets/src/commands/commands/insert-row-col.command.tsindex 1791a276b0..63c82d0c36 100644--- a/packages/sheets/src/commands/commands/insert-row-col.command.ts+++ b/packages/sheets/src/commands/commands/insert-row-col.command.ts@@ -98,33 +98,42 @@ export const InsertRowCommand: ICommand = {export const InsertRowByRangeCommand: ICommand = {type: CommandType.COMMAND,id: 'sheet.command.insert-row-by-range',+ // eslint-disable-next-line max-lines-per-functionhandler: (accessor: IAccessor, params: IInsertRowCommandParams) => {- const commandService = accessor.get(ICommandService);- const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);- const sheetInterceptorService = accessor.get(SheetInterceptorService);-- const target = getSheetCommandTarget(univerInstanceService, params);- if (!target) return false;+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);+ if (!target) {+ throw new Error('Workbook or Worksheet not found at InsertRowByRangeCommand');+ };- const { workbook, worksheet } = target;- const { range, direction, unitId, subUnitId, cellValue } = params;+ const { workbook, worksheet, unitId, subUnitId } = target;+ const { range, direction, cellValue } = params;const { startRow, endRow } = range;range.rangeType = RANGE_TYPE.ROW;const anchorRow = direction === Direction.UP ? startRow : startRow - 1;- const height = worksheet.getRowHeight(anchorRow);+ if (anchorRow < 0 || anchorRow > worksheet.getRowCount() - 1) {+ throw new Error('Anchor row is out of bounds in InsertRowByRangeCommand');+ }++ const commandService = accessor.get(ICommandService);+ const undoRedoService = accessor.get(IUndoRedoService);+ const sheetInterceptorService = accessor.get(SheetInterceptorService);// insert row properties & undosconst insertRowParams: IInsertRowMutationParams = {unitId,subUnitId,range,- rowInfo: new Array(endRow - startRow + 1).fill(undefined).map(() => ({+ };++ const height = worksheet.getRowHeight(anchorRow);+ if (height !== worksheet.getConfig().defaultRowHeight) {+ insertRowParams.rowInfo = new Array(endRow - startRow + 1).fill(undefined).map(() => ({h: height,hd: BooleanNumber.FALSE,- })), // row height should inherit from the anchor row- };+ }));+ }+const undoRowInsertionParams: IRemoveRowsMutationParams = InsertRowMutationUndoFactory(accessor,insertRowParams@@ -409,32 +418,41 @@ export const InsertColCommand: ICommand<IInsertColCommandParams> = {export const InsertColByRangeCommand: ICommand<IInsertColCommandParams> = {type: CommandType.COMMAND,id: 'sheet.command.insert-col-by-range',-+ // eslint-disable-next-line max-lines-per-functionhandler: (accessor: IAccessor, params: IInsertColCommandParams) => {- const commandService = accessor.get(ICommandService);- const undoRedoService = accessor.get(IUndoRedoService);- const univerInstanceService = accessor.get(IUniverInstanceService);- const sheetInterceptorService = accessor.get(SheetInterceptorService);+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);+ if (!target) {+ throw new Error('Workbook or Worksheet not found at InsertColByRangeCommand');+ }- const { range, direction, subUnitId, unitId, cellValue } = params;- const { startColumn, endColumn } = params.range;+ const { workbook, worksheet, unitId, subUnitId } = target;+ const { range, direction, cellValue } = params;+ const { startColumn, endColumn } = range;range.rangeType = RANGE_TYPE.COLUMN;- const workbook = univerInstanceService.getUniverSheetInstance(params.unitId)!;- const worksheet = workbook.getSheetBySheetId(params.subUnitId)!;const anchorCol = direction === Direction.LEFT ? startColumn : startColumn - 1;- const width = worksheet.getColumnWidth(anchorCol);+ if (anchorCol < 0 || anchorCol > worksheet.getColumnCount() - 1) {+ throw new Error('Anchor column is out of bounds in InsertColByRangeCommand');+ }++ const commandService = accessor.get(ICommandService);+ const undoRedoService = accessor.get(IUndoRedoService);+ const sheetInterceptorService = accessor.get(SheetInterceptorService);// insert cols & undosconst insertColParams: IInsertColMutationParams = {unitId,subUnitId,range,- colInfo: new Array(endColumn - startColumn + 1).fill(undefined).map(() => ({+ };++ const width = worksheet.getColumnWidth(anchorCol);+ if (width !== worksheet.getConfig().defaultColumnWidth) {+ insertColParams.colInfo = new Array(endColumn - startColumn + 1).fill(undefined).map(() => ({w: width,hd: BooleanNumber.FALSE,- })),- };+ }));+ }const undoColInsertionParams: IRemoveColMutationParams = InsertColMutationUndoFactory(accessor,@@ -445,7 +463,7 @@ export const InsertColByRangeCommand: ICommand<IInsertColCommandParams> = {const undos: IMutationInfo[] = [{ id: RemoveColMutation.id, params: undoColInsertionParams }];// set range values- if (cellValue) {+ if (cellValue && Object.keys(cellValue).length > 0) {redos.push({id: SetRangeValuesMutation.id,params: {diff --git a/packages/sheets/src/commands/commands/utils/target-util.ts b/packages/sheets/src/commands/commands/utils/target-util.tsindex 7bc8c7e8f4..1f4be146b1 100644--- a/packages/sheets/src/commands/commands/utils/target-util.ts+++ b/packages/sheets/src/commands/commands/utils/target-util.ts@@ -60,9 +60,7 @@ export function getSheetCommandTarget(univerInstanceService: IUniverInstanceServif (!workbook) return null;const worksheet = subUnitId ? workbook.getSheetBySheetId(subUnitId) : workbook.getActiveSheet(true);- if (!worksheet) {- return null;- }+ if (!worksheet) return null;return {worksheet,@@ -79,9 +77,7 @@ export function getSheetMutationTarget(univerInstanceService: IUniverInstanceSerif (!workbook) return null;const worksheet = workbook.getSheetBySheetId(subUnitId);- if (!worksheet) {- return null;- }+ if (!worksheet) return null;return {worksheet,diff --git a/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts b/packages/sheets/src/commands/mutations/insert-row-col.mutation.tsindex 1f12d2419b..760d1ffae6 100644--- a/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts+++ b/packages/sheets/src/commands/mutations/insert-row-col.mutation.ts@@ -22,16 +22,15 @@ import type {IRemoveRowsMutationParams,} from '../../basics/interfaces/mutation-interface';import { CommandType, insertMatrixArray, IUniverInstanceService } from '@univerjs/core';+import { getSheetMutationTarget } from '../commands/utils/target-util';export const InsertRowMutationUndoFactory = (accessor: IAccessor,params: IInsertRowMutationParams): IRemoveRowsMutationParams => {- const univerInstanceService = accessor.get(IUniverInstanceService);- const universheet = univerInstanceService.getUniverSheetInstance(params.unitId);-- if (universheet == null) {- throw new Error('universheet is null error!');+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);+ if (!target) {+ throw new Error('Workbook or Worksheet not found at InsertRowMutationUndoFactory');}return {@@ -45,43 +44,30 @@ export const InsertRowMutation: IMutation<IInsertRowMutationParams> = {id: 'sheet.mutation.insert-row',type: CommandType.MUTATION,handler: (accessor, params) => {- const { unitId, subUnitId, range, rowInfo } = params;- const univerInstanceService = accessor.get(IUniverInstanceService);-- const universheet = univerInstanceService.getUniverSheetInstance(unitId);- if (universheet == null) {- throw new Error('universheet is null error!');- }-- const worksheet = universheet.getSheetBySheetId(subUnitId);- if (worksheet == null) {- throw new Error('worksheet is null error!');+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);+ if (!target) {+ throw new Error('Workbook or Worksheet not found at InsertRowMutation');}+ const { worksheet } = target;// TODO@wzhudev: should not expose row data and let outside modules to modify it directly// the correct way to do this is to provide a method from RowManager to modify row dataconst rowWrapper = worksheet.getRowManager().getRowData();- const defaultRowInfo = {- h: worksheet.getConfig().defaultRowHeight,- hd: 0,- };-- const rowIndex = range.startRow;- const rowCount = range.endRow - range.startRow + 1;+ const { range, rowInfo } = params;+ const { startRow, endRow } = range;- for (let j = rowIndex; j < rowIndex + rowCount; j++) {- if (rowInfo) {- insertMatrixArray(j, rowInfo[j - range.startRow] ?? defaultRowInfo, rowWrapper);- } else {- insertMatrixArray(j, defaultRowInfo, rowWrapper);+ for (let r = startRow; r <= endRow; r++) {+ if (rowInfo && rowInfo[r - startRow]) {+ insertMatrixArray(r, rowInfo[r - startRow], rowWrapper);}}- worksheet.setRowCount(worksheet.getRowCount() + rowCount);+ const insertedRowCount = endRow - startRow + 1;+ worksheet.setRowCount(worksheet.getRowCount() + insertedRowCount);// remove cells contents by directly mutating worksheetCellMatrixconst cellMatrix = worksheet.getCellMatrix();- cellMatrix.insertRows(range.startRow, rowCount);+ cellMatrix.insertRows(range.startRow, insertedRowCount);return true;},@@ -91,11 +77,9 @@ export const InsertColMutationUndoFactory = (accessor: IAccessor,params: IInsertColMutationParams): IRemoveColMutationParams => {- const univerInstanceService = accessor.get(IUniverInstanceService);- const universheet = univerInstanceService.getUniverSheetInstance(params.unitId);-- if (universheet == null) {- throw new Error('universheet is null error!');+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);+ if (!target) {+ throw new Error('Workbook or Worksheet not found at InsertColMutationUndoFactory');}return {@@ -109,40 +93,28 @@ export const InsertColMutation: IMutation<IInsertColMutationParams> = {id: 'sheet.mutation.insert-col',type: CommandType.MUTATION,handler: (accessor, params) => {- const univerInstanceService = accessor.get(IUniverInstanceService);- const universheet = univerInstanceService.getUniverSheetInstance(params.unitId);-- if (universheet == null) {- throw new Error('universheet is null error!');- }+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);+ if (!target) {+ throw new Error('Workbook or Worksheet not found at InsertColMutation');+ };- const worksheet = universheet.getSheetBySheetId(params.subUnitId);- if (!worksheet) return false;- const manager = worksheet.getColumnManager();+ const { worksheet } = target;+ const columnWrapper = worksheet.getColumnManager().getColumnData();const { range, colInfo } = params;- const columnPrimitive = manager.getColumnData();- const columnWrapper = columnPrimitive;-- const colIndex = range.startColumn;- const colCount = range.endColumn - range.startColumn + 1;- const defaultColWidth = worksheet.getConfig().defaultColumnWidth;- for (let j = colIndex; j < colIndex + colCount; j++) {- const defaultColInfo = {- w: defaultColWidth,- hd: 0,- };- if (colInfo) {- insertMatrixArray(j, colInfo[j - range.startColumn] ?? defaultColInfo, columnWrapper);- } else {- insertMatrixArray(j, defaultColInfo, columnWrapper);+ const { startColumn, endColumn } = range;++ for (let c = startColumn; c <= endColumn; c++) {+ if (colInfo && colInfo[c - startColumn]) {+ insertMatrixArray(c, colInfo[c - startColumn], columnWrapper);}}- worksheet.setColumnCount(worksheet.getColumnCount() + range.endColumn - range.startColumn + 1);+ const insertedColumnCount = endColumn - startColumn + 1;+ worksheet.setColumnCount(worksheet.getColumnCount() + insertedColumnCount);// remove cells contents by directly mutating worksheetCellMatrixconst cellMatrix = worksheet.getCellMatrix();- cellMatrix.insertColumns(range.startColumn, colCount);+ cellMatrix.insertColumns(range.startColumn, insertedColumnCount);return true;},