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): add facade comments (#4264)

VicKunb808f9830ad5
diff --git a/packages/core/src/sheets/typedef.ts b/packages/core/src/sheets/typedef.ts
index c211cd11c3..82ec74fa47 100644
--- a/packages/core/src/sheets/typedef.ts
+++ b/packages/core/src/sheets/typedef.ts
@@ -137,6 +137,11 @@ export interface IWorksheetData {
};
showGridlines: BooleanNumber;
+ /**
+ * Color of the gridlines.
+ */
+ gridlinesColor?: string;
+
rightToLeft: BooleanNumber;
}
diff --git a/packages/core/src/sheets/worksheet.ts b/packages/core/src/sheets/worksheet.ts
index 8ef32e35f7..8d0eb11fed 100644
--- a/packages/core/src/sheets/worksheet.ts
+++ b/packages/core/src/sheets/worksheet.ts
@@ -679,7 +679,7 @@ export class Worksheet {
/**
* Returns true if the sheet's gridlines are hidden; otherwise returns false. Gridlines are visible by default.
- * @returns Gridlines Hidden Status
+ * @returns {boolean} Gridlines Hidden Status.
*/
hasHiddenGridlines(): boolean {
const { _snapshot: _config } = this;
@@ -691,6 +691,14 @@ export class Worksheet {
return false;
}
+ /**
+ * Returns the color of the gridlines, or undefined if the gridlines are not colored.
+ * @returns {string | undefined} returns the color of the gridlines, or undefined if the gridlines are default.
+ */
+ getGridlinesColor(): string | undefined {
+ return this.getConfig().gridlinesColor;
+ }
+
/**
* Gets the sheet tab color, or null if the sheet tab has no color.
* @returns the sheet tab color or null
@@ -815,6 +823,10 @@ export class Worksheet {
return this._cellData.getRange().endColumn;
}
+ getDataRangeScope(): IRange {
+ return this._cellData.getStartEndScope();
+ }
+
cellHasValue(value: ICellData) {
return value && (value.v !== undefined || value.f !== undefined || value.p !== undefined);
}
diff --git a/packages/engine-render/src/components/sheets/sheet-skeleton.ts b/packages/engine-render/src/components/sheets/sheet-skeleton.ts
index 54ec9af856..bd98921485 100644
--- a/packages/engine-render/src/components/sheets/sheet-skeleton.ts
+++ b/packages/engine-render/src/components/sheets/sheet-skeleton.ts
@@ -240,6 +240,7 @@ export class SpreadsheetSkeleton extends Skeleton {
private _handleFontMatrix = new ObjectMatrix<boolean>();
private _showGridlines: BooleanNumber = BooleanNumber.TRUE;
+ private _gridlinesColor: string | undefined = undefined;
private _marginTop: number = 0;
private _marginLeft: number = 0;
@@ -329,6 +330,10 @@ export class SpreadsheetSkeleton extends Skeleton {
return this._showGridlines;
}
+ get gridlinesColor(): string | undefined {
+ return this._gridlinesColor;
+ }
+
get mergeData(): IRange[] {
return this._worksheetData.mergeData;
}
@@ -837,6 +842,7 @@ export class SpreadsheetSkeleton extends Skeleton {
rowHeader,
columnHeader,
showGridlines,
+ gridlinesColor,
} = this._worksheetData;
const { rowTotalHeight, rowHeightAccumulation } = this._generateRowMatrixCache(
@@ -859,6 +865,7 @@ export class SpreadsheetSkeleton extends Skeleton {
this._columnTotalWidth = columnTotalWidth;
this._columnWidthAccumulation = columnWidthAccumulation;
this._showGridlines = showGridlines;
+ this._gridlinesColor = gridlinesColor;
this.makeDirty(false);
}
diff --git a/packages/engine-render/src/components/sheets/spreadsheet.ts b/packages/engine-render/src/components/sheets/spreadsheet.ts
index cc53ef1844..5cc52b5f2b 100644
--- a/packages/engine-render/src/components/sheets/spreadsheet.ts
+++ b/packages/engine-render/src/components/sheets/spreadsheet.ts
@@ -541,7 +541,7 @@ export class Spreadsheet extends SheetComponent {
return;
}
- const { rowColumnSegment, overflowCache, showGridlines } = spreadsheetSkeleton;
+ const { rowColumnSegment, overflowCache, showGridlines, gridlinesColor } = spreadsheetSkeleton;
const mergeCellRanges = spreadsheetSkeleton.getCurrentRowColumnSegmentMergeData(rowColumnSegment);
const { startRow, endRow, startColumn, endColumn } = rowColumnSegment;
if (!spreadsheetSkeleton || showGridlines === BooleanNumber.FALSE || this._forceDisableGridlines) {
@@ -562,7 +562,7 @@ export class Spreadsheet extends SheetComponent {
ctx.setLineWidthByPrecision(1);
- ctx.strokeStyle = ctx.renderConfig.gridlineColor ?? getColor([214, 216, 219]);
+ ctx.strokeStyle = gridlinesColor ?? ctx.renderConfig.gridlinesColor ?? getColor([214, 216, 219]);
const columnWidthAccumulationLength = columnWidthAccumulation.length;
const rowHeightAccumulationLength = rowHeightAccumulation.length;
diff --git a/packages/sheets-ui/src/facade/f-worksheet.ts b/packages/sheets-ui/src/facade/f-worksheet.ts
index d26a3b6fd6..a79c230295 100644
--- a/packages/sheets-ui/src/facade/f-worksheet.ts
+++ b/packages/sheets-ui/src/facade/f-worksheet.ts
@@ -14,16 +14,43 @@
* limitations under the License.
*/
+import { ICommandService } from '@univerjs/core';
import { IRenderManagerService } from '@univerjs/engine-render';
import { FWorksheet } from '@univerjs/sheets/facade';
+import { ChangeZoomRatioCommand } from '../commands/commands/set-zoom-ratio.command';
import { SheetSkeletonManagerService } from '../services/sheet-skeleton-manager.service';
export interface IFWorksheetSkeletonMixin {
-
/**
* Refresh the canvas.
*/
refreshCanvas(): void;
+ /**
+ * Set zoom ratio of the worksheet.
+ * @param {number} zoomRatio The zoom ratio to set.It should be in the range of 10 to 400.
+ * @returns True if the command was successful, false otherwise.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorksheet = fWorkbook.getActiveSheet();
+ * fWorksheet.zoom(200);
+ * const zoomRatio = fWorksheet.getZoom();
+ * console.log(zoomRatio); // 200
+ * ```
+ */
+ zoom(zoomRatio: number): Promise<boolean>;
+ /**
+ * Get the zoom ratio of the worksheet.
+ * @returns The zoom ratio of the worksheet.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorksheet = fWorkbook.getActiveSheet();
+ * const zoomRatio = fWorksheet.getZoom();
+ * console.log(zoomRatio);
+ * ```
+ */
+ getZoom(): number;
}
export class FWorksheetSkeletonMixin extends FWorksheet implements IFWorksheetSkeletonMixin {
@@ -46,6 +73,19 @@ export class FWorksheetSkeletonMixin extends FWorksheet implements IFWorksheetSk
mainComponent.makeDirty();
}
+
+ override zoom(zoomRatio: number): Promise<boolean> {
+ const commandService = this._injector.get(ICommandService);
+ return commandService.executeCommand(ChangeZoomRatioCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ zoomRatio,
+ });
+ }
+
+ override getZoom(): number {
+ return this._worksheet.getZoomRatio();
+ }
}
FWorksheet.extend(FWorksheetSkeletonMixin);
diff --git a/packages/sheets/src/basics/const/command-listener-const.ts b/packages/sheets/src/basics/const/command-listener-const.ts
index 88befb86af..a3148b7b2b 100644
--- a/packages/sheets/src/basics/const/command-listener-const.ts
+++ b/packages/sheets/src/basics/const/command-listener-const.ts
@@ -23,6 +23,7 @@ import { RemoveWorksheetMergeMutation } from '../../commands/mutations/remove-wo
import { ReorderRangeMutation } from '../../commands/mutations/reorder-range.mutation';
import { SetColDataMutation } from '../../commands/mutations/set-col-data.mutation';
import { SetColHiddenMutation, SetColVisibleMutation } from '../../commands/mutations/set-col-visible.mutation';
+import { SetGridlinesColorMutation } from '../../commands/mutations/set-gridlines-color.mutation';
import { SetRangeValuesMutation } from '../../commands/mutations/set-range-values.mutation';
import { SetRowDataMutation } from '../../commands/mutations/set-row-data.mutation';
import { SetRowHiddenMutation, SetRowVisibleMutation } from '../../commands/mutations/set-row-visible.mutation';
@@ -56,6 +57,7 @@ export const COMMAND_LISTENER_SKELETON_CHANGE = [
RemoveColMutation.id,
RemoveRowMutation.id,
ToggleGridlinesMutation.id,
+ SetGridlinesColorMutation.id,
];
export const COMMAND_LISTENER_VALUE_CHANGE = [
diff --git a/packages/sheets/src/commands/commands/__tests__/set-gridlines-command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-gridlines-command.spec.ts
new file mode 100644
index 0000000000..dfc77ae176
--- /dev/null
+++ b/packages/sheets/src/commands/commands/__tests__/set-gridlines-command.spec.ts
@@ -0,0 +1,96 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * 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 { Injector, Univer, Workbook } from '@univerjs/core';
+import { ICommandService, IUniverInstanceService, RedoCommand, UndoCommand, UniverInstanceType } from '@univerjs/core';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
+
+import { SetGridlinesColorMutation } from '../../mutations/set-gridlines-color.mutation';
+import { SetGridlinesColorCommand } from '../set-gridlines-color.command';
+import { createCommandTestBed } from './create-command-test-bed';
+
+describe('Test set worksheet default style commands', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+
+ beforeEach(() => {
+ const testBed = createCommandTestBed();
+ univer = testBed.univer;
+ get = testBed.get;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(SetGridlinesColorCommand);
+ commandService.registerCommand(SetGridlinesColorMutation);
+ });
+
+ afterEach(() => {
+ univer.dispose();
+ });
+
+ describe('set worksheet grid line color', () => {
+ it('correct situation', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
+ const worksheet = workbook.getSheetBySheetId('sheet1');
+ if (!workbook) throw new Error('This is an error');
+
+ const color = '#ff0000';
+
+ expect(
+ await commandService.executeCommand(SetGridlinesColorCommand.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ color,
+ })
+ ).toBeTruthy();
+
+ expect(worksheet?.getGridlinesColor()).toEqual(color);
+
+ // undo;
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(worksheet?.getGridlinesColor()).toBeUndefined();
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(worksheet?.getGridlinesColor()).toEqual(color);
+ });
+ });
+ it('reset worksheet grid line color', async () => {
+ const workbook = get(IUniverInstanceService).getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
+ const worksheet = workbook.getSheetBySheetId('sheet1');
+ if (!workbook) throw new Error('This is an error');
+
+ const color = '#ff0000';
+
+ expect(
+ await commandService.executeCommand(SetGridlinesColorCommand.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ color,
+ })
+ ).toBeTruthy();
+ expect(worksheet?.getGridlinesColor()).toEqual(color);
+
+ expect(
+ await commandService.executeCommand(SetGridlinesColorCommand.id, {
+ unitId: 'test',
+ subUnitId: 'sheet1',
+ color: undefined,
+ })
+ ).toBeTruthy();
+
+ expect(worksheet?.getGridlinesColor()).toBeUndefined();
+ });
+});
diff --git a/packages/sheets/src/commands/commands/clear-selection-all.command.ts b/packages/sheets/src/commands/commands/clear-selection-all.command.ts
index baafa72bf8..3c69a686ae 100644
--- a/packages/sheets/src/commands/commands/clear-selection-all.command.ts
+++ b/packages/sheets/src/commands/commands/clear-selection-all.command.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IAccessor, ICommand, IMutationInfo, Workbook } from '@univerjs/core';
+import type { IAccessor, ICommand, IMutationInfo, IRange, Workbook } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
import {
@@ -30,13 +30,19 @@ import { SheetsSelectionsService } from '../../services/selections/selection.ser
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
+interface IClearSelectionAllCommandParams {
+ unitId?: string;
+ subUnitId?: string;
+ ranges?: IRange[];
+}
+
/**
* The command to clear all in current selected ranges.
*/
export const ClearSelectionAllCommand: ICommand = {
id: 'sheet.command.clear-selection-all',
type: CommandType.COMMAND,
- handler: async (accessor: IAccessor) => {
+ handler: async (accessor: IAccessor, params: IClearSelectionAllCommandParams) => {
const univerInstanceService = accessor.get(IUniverInstanceService);
const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
@@ -46,12 +52,12 @@ export const ClearSelectionAllCommand: ICommand = {
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
if (!workbook) return false;
- const unitId = workbook.getUnitId();
+ const unitId = params?.unitId || workbook.getUnitId();
const worksheet = workbook.getActiveSheet();
if (!worksheet) return false;
- const subUnitId = worksheet.getSheetId();
- const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
+ const subUnitId = params?.subUnitId || worksheet.getSheetId();
+ const selections = params?.ranges || selectionManagerService.getCurrentSelections()?.map((s) => s.range);
if (!selections?.length) {
return false;
}
diff --git a/packages/sheets/src/commands/commands/clear-selection-content.command.ts b/packages/sheets/src/commands/commands/clear-selection-content.command.ts
index b7d7892f30..1f130968c3 100644
--- a/packages/sheets/src/commands/commands/clear-selection-content.command.ts
+++ b/packages/sheets/src/commands/commands/clear-selection-content.command.ts
@@ -14,9 +14,9 @@
* limitations under the License.
*/
-import type { IAccessor, ICommand, Workbook } from '@univerjs/core';
-import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
+import type { IAccessor, ICommand, IRange, Workbook } from '@univerjs/core';
+import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
import {
CommandType,
ICommandService,
@@ -30,6 +30,12 @@ import { SheetsSelectionsService } from '../../services/selections/selection.ser
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
+interface IClearSelectionContentCommandParams {
+ unitId?: string;
+ subUnitId?: string;
+ ranges?: IRange[];
+}
+
/**
* The command to clear content in current selected ranges.
*/
@@ -38,7 +44,7 @@ export const ClearSelectionContentCommand: ICommand = {
type: CommandType.COMMAND,
- handler: async (accessor: IAccessor) => {
+ handler: async (accessor: IAccessor, params: IClearSelectionContentCommandParams) => {
const univerInstanceService = accessor.get(IUniverInstanceService);
const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
@@ -48,20 +54,20 @@ export const ClearSelectionContentCommand: ICommand = {
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
if (!workbook) return false;
- const unitId = workbook.getUnitId();
+ const unitId = params?.unitId || workbook.getUnitId();
const worksheet = workbook.getActiveSheet();
if (!worksheet) return false;
- const subUnitId = worksheet.getSheetId();
- const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- if (!selections?.length) {
+ const subUnitId = params?.subUnitId || worksheet.getSheetId();
+ const ranges = params?.ranges || selectionManagerService.getCurrentSelections()?.map((s) => s.range);
+ if (!ranges?.length) {
return false;
}
const clearMutationParams: ISetRangeValuesMutationParams = {
subUnitId,
unitId,
- cellValue: generateNullCellValue(selections),
+ cellValue: generateNullCellValue(ranges),
};
const undoClearMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
accessor,
diff --git a/packages/sheets/src/commands/commands/clear-selection-format.command.ts b/packages/sheets/src/commands/commands/clear-selection-format.command.ts
index c1cc725e82..7c127ee917 100644
--- a/packages/sheets/src/commands/commands/clear-selection-format.command.ts
+++ b/packages/sheets/src/commands/commands/clear-selection-format.command.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IAccessor, ICommand, IMutationInfo, Workbook } from '@univerjs/core';
+import type { IAccessor, ICommand, IMutationInfo, IRange, Workbook } from '@univerjs/core';
import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';
import {
@@ -30,6 +30,12 @@ import { SheetsSelectionsService } from '../../services/selections/selection.ser
import { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';
import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';
+interface IClearSelectionFormatCommandParams {
+ unitId?: string;
+ subUnitId?: string;
+ ranges?: IRange[];
+}
+
/**
* The command to clear content in current selected ranges.
*/
@@ -38,7 +44,7 @@ export const ClearSelectionFormatCommand: ICommand = {
type: CommandType.COMMAND,
- handler: async (accessor: IAccessor) => {
+ handler: async (accessor: IAccessor, params: IClearSelectionFormatCommandParams) => {
const univerInstanceService = accessor.get(IUniverInstanceService);
const commandService = accessor.get(ICommandService);
const selectionManagerService = accessor.get(SheetsSelectionsService);
@@ -48,13 +54,13 @@ export const ClearSelectionFormatCommand: ICommand = {
const workbook = univerInstanceService.getCurrentUnitForType<Workbook>(UniverInstanceType.UNIVER_SHEET);
if (!workbook) return false;
- const unitId = workbook.getUnitId();
+ const unitId = params?.unitId || workbook.getUnitId();
const worksheet = workbook.getActiveSheet();
if (!worksheet) return false;
- const subUnitId = worksheet.getSheetId();
- const selections = selectionManagerService.getCurrentSelections()?.map((s) => s.range);
- if (!selections?.length) {
+ const subUnitId = params?.subUnitId || worksheet.getSheetId();
+ const ranges = params?.ranges || selectionManagerService.getCurrentSelections()?.map((s) => s.range);
+ if (!ranges?.length) {
return false;
}
@@ -65,7 +71,7 @@ export const ClearSelectionFormatCommand: ICommand = {
const clearMutationParams: ISetRangeValuesMutationParams = {
subUnitId,
unitId,
- cellValue: generateNullCellStyle(selections),
+ cellValue: generateNullCellStyle(ranges),
};
const undoClearMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(
accessor,
diff --git a/packages/sheets/src/commands/commands/set-gridlines-color.command.ts b/packages/sheets/src/commands/commands/set-gridlines-color.command.ts
new file mode 100644
index 0000000000..256933b263
--- /dev/null
+++ b/packages/sheets/src/commands/commands/set-gridlines-color.command.ts
@@ -0,0 +1,69 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * 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 } from '@univerjs/core';
+import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';
+import { type ISetGridlinesColorMutationParams, SetGridlinesColorMutation } from '../mutations/set-gridlines-color.mutation';
+import { getSheetCommandTarget } from './utils/target-util';
+
+export interface ISetGridlinesColorCommandParams {
+ color: string | undefined;
+ unitId?: string;
+ subUnitId?: string;
+}
+
+export const SetGridlinesColorCommand: ICommand = {
+ type: CommandType.COMMAND,
+ id: 'sheet.command.set-gridlines-color',
+ handler: async (accessor: IAccessor, params?: ISetGridlinesColorCommandParams) => {
+ const commandService = accessor.get(ICommandService);
+ const undoRedoService = accessor.get(IUndoRedoService);
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+
+ const target = getSheetCommandTarget(univerInstanceService);
+ if (!target) return false;
+
+ const { worksheet } = target;
+ const currentlyColor = worksheet.getConfig().gridlinesColor;
+ if (currentlyColor === params?.color) return false;
+
+ const { unitId, subUnitId } = target;
+ const doParams: ISetGridlinesColorMutationParams = {
+ color: params?.color,
+ unitId,
+ subUnitId,
+ };
+
+ const undoMutationParams: ISetGridlinesColorMutationParams = {
+ color: currentlyColor,
+ unitId,
+ subUnitId,
+ };
+
+ const result = commandService.syncExecuteCommand(SetGridlinesColorMutation.id, doParams);
+ if (result) {
+ undoRedoService.pushUndoRedo({
+ unitID: unitId,
+ undoMutations: [{ id: SetGridlinesColorMutation.id, params: undoMutationParams }],
+ redoMutations: [{ id: SetGridlinesColorMutation.id, params: doParams }],
+ });
+
+ return true;
+ }
+
+ return false;
+ },
+};
diff --git a/packages/sheets/src/commands/commands/set-tab-color.command.ts b/packages/sheets/src/commands/commands/set-tab-color.command.ts
index d65dd562be..9a01cce107 100644
--- a/packages/sheets/src/commands/commands/set-tab-color.command.ts
+++ b/packages/sheets/src/commands/commands/set-tab-color.command.ts
@@ -15,9 +15,9 @@
*/
import type { IAccessor, ICommand } from '@univerjs/core';
-import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';
-
import type { ISetTabColorMutationParams } from '../mutations/set-tab-color.mutation';
+
+import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';
import { SetTabColorMutation, SetTabColorUndoMutationFactory } from '../mutations/set-tab-color.mutation';
import { getSheetCommandTarget } from './utils/target-util';
diff --git a/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts b/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts
index e2e7745419..7109a7fe87 100644
--- a/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts
+++ b/packages/sheets/src/commands/commands/set-worksheet-hide.command.ts
@@ -15,6 +15,8 @@
*/
import type { IAccessor, ICommand } from '@univerjs/core';
+import type { ISetWorksheetHideMutationParams } from '../mutations/set-worksheet-hide.mutation';
+
import {
BooleanNumber,
CommandType,
@@ -24,8 +26,6 @@ import {
IUniverInstanceService,
LocaleService,
} from '@univerjs/core';
-
-import type { ISetWorksheetHideMutationParams } from '../mutations/set-worksheet-hide.mutation';
import { SetWorksheetHideMutation, SetWorksheetHideMutationFactory } from '../mutations/set-worksheet-hide.mutation';
import { getSheetCommandTarget } from './utils/target-util';
diff --git a/packages/sheets/src/commands/commands/set-worksheet-show.command.ts b/packages/sheets/src/commands/commands/set-worksheet-show.command.ts
index 8d9b6a52e6..67ee0c72c9 100644
--- a/packages/sheets/src/commands/commands/set-worksheet-show.command.ts
+++ b/packages/sheets/src/commands/commands/set-worksheet-show.command.ts
@@ -15,11 +15,11 @@
*/
import type { IAccessor, ICommand, Workbook } from '@univerjs/core';
-import { BooleanNumber, CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
-
import type { ISetWorksheetHideMutationParams } from '../mutations/set-worksheet-hide.mutation';
-import { SetWorksheetHideMutation, SetWorksheetHideMutationFactory } from '../mutations/set-worksheet-hide.mutation';
+
import type { ISetWorksheetActiveOperationParams } from '../operations/set-worksheet-active.operation';
+import { BooleanNumber, CommandType, ICommandService, IUndoRedoService, IUniverInstanceService, UniverInstanceType } from '@univerjs/core';
+import { SetWorksheetHideMutation, SetWorksheetHideMutationFactory } from '../mutations/set-worksheet-hide.mutation';
import {
SetWorksheetActiveOperation,
// SetWorksheetUnActivateMutationFactory,
@@ -29,7 +29,6 @@ import { getSheetCommandTarget } from './utils/target-util';
export interface ISetWorksheetShowCommandParams {
unitId: string;
subUnitId: string;
- value?: string;
}
export const SetWorksheetShowCommand: ICommand = {
diff --git a/packages/sheets/src/commands/mutations/set-gridlines-color.mutation.ts b/packages/sheets/src/commands/mutations/set-gridlines-color.mutation.ts
new file mode 100644
index 0000000000..1ec30193f7
--- /dev/null
+++ b/packages/sheets/src/commands/mutations/set-gridlines-color.mutation.ts
@@ -0,0 +1,54 @@
+/**
+ * Copyright 2023-present DreamNum Inc.
+ *
+ * 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 { getSheetCommandTarget } from '../commands/utils/target-util';
+
+export interface ISetGridlinesColorMutationParams {
+ color: string | undefined;
+ unitId: string;
+ subUnitId: string;
+}
+
+export const SetGridlinesColorUndoMutationFactory = (accessor: IAccessor, params: ISetGridlinesColorMutationParams) => {
+ const univerInstanceService = accessor.get(IUniverInstanceService);
+ const universheet = univerInstanceService.getUniverSheetInstance(params.unitId);
+
+ if (universheet == null) {
+ throw new Error('universheet is null error!');
+ }
+
+ return {
+ unitId: params.unitId,
+ subUnitId: params.subUnitId,
+ color: params.color,
+ };
+};
+
+export const SetGridlinesColorMutation: IMutation<ISetGridlinesColorMutationParams> = {
+ id: 'sheet.mutation.set-gridlines-color',
+ type: CommandType.MUTATION,
+ handler: (accessor, params) => {
+ const target = getSheetCommandTarget(accessor.get(IUniverInstanceService), params);
+ if (!target) return false;
+
+ const { worksheet } = target;
+ const config = worksheet.getConfig();
+ config.gridlinesColor = params.color;
+ return true;
+ },
+};
diff --git a/packages/sheets/src/controllers/basic-worksheet.controller.ts b/packages/sheets/src/controllers/basic-worksheet.controller.ts
index c0fb23a32e..90473cf6ce 100644
--- a/packages/sheets/src/controllers/basic-worksheet.controller.ts
+++ b/packages/sheets/src/controllers/basic-worksheet.controller.ts
@@ -62,6 +62,7 @@ import {
} from '../commands/commands/set-col-visible.command';
import { SetDefinedNameCommand } from '../commands/commands/set-defined-name.command';
import { CancelFrozenCommand, SetFrozenCommand } from '../commands/commands/set-frozen.command';
+import { SetGridlinesColorCommand } from '../commands/commands/set-gridlines-color.command';
import { SetProtectionCommand } from '../commands/commands/set-protection.command';
import { SetRangeValuesCommand } from '../commands/commands/set-range-values.command';
import { SetRowDataCommand } from '../commands/commands/set-row-data.command';
@@ -112,13 +113,14 @@ import { MoveColsMutation, MoveRowsMutation } from '../commands/mutations/move-r
import { RemoveNumfmtMutation, SetNumfmtMutation } from '../commands/mutations/numfmt-mutation';
import { RemoveColMutation, RemoveRowMutation } from '../commands/mutations/remove-row-col.mutation';
import { RemoveSheetMutation } from '../commands/mutations/remove-sheet.mutation';
-import { RemoveWorksheetMergeMutation } from '../commands/mutations/remove-worksheet-merge.mutation';
+import { RemoveWorksheetMergeMutation } from '../commands/mutations/remove-worksheet-merge.mutation';
import { ReorderRangeMutation } from '../commands/mutations/reorder-range.mutation';
import { SetColDataMutation } from '../commands/mutations/set-col-data.mutation';
-import { SetColHiddenMutation, SetColVisibleMutation } from '../commands/mutations/set-col-visible.mutation';
+import { SetColHiddenMutation, SetColVisibleMutation } from '../commands/mutations/set-col-visible.mutation';
import { SetFrozenMutation } from '../commands/mutations/set-frozen.mutation';
+import { SetGridlinesColorMutation } from '../commands/mutations/set-gridlines-color.mutation';
import { SetRangeProtectionMutation } from '../commands/mutations/set-range-protection.mutation';
import { SetRangeValuesMutation } from '../commands/mutations/set-range-values.mutation';
import { SetRowDataMutation } from '../commands/mutations/set-row-data.mutation';
@@ -276,6 +278,8 @@ export class BasicWorksheetController extends Disposable implements IDisposable
ToggleGridlinesCommand,
ToggleGridlinesMutation,
+ SetGridlinesColorCommand,
+ SetGridlinesColorMutation,
// permissions range protection
SetWorksheetPermissionPointsCommand,
diff --git a/packages/sheets/src/facade/f-selection.ts b/packages/sheets/src/facade/f-selection.ts
index b533eef70c..4b081ddf15 100644
--- a/packages/sheets/src/facade/f-selection.ts
+++ b/packages/sheets/src/facade/f-selection.ts
@@ -14,12 +14,23 @@
* limitations under the License.
*/
-import type { Workbook, Worksheet } from '@univerjs/core';
+import type { ISelectionCell, Nullable, Workbook, Worksheet } from '@univerjs/core';
import type { ISelectionWithStyle } from '@univerjs/sheets';
import { Inject, Injector } from '@univerjs/core';
import { FRange } from './f-range';
+/**
+ * @description Represents the active selection in the sheet.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook()
+ * const fWorksheet = fWorkbook.getActiveSheet()
+ * const fSelection = fWorksheet.getSelection();
+ * const activeRange = fSelection.getActiveRange();
+ * console.log(activeRange);
+ * ```
+ */
export class FSelection {
constructor(
private readonly _workbook: Workbook,
@@ -30,6 +41,10 @@ export class FSelection {
// empty
}
+ /**
+ * Represents the active selection in the sheet. Which means the selection contains the active cell.
+ * @returns {FRange | null} The active selection.
+ */
getActiveRange(): FRange | null {
const active = this._selections.find((selection) => !!selection.primary);
if (!active) {
@@ -38,5 +53,28 @@ export class FSelection {
return this._injector.createInstance(FRange, this._workbook, this._worksheet, active.range);
}
+
+ /**
+ * Represents the active selection list in the sheet.
+ * @returns {FRange[]} The active selection list.
+ */
+ getActiveRangeList(): FRange[] {
+ return this._selections.map((selection) => {
+ return this._injector.createInstance(FRange, this._workbook, this._worksheet, selection.range);
+ });
+ }
+
+ /**
+ * Represents the current select cell in the sheet.
+ * @returns {ISelectionCell} The current select cell info.Pay attention to the type of the return value.
+ */
+ getCurrentCell(): Nullable<ISelectionCell> {
+ const current = this._selections.find((selection) => !!selection.primary);
+ if (!current) {
+ return null;
+ }
+
+ return current.primary;
+ }
}
diff --git a/packages/sheets/src/facade/f-worksheet.ts b/packages/sheets/src/facade/f-worksheet.ts
index 1c5592cdc2..9fcc63dd70 100644
--- a/packages/sheets/src/facade/f-worksheet.ts
+++ b/packages/sheets/src/facade/f-worksheet.ts
@@ -15,15 +15,23 @@
*/
import type { CustomData, ICellData, IColumnData, IDisposable, IFreeze, IObjectArrayPrimitiveType, IRange, IRowData, IStyleData, Nullable, Workbook, Worksheet } from '@univerjs/core';
-import type { ISetColDataCommandParams, ISetRangeValuesMutationParams, ISetRowDataCommandParams, IToggleGridlinesCommandParams } from '@univerjs/sheets';
+import type { ISetColDataCommandParams, ISetGridlinesColorCommandParams, ISetRangeValuesMutationParams, ISetRowDataCommandParams, IToggleGridlinesCommandParams } from '@univerjs/sheets';
import type { FWorkbook } from './f-workbook';
import { BooleanNumber, Direction, FBase, ICommandService, Inject, Injector, ObjectMatrix, RANGE_TYPE } from '@univerjs/core';
import { deserializeRangeWithSheet } from '@univerjs/engine-formula';
-import { CancelFrozenCommand, copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColDataCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetWorksheetDefaultStyleMutation, SetWorksheetRowIsAutoHeightCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets';
+import { CancelFrozenCommand, ClearSelectionAllCommand, ClearSelectionContentCommand, ClearSelectionFormatCommand, copyRangeStyles, InsertColCommand, InsertRowCommand, MoveColsCommand, MoveRowsCommand, RemoveColCommand, RemoveRowCommand, SetColDataCommand, SetColHiddenCommand, SetColWidthCommand, SetFrozenCommand, SetGridlinesColorCommand, SetRangeValuesMutation, SetRowDataCommand, SetRowHeightCommand, SetRowHiddenCommand, SetSpecificColsVisibleCommand, SetSpecificRowsVisibleCommand, SetTabColorCommand, SetWorksheetDefaultStyleMutation, SetWorksheetHideCommand, SetWorksheetNameCommand, SetWorksheetRowIsAutoHeightCommand, SetWorksheetShowCommand, SheetsSelectionsService, ToggleGridlinesCommand } from '@univerjs/sheets';
import { FRange } from './f-range';
import { FSelection } from './f-selection';
import { covertToColRange, covertToRowRange } from './utils';
+interface IFacadeClearOptions {
+ contentsOnly?: boolean;
+ formatOnly?: boolean;
+}
+
+/**
+ * Represents a worksheet facade api instance. Which provides a set of methods to interact with the worksheet.
+ */
export class FWorksheet extends FBase {
constructor(
protected readonly _fWorkbook: FWorkbook,
@@ -46,28 +54,32 @@ export class FWorksheet extends FBase {
/**
* Returns the workbook
- * @returns The workbook
+ * @returns {Workbook} The workbook instance.
*/
getWorkbook(): Workbook {
return this._workbook;
}
/**
- * Returns the worksheet id
- * @returns The id of the worksheet
+ * Returns the worksheet id.
+ * @returns {string} The id of the worksheet.
*/
getSheetId(): string {
return this._worksheet.getSheetId();
}
/**
- * Returns the worksheet name
- * @returns The name of the worksheet
+ * Returns the worksheet name.
+ * @returns {string} The name of the worksheet.
*/
getSheetName(): string {
return this._worksheet.getName();
}
+ /**
+ * Represents the selection ranges info of the worksheet.
+ * @returns {FSelection} return the current selections of the worksheet or null if there is no selection.
+ */
getSelection(): FSelection | null {
const selections = this._selectionManagerService.getCurrentSelections();
if (!selections) {
@@ -83,7 +95,7 @@ export class FWorksheet extends FBase {
/**
* Get the default style of the worksheet
- * @returns Default style
+ * @returns {IStyleData} Default style of the worksheet.
*/
getDefaultStyle(): Nullable<IStyleData> | string {
return this._worksheet.getDefaultCellStyle();
@@ -111,8 +123,8 @@ export class FWorksheet extends FBase {
/**
* Set the default style of the worksheet
- * @param style default style
- * @returns this worksheet
+ * @param {StyleDataInfo} style default style
+ * @returns {Promise<FWorksheet>} This sheet, for chaining.
*/
async setDefaultStyle(style: string): Promise<FWorksheet> {
const unitId = this._workbook.getUnitId();
@@ -130,6 +142,7 @@ export class FWorksheet extends FBase {
* Set the default style of the worksheet row
* @param {number} index The row index
* @param {string | Nullable<IStyleData>} style The style name or style data
+ * @returns {Promise<FWorksheet>} This sheet, for chaining.
*/
async setColumnDefaultStyle(index: number, style: string | Nullable<IStyleData>): Promise<FWorksheet> {
const unitId = this._workbook.getUnitId();
@@ -153,6 +166,7 @@ export class FWorksheet extends FBase {
* Set the default style of the worksheet column
* @param {number} index The column index
* @param {string | Nullable<IStyleData>} style The style name or style data
+ * @returns {Promise<FWorksheet>} This sheet, for chaining.
*/
async setRowDefaultStyle(index: number, style: string | Nullable<IStyleData>): Promise<FWorksheet> {
const unitId = this._workbook.getUnitId();
@@ -1099,6 +1113,13 @@ export class FWorksheet extends FBase {
/**
* Hides or reveals the sheet gridlines.
* @param {boolean} hidden If `true`, hide gridlines in this sheet; otherwise show the gridlines.
+ * @example
+ * ``` ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * // hide the gridlines
+ * fWorkSheet.setHiddenGridlines(true);
+ * ```
*/
setHiddenGridlines(hidden: boolean): Promise<boolean> {
return this._commandService.executeCommand(ToggleGridlinesCommand.id, {
@@ -1108,6 +1129,62 @@ export class FWorksheet extends FBase {
} as IToggleGridlinesCommandParams);
}
+ /**
+ * Set the color of the gridlines in the sheet.
+ * @param {string|undefined} color The color to set for the gridlines.Undefined or null to reset to the default color.
+ * @returns {Promise<boolean>} True if the command was successful, false otherwise.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * // set the gridlines color to red
+ * fWorkSheet.setGridLinesColor('#ff0000');
+ * ```
+ */
+ setGridLinesColor(color: string | undefined): Promise<boolean> {
+ return this._commandService.executeCommand(SetGridlinesColorCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ color,
+ } as ISetGridlinesColorCommandParams);
+ }
+
+ /**
+ * Get the color of the gridlines in the sheet.
+ * @returns {string | undefined} The color of the gridlines in the sheet or undefined. The default color is 'rgb(214, 216, 219)'.
+ */
+ getGridLinesColor(): string | undefined {
+ return this._worksheet.getGridlinesColor() as string | undefined;
+ }
+
+ /**
+ * Sets the sheet tab color.
+ * @param {string|null|undefined} color A color code in CSS notation (like '#ffffff' or 'white'), or null to reset the tab color.
+ * @returns {Promise<boolean>} True if the command was successful, false otherwise.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * // set the tab color to red
+ * fWorkSheet.setTabColor('#ff0000');
+ * ```
+ */
+ setTabColor(color: string): Promise<boolean> {
+ return this._commandService.executeCommand(SetTabColorCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ color,
+ });
+ }
+
+ /**
+ * Get the tab color of the sheet.
+ * @returns {string} The tab color of the sheet or undefined.
+ */
+ getTabColor(): string | undefined {
+ return this._worksheet.getTabColor() as string | undefined;
+ }
+
/**
* Subscribe to the cell data change event.
* @param callback - The callback function to be executed when the cell data changes.
@@ -1145,4 +1222,231 @@ export class FWorksheet extends FBase {
}
});
}
+
+ /**
+ * Hides this sheet. Has no effect if the sheet is already hidden. If this method is called on the only visible sheet, it throws an exception.
+ */
+ hideSheet(): void {
+ const commandService = this._injector.get(ICommandService);
+ const workbook = this._workbook;
+ const sheets = workbook.getSheets();
+ const visibleSheets = sheets.filter((sheet) => sheet.isSheetHidden() === BooleanNumber.TRUE);
+ if (visibleSheets.length <= 1) {
+ throw new Error('Cannot hide the only visible sheet');
+ }
+
+ commandService.executeCommand(SetWorksheetHideCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ });
+ }
+
+ /**
+ * Shows this sheet. Has no effect if the sheet is already visible.
+ */
+ showSheet(): Promise<boolean> {
+ const commandService = this._injector.get(ICommandService);
+ return commandService.executeCommand(SetWorksheetShowCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ });
+ }
+
+ /**
+ * Returns true if the sheet is currently hidden.
+ * @returns {boolean} True if the sheet is hidden; otherwise, false.
+ */
+ isSheetHidden(): boolean {
+ return Boolean(this._worksheet.isSheetHidden() === BooleanNumber.TRUE);
+ }
+
+ /**
+ * Sets the sheet name.
+ * @param {string} name The new name for the sheet.
+ * @returns {Promise<boolean>} True if the command was successful, false otherwise.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * // set the sheet name to 'Sheet1'
+ * fWorkSheet.setName('Sheet1');
+ * ```
+ */
+ setName(name: string): Promise<boolean> {
+ return this._commandService.executeCommand(SetWorksheetNameCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ name,
+ });
+ }
+
+ /**
+ * Activates this sheet. Does not alter the sheet itself, only the parent's notion of the active sheet.
+ * @returns Current sheet, for chaining.
+ */
+ activate(): FWorksheet {
+ this._fWorkbook.setActiveSheet(this);
+ return this;
+ }
+
+ /**
+ * Gets the position of the sheet in its parent spreadsheet. Starts at 0.
+ * @returns {number} The position of the sheet in its parent spreadsheet.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * // get the position of the active sheet
+ * const position = fWorkSheet.getIndex();
+ * console.log(position); // 0
+ * ```
+ */
+ getIndex(): number {
+ return this._workbook.getSheetIndex(this._worksheet);
+ }
+
+ /**
+ * Clears the sheet of content and formatting information.Or Optionally clears only the contents or only the formatting.
+ * @param {IFacadeClearOptions} [options] Options for clearing the sheet. If not provided, the contents and formatting are cleared both.
+ * @param {boolean} [options.contentsOnly] If true, the contents of the sheet are cleared. If false, the contents and formatting are cleared. Default is false.
+ * @param {boolean} [options.formatOnly] If true, the formatting of the sheet is cleared. If false, the contents and formatting are cleared. Default is false.
+ * @returns {Promise<boolean>} True if the command was successful, false otherwise.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * // clear the sheet of content and formatting information
+ * fWorkSheet.clear();
+ * // clear the sheet of content only
+ * fWorkSheet.clear({ contentsOnly: true });
+ * ```
+ */
+ clear(options?: IFacadeClearOptions): Promise<boolean> {
+ if (options && options.contentsOnly && !options.formatOnly) {
+ return this.clearContents();
+ }
+
+ if (options && options.formatOnly && !options.contentsOnly) {
+ return this.clearFormats();
+ }
+
+ const unitId = this._workbook.getUnitId();
+ const subUnitId = this._worksheet.getSheetId();
+ const commandService = this._injector.get(ICommandService);
+
+ const range = {
+ startRow: 0,
+ endRow: this._worksheet.getRowCount() - 1,
+ startColumn: 0,
+ endColumn: this._worksheet.getColumnCount() - 1,
+ };
+
+ return commandService.executeCommand(ClearSelectionAllCommand.id, {
+ unitId,
+ subUnitId,
+ ranges: [range],
+ options,
+ });
+ }
+
+ /**
+ * Clears the sheet of contents, while preserving formatting information.
+ * @returns {Promise<boolean>} True if the command was successful, false otherwise.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * // clear the sheet of content only
+ * fWorkSheet.clearContents();
+ * ```
+ */
+ clearContents(): Promise<boolean> {
+ const unitId = this._workbook.getUnitId();
+ const subUnitId = this._worksheet.getSheetId();
+ const commandService = this._injector.get(ICommandService);
+
+ const range = {
+ startRow: 0,
+ endRow: this._worksheet.getRowCount() - 1,
+ startColumn: 0,
+ endColumn: this._worksheet.getColumnCount() - 1,
+ };
+
+ return commandService.executeCommand(ClearSelectionContentCommand.id, {
+ unitId,
+ subUnitId,
+ ranges: [range],
+ });
+ }
+
+ /**
+ * Clears the sheet of formatting, while preserving contents.
+ * @returns {Promise<boolean>} True if the command was successful, false otherwise.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * // clear the sheet of formatting only
+ * fWorkSheet.clearFormats();
+ * ```
+ */
+ clearFormats(): Promise<boolean> {
+ const unitId = this._workbook.getUnitId();
+ const subUnitId = this._worksheet.getSheetId();
+ const commandService = this._injector.get(ICommandService);
+
+ const range = {
+ startRow: 0,
+ endRow: this._worksheet.getRowCount() - 1,
+ startColumn: 0,
+ endColumn: this._worksheet.getColumnCount() - 1,
+ };
+
+ return commandService.executeCommand(ClearSelectionFormatCommand.id, {
+ unitId,
+ subUnitId,
+ ranges: [range],
+ });
+ }
+
+ /**
+ * Returns a Range corresponding to the dimensions in which data is present.
+ * This is functionally equivalent to creating a Range bounded by A1 and (Sheet.getLastColumns(), Sheet.getLastRows()).
+ * @returns {FRange} The range of the data in the sheet.
+ */
+ getDataRange() {
+ const lastRow = this.getLastRows();
+ const lastColumn = this.getLastColumns();
+ return this.getRange(0, 0, lastRow + 1, lastColumn + 1);
+ }
+
+ /**
+ * Returns the position of the last column that has content.
+ * @returns {number} the last column of the sheet that contains content.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * const fRange = fWorkSheet.getRange(100, 20, 1, 1);
+ * console.log(fWorkSheet.getLastColumns()); // 20
+ * ```
+ */
+ getLastColumns(): number {
+ return this._worksheet.getLastColumnWithContent();
+ }
+
+ /**
+ * Returns the position of the last row that has content.
+ * @returns {number} the last row of the sheet that contains content.
+ * @example
+ * ```ts
+ * const fWorkbook = univerAPI.getActiveWorkbook();
+ * const fWorkSheet = fWorkbook.getActiveSheet();
+ * const fRange = fWorkSheet.getRange(100,1,1,1);
+ * fRange.setValue('Hello World');
+ * console.log(fWorkSheet.getLastRows()); // 100
+ */
+ getLastRows(): number {
+ return this._worksheet.getLastRowWithContent();
+ }
}
diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.ts
index b0e702a9d6..ec3435d23e 100644
--- a/packages/sheets/src/index.ts
+++ b/packages/sheets/src/index.ts
@@ -65,8 +65,8 @@ export type { EffectRefRangeParams, IOperator } from './services/ref-range/type'
export { EffectRefRangId, OperatorType } from './services/ref-range/type';
export { DefinedNameDataController } from './controllers/defined-name-data.controller';
export {
- getSeparateEffectedRangesOnCommand,
adjustRangeOnMutation,
+ getSeparateEffectedRangesOnCommand,
handleBaseInsertRange,
handleBaseMoveRowsCols,
handleBaseRemoveRange,
@@ -256,6 +256,7 @@ export { SetDefinedNameCommand } from './commands/commands/set-defined-name.comm
export { type ICancelFrozenCommandParams, type ISetFrozenCommandParams } from './commands/commands/set-frozen.command';
export { CancelFrozenCommand, SetFrozenCommand } from './commands/commands/set-frozen.command';
export { type IToggleGridlinesCommandParams, ToggleGridlinesCommand } from './commands/commands/toggle-gridlines.command';
+export { type ISetGridlinesColorCommandParams, SetGridlinesColorCommand } from './commands/commands/set-gridlines-color.command';
export { type ISetRangeValuesCommandParams, SetRangeValuesCommand } from './commands/commands/set-range-values.command';
export {
type ISetSpecificRowsVisibleCommandParams,
@@ -368,6 +369,7 @@ export {
SetFrozenMutationFactory,
} from './commands/mutations/set-frozen.mutation';
export { type IToggleGridlinesMutationParams, ToggleGridlinesMutation } from './commands/mutations/toggle-gridlines.mutation';
+export { type ISetGridlinesColorMutationParams, SetGridlinesColorMutation } from './commands/mutations/set-gridlines-color.mutation';
export { FactorySetRangeProtectionMutation, type ISetRangeProtectionMutationParams, SetRangeProtectionMutation } from './commands/mutations/set-range-protection.mutation';
export {
type ISetRangeValuesMutationParams,