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.

feat(facade): supplement setRowCount setColumnCount facade (#5025)

wpxp12345625cc5fd05e42
diff --git a/packages/sheets/src/basics/const/command-listener-const.ts b/packages/sheets/src/basics/const/command-listener-const.ts
index 8b2fb17906..9dfb39f084 100644
--- a/packages/sheets/src/basics/const/command-listener-const.ts
+++ b/packages/sheets/src/basics/const/command-listener-const.ts
@@ -52,7 +52,9 @@ import { SetRangeValuesMutation } from '../../commands/mutations/set-range-value
import { SetRowDataMutation } from '../../commands/mutations/set-row-data.mutation';
import { SetRowHiddenMutation, SetRowVisibleMutation } from '../../commands/mutations/set-row-visible.mutation';
import { SetWorksheetColWidthMutation } from '../../commands/mutations/set-worksheet-col-width.mutation';
+import { SetWorksheetColumnCountMutation } from '../../commands/mutations/set-worksheet-column-count.mutation';
import { SetWorksheetDefaultStyleMutation } from '../../commands/mutations/set-worksheet-default-style.mutation';
+import { SetWorksheetRowCountMutation } from '../../commands/mutations/set-worksheet-row-count.mutation';
import {
SetWorksheetRowAutoHeightMutation,
SetWorksheetRowHeightMutation,
@@ -121,6 +123,8 @@ export const COMMAND_LISTENER_SKELETON_CHANGE = [
RemoveRowMutation.id,
ToggleGridlinesMutation.id,
SetGridlinesColorMutation.id,
+ SetWorksheetRowCountMutation.id,
+ SetWorksheetColumnCountMutation.id,
];
export const COMMAND_LISTENER_VALUE_CHANGE = [
diff --git a/packages/sheets/src/commands/commands/set-worksheet-column-count.command.ts b/packages/sheets/src/commands/commands/set-worksheet-column-count.command.ts
new file mode 100644
index 0000000000..b9d8d63ee9
--- /dev/null
+++ b/packages/sheets/src/commands/commands/set-worksheet-column-count.command.ts
@@ -0,0 +1,70 @@
+/**
+ * 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.
+ */
+
+import type { IAccessor, ICommand, Workbook } from '@univerjs/core';
+import type { ISetWorksheetColumnCountMutationParams } from '../mutations/set-worksheet-column-count.mutation';
+import {
+ CommandType,
+ ICommandService,
+ IUndoRedoService,
+ IUniverInstanceService,
+ UniverInstanceType,
+} from '@univerjs/core';
+import { SetWorksheetColumnCountMutation, SetWorksheetColumnCountMutationFactory } from '../mutations/set-worksheet-column-count.mutation';
+import { getSheetCommandTarget } from './utils/target-util';
+
+export const SetWorksheetColumnCountCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-worksheet-column-count',
+
+ handler: (accessor: IAccessor, params: ISetWorksheetColumnCountMutationParams) => {
+ const { unitId, subUnitId, columnCount } = params;
+
+ const commandService = accessor.get(ICommandService);
+ const undoRedoService = accessor.get(IUndoRedoService);
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));
+ if (!target) return false;
+
+ const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
+ if (!workbook) return false;
+
+ const worksheet = workbook.getSheetBySheetId(subUnitId);
+ if (!worksheet) return false;
+
+ const redoMutationParams: ISetWorksheetColumnCountMutationParams = {
+ unitId,
+ subUnitId,
+ columnCount,
+ };
+
+ const undoMutationParams = SetWorksheetColumnCountMutationFactory(accessor, redoMutationParams);
+
+ const result = commandService.syncExecuteCommand(SetWorksheetColumnCountMutation.id, redoMutationParams);
+
+ if (result) {
+ undoRedoService.pushUndoRedo({
+ unitID: unitId,
+ undoMutations: [{ id: SetWorksheetColumnCountMutation.id, params: undoMutationParams }],
+ redoMutations: [{ id: SetWorksheetColumnCountMutation.id, params: redoMutationParams }],
+ });
+ return true;
+ }
+
+ return false;
+ },
+};
diff --git a/packages/sheets/src/commands/commands/set-worksheet-row-count.command.ts b/packages/sheets/src/commands/commands/set-worksheet-row-count.command.ts
new file mode 100644
index 0000000000..906666ffd5
--- /dev/null
+++ b/packages/sheets/src/commands/commands/set-worksheet-row-count.command.ts
@@ -0,0 +1,70 @@
+/**
+ * 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.
+ */
+
+import type { IAccessor, ICommand, Workbook } from '@univerjs/core';
+import type { ISetWorksheetRowCountMutationParams } from '../mutations/set-worksheet-row-count.mutation';
+import {
+ CommandType,
+ ICommandService,
+ IUndoRedoService,
+ IUniverInstanceService,
+ UniverInstanceType,
+} from '@univerjs/core';
+import { SetWorksheetRowCountMutation, SetWorksheetRowCountMutationFactory } from '../mutations/set-worksheet-row-count.mutation';
+import { getSheetCommandTarget } from './utils/target-util';
+
+export const SetWorksheetRowCountCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-worksheet-row-count',
+
+ handler: (accessor: IAccessor, params: ISetWorksheetRowCountMutationParams) => {
+ const { unitId, subUnitId, rowCount } = params;
+
+ const commandService = accessor.get(ICommandService);
+ const undoRedoService = accessor.get(IUndoRedoService);
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService));
+ if (!target) return false;
+
+ const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
+ if (!workbook) return false;
+
+ const worksheet = workbook.getSheetBySheetId(subUnitId);
+ if (!worksheet) return false;
+
+ const redoMutationParams: ISetWorksheetRowCountMutationParams = {
+ unitId,
+ subUnitId,
+ rowCount,
+ };
+
+ const undoMutationParams = SetWorksheetRowCountMutationFactory(accessor, redoMutationParams);
+
+ const result = commandService.syncExecuteCommand(SetWorksheetRowCountMutation.id, redoMutationParams);
+
+ if (result) {
+ undoRedoService.pushUndoRedo({
+ unitID: unitId,
+ undoMutations: [{ id: SetWorksheetRowCountMutation.id, params: undoMutationParams }],
+ redoMutations: [{ id: SetWorksheetRowCountMutation.id, params: redoMutationParams }],
+ });
+ return true;
+ }
+
+ return false;
+ },
+};
diff --git a/packages/sheets/src/commands/mutations/set-worksheet-column-count.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-column-count.mutation.ts
new file mode 100644
index 0000000000..0d75d71aa5
--- /dev/null
+++ b/packages/sheets/src/commands/mutations/set-worksheet-column-count.mutation.ts
@@ -0,0 +1,66 @@
+/**
+ * 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.
+ */
+
+import type { IAccessor, IMutation } from '@univerjs/core';
+import { CommandType, IUniverInstanceService } from '@univerjs/core';
+import { getSheetMutationTarget } from '../commands/utils/target-util';
+
+export interface ISetWorksheetColumnCountMutationParams {
+ unitId: string;
+ subUnitId: string;
+ columnCount: number;
+}
+
+export const SetWorksheetColumnCountMutationFactory = (
+ accessor: IAccessor,
+ params: ISetWorksheetColumnCountMutationParams
+): ISetWorksheetColumnCountMutationParams => {
+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) {
+ throw new Error('[SetWorksheetColumnCountMutationFactory]: worksheet is null error!');
+ }
+
+ const { worksheet } = target;
+ return {
+ unitId: params.unitId,
+ subUnitId: worksheet.getSheetId(),
+ columnCount: worksheet.getColumnCount(),
+ };
+};
+
+export const SetWorksheetColumnCountMutation: IMutation<ISetWorksheetColumnCountMutationParams> = {
+ id: 'sheet.mutation.set-worksheet-column-count',
+ type: CommandType.MUTATION,
+ handler: (accessor, params) => {
+ const { unitId, subUnitId, columnCount } = params;
+
+ const workbook = accessor.get(IUniverInstanceService).getUniverSheetInstance(unitId);
+
+ if (!workbook) {
+ return false;
+ }
+
+ const worksheet = workbook.getSheetBySheetId(subUnitId);
+
+ if (!worksheet) {
+ return false;
+ }
+
+ worksheet.setColumnCount(columnCount);
+
+ return true;
+ },
+};
diff --git a/packages/sheets/src/commands/mutations/set-worksheet-row-count.mutation.ts b/packages/sheets/src/commands/mutations/set-worksheet-row-count.mutation.ts
new file mode 100644
index 0000000000..e82cd89b6e
--- /dev/null
+++ b/packages/sheets/src/commands/mutations/set-worksheet-row-count.mutation.ts
@@ -0,0 +1,66 @@
+/**
+ * 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.
+ */
+
+import type { IAccessor, IMutation } from '@univerjs/core';
+import { CommandType, IUniverInstanceService } from '@univerjs/core';
+import { getSheetMutationTarget } from '../commands/utils/target-util';
+
+export interface ISetWorksheetRowCountMutationParams {
+ unitId: string;
+ subUnitId: string;
+ rowCount: number;
+}
+
+export const SetWorksheetRowCountMutationFactory = (
+ accessor: IAccessor,
+ params: ISetWorksheetRowCountMutationParams
+): ISetWorksheetRowCountMutationParams => {
+ const target = getSheetMutationTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) {
+ throw new Error('[SetWorksheetRowCountMutationFactory]: worksheet is null error!');
+ }
+
+ const { worksheet } = target;
+ return {
+ unitId: params.unitId,
+ subUnitId: worksheet.getSheetId(),
+ rowCount: worksheet.getRowCount(),
+ };
+};
+
+export const SetWorksheetRowCountMutation: IMutation<ISetWorksheetRowCountMutationParams> = {
+ id: 'sheet.mutation.set-worksheet-row-count',
+ type: CommandType.MUTATION,
+ handler: (accessor, params) => {
+ const { unitId, subUnitId, rowCount } = params;
+
+ const workbook = accessor.get(IUniverInstanceService).getUniverSheetInstance(unitId);
+
+ if (!workbook) {
+ return false;
+ }
+
+ const worksheet = workbook.getSheetBySheetId(subUnitId);
+
+ if (!worksheet) {
+ return false;
+ }
+
+ worksheet.setRowCount(rowCount);
+
+ return true;
+ },
+};
diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts
index 1d9ed6f269..cb2aa90588 100644
--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts
+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts
@@ -17,7 +17,6 @@
import type { IDisposable, IMutation, IStyleData } from '@univerjs/core';
import { Disposable, ICommandService, IConfigService, Optional } from '@univerjs/core';
import { DataSyncPrimaryController } from '@univerjs/rpc';
-
import { AddRangeProtectionCommand } from '../commands/commands/add-range-protection.command';
import { AddWorksheetProtectionCommand } from '../commands/commands/add-worksheet-protection.command';
import { SetWorksheetRangeThemeStyleCommand } from '../commands/commands/add-worksheet-range-theme.command';
@@ -96,12 +95,14 @@ import { SetTabColorCommand } from '../commands/commands/set-tab-color.command';
import { SetWorkbookNameCommand } from '../commands/commands/set-workbook-name.command';
import { SetWorksheetActivateCommand } from '../commands/commands/set-worksheet-activate.command';
import { DeltaColumnWidthCommand, SetColWidthCommand } from '../commands/commands/set-worksheet-col-width.command';
+import { SetWorksheetColumnCountCommand } from '../commands/commands/set-worksheet-column-count.command';
import { SetWorksheetDefaultStyleCommand } from '../commands/commands/set-worksheet-default-style.command';
import { SetWorksheetHideCommand } from '../commands/commands/set-worksheet-hide.command';
import { SetWorksheetNameCommand } from '../commands/commands/set-worksheet-name.command';
import { SetWorksheetOrderCommand } from '../commands/commands/set-worksheet-order.command';
import { SetWorksheetPermissionPointsCommand } from '../commands/commands/set-worksheet-permission-points.command';
import { SetWorksheetProtectionCommand } from '../commands/commands/set-worksheet-protection.command';
+import { SetWorksheetRowCountCommand } from '../commands/commands/set-worksheet-row-count.command';
import {
DeltaRowHeightCommand,
SetRowHeightCommand,
@@ -120,11 +121,9 @@ import { DeleteRangeProtectionMutation } from '../commands/mutations/delete-rang
import { DeleteWorksheetProtectionMutation } from '../commands/mutations/delete-worksheet-protection.mutation';
import { DeleteWorksheetRangeThemeStyleMutation } from '../commands/mutations/delete-worksheet-range-theme.mutation';
import { EmptyMutation } from '../commands/mutations/empty.mutation';
-
import { InsertColMutation, InsertRowMutation } from '../commands/mutations/insert-row-col.mutation';
import { InsertSheetMutation } from '../commands/mutations/insert-sheet.mutation';
import { MoveRangeMutation } from '../commands/mutations/move-range.mutation';
-
import { MoveColsMutation, MoveRowsMutation } from '../commands/mutations/move-rows-cols.mutation';
import { RemoveNumfmtMutation, SetNumfmtMutation } from '../commands/mutations/numfmt-mutation';
import { RegisterWorksheetRangeThemeStyleMutation } from '../commands/mutations/register-range-theme.mutation';
@@ -143,12 +142,14 @@ import { SetRowHiddenMutation, SetRowVisibleMutation } from '../commands/mutatio
import { SetTabColorMutation } from '../commands/mutations/set-tab-color.mutation';
import { SetWorkbookNameMutation } from '../commands/mutations/set-workbook-name.mutation';
import { SetWorksheetColWidthMutation } from '../commands/mutations/set-worksheet-col-width.mutation';
+import { SetWorksheetColumnCountMutation } from '../commands/mutations/set-worksheet-column-count.mutation';
import { SetWorksheetDefaultStyleMutation } from '../commands/mutations/set-worksheet-default-style.mutation';
import { SetWorksheetHideMutation } from '../commands/mutations/set-worksheet-hide.mutation';
import { SetWorksheetNameMutation } from '../commands/mutations/set-worksheet-name.mutation';
import { SetWorksheetOrderMutation } from '../commands/mutations/set-worksheet-order.mutation';
import { SetWorksheetPermissionPointsMutation } from '../commands/mutations/set-worksheet-permission-points.mutation';
import { SetWorksheetProtectionMutation } from '../commands/mutations/set-worksheet-protection.mutation';
+import { SetWorksheetRowCountMutation } from '../commands/mutations/set-worksheet-row-count.mutation';
import {
SetWorksheetRowAutoHeightMutation,
SetWorksheetRowHeightMutation,
@@ -294,6 +295,11 @@ export class BasicWorksheetController extends Disposable implements IDisposable
SetWorksheetColWidthMutation,
// SetWorksheetColIsAutoWidthCommand,
+ SetWorksheetRowCountCommand,
+ SetWorksheetRowCountMutation,
+ SetWorksheetColumnCountCommand,
+ SetWorksheetColumnCountMutation,
+
SelectRangeCommand,
SetSelectionsOperation,
ScrollToCellOperation,
diff --git a/packages/sheets/src/facade/f-worksheet.ts b/packages/sheets/src/facade/f-worksheet.ts
index 177120cdd5..8f10f1c1df 100644
--- a/packages/sheets/src/facade/f-worksheet.ts
+++ b/packages/sheets/src/facade/f-worksheet.ts
@@ -21,7 +21,7 @@ import type { FWorkbook } from './f-workbook';
import { BooleanNumber, Direction, 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, SetWorksheetDefaultStyleMutation, SetWorksheetHideCommand, SetWorksheetNameCommand, SetWorksheetRowIsAutoHeightCommand, SetWorksheetRowIsAutoHeightMutation, SetWorksheetShowCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets';
+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';
import { FDefinedNameBuilder } from './f-defined-name';
import { FRange } from './f-range';
import { FSelection } from './f-selection';
@@ -2509,4 +2509,48 @@ export class FWorksheet extends FBaseInitialable {
});
return this;
}
+
+ /**
+ * Sets the number of rows in the worksheet.
+ * @param {number} rowCount - The number of rows to set.
+ * @returns {FWorksheet} Returns the current worksheet instance for method chaining.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ *
+ * // Set the number of rows in the worksheet to 40
+ * fWorkSheet.setRowCount(40);
+ * ```
+ */
+ setRowCount(rowCount: number): FWorksheet {
+ this._commandService.syncExecuteCommand(SetWorksheetRowCountCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ rowCount,
+ });
+ return this;
+ }
+
+ /**
+ * Sets the number of columns in the worksheet.
+ * @param {number} columnCount - The number of columns to set.
+ * @returns {FWorksheet} Returns the current worksheet instance for method chaining.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ *
+ * // Set the number of columns in the worksheet to 10
+ * fWorkSheet.setColumnCount(10);
+ * ```
+ */
+ setColumnCount(columnCount: number): FWorksheet {
+ this._commandService.syncExecuteCommand(SetWorksheetColumnCountCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ columnCount,
+ });
+ return this;
+ }
}
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index 3e348ce34a..7d4e320aff 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -454,4 +454,9 @@ export type { ICellOverGridPosition, ISheetOverGridPosition } from './basics/cel
export { SheetSkeletonService } from './skeleton/skeleton.service';
+export { SetWorksheetRowCountCommand } from './commands/commands/set-worksheet-row-count.command';
+export { SetWorksheetColumnCountCommand } from './commands/commands/set-worksheet-column-count.command';
+export { type ISetWorksheetRowCountMutationParams, SetWorksheetRowCountMutation } from './commands/mutations/set-worksheet-row-count.mutation';
+export { type ISetWorksheetColumnCountMutationParams, SetWorksheetColumnCountMutation } from './commands/mutations/set-worksheet-column-count.mutation';
+
// #endregion