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.

chore(core): move cellToRange to core (#1684)

Dushusir6f4c5257551f
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index cecc781bbe..f68de6d8f1 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -149,5 +149,6 @@ export { ClientSnapshotServerService } from './services/snapshot/snapshot-server
export { getSheetBlocksFromSnapshot } from './services/snapshot/snapshot-transform';
export { isBlackColor, isWhiteColor } from './shared/color/color-kit';
+export { cellToRange } from './shared/common';
installShims();
diff --git a/packages/core/src/shared/__test__/common.spec.ts b/packages/core/src/shared/__test__/common.spec.ts
new file mode 100644
index 0000000000..d33930e9ed
--- /dev/null
+++ b/packages/core/src/shared/__test__/common.spec.ts
@@ -0,0 +1,24 @@
+/**
+ * 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 { describe, expect, it } from 'vitest';
+import { cellToRange } from '../common';
+
+describe('Test common', () => {
+ it('Test cellToRange', () => {
+ expect(cellToRange(0, 1)).toStrictEqual({ startRow: 0, startColumn: 1, endRow: 0, endColumn: 1 });
+ });
+});
diff --git a/packages/core/src/shared/common.ts b/packages/core/src/shared/common.ts
index d048ca910c..67745cd6ae 100644
--- a/packages/core/src/shared/common.ts
+++ b/packages/core/src/shared/common.ts
@@ -528,3 +528,13 @@ export function isValidRange(range: IRange): boolean {
return true;
}
+
+/**
+ * Covert row/column to range object
+ * @param row
+ * @param col
+ * @returns
+ */
+export function cellToRange(row: number, col: number): IRange {
+ return { startRow: row, endRow: row, startColumn: col, endColumn: col };
+}
diff --git a/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts b/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts
index 37932919ea..d5d82ed91d 100644
--- a/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts
+++ b/packages/sheets-formula/src/controllers/__tests__/update-formula.controller.spec.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
-import type { ICellData, IRange, IWorkbookData, Nullable, Univer } from '@univerjs/core';
-import { Direction, ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE } from '@univerjs/core';
+import type { ICellData, IWorkbookData, Nullable, Univer } from '@univerjs/core';
+import { cellToRange, Direction, ICommandService, IUniverInstanceService, LocaleType, RANGE_TYPE } from '@univerjs/core';
import type { IInsertColCommandParams } from '@univerjs/sheets';
import { InsertColCommand, MoveRowsCommand, NORMAL_SELECTION_PLUGIN_NAME, SelectionManagerService } from '@univerjs/sheets';
import type { Injector } from '@wendellhu/redi';
@@ -46,8 +46,6 @@ const TEST_WORKBOOK_DATA_DEMO = (): IWorkbookData => ({
styles: {},
});
-const cellToRange = (row: number, col: number) =>
- ({ startRow: row, endRow: row, startColumn: col, endColumn: col }) as IRange;
// TODO@Dushusir: add move range,insert range,delete range test case
describe('Test insert function operation', () => {
let univer: Univer;
diff --git a/packages/sheets-formula/src/controllers/utils/__tests__/offset-formula-data.spec.ts b/packages/sheets-formula/src/controllers/utils/__tests__/offset-formula-data.spec.ts
index 52b80fcd09..8edec1d3c5 100644
--- a/packages/sheets-formula/src/controllers/utils/__tests__/offset-formula-data.spec.ts
+++ b/packages/sheets-formula/src/controllers/utils/__tests__/offset-formula-data.spec.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IRange } from '@univerjs/core';
+import { cellToRange } from '@univerjs/core';
import type {
IDeleteRangeMoveLeftCommandParams,
IDeleteRangeMoveUpCommandParams,
@@ -40,8 +40,6 @@ import { describe, expect, it } from 'vitest';
import { offsetArrayFormula, offsetFormula, removeFormulaData } from '../offset-formula-data';
-const cellToRange = (row: number, col: number) =>
- ({ startRow: row, endRow: row, startColumn: col, endColumn: col }) as IRange;
describe('Utils offset formula data test', () => {
describe('function offsetFormula', () => {
it('move range normal formula', () => {
diff --git a/packages/sheets-numfmt/src/__tests__/utils.test.ts b/packages/sheets-numfmt/src/__tests__/utils.test.ts
index 663308853f..9805fa2283 100644
--- a/packages/sheets-numfmt/src/__tests__/utils.test.ts
+++ b/packages/sheets-numfmt/src/__tests__/utils.test.ts
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-import type { IRange } from '@univerjs/core';
+import { cellToRange } from '@univerjs/core';
import { RemoveNumfmtMutation, SetNumfmtMutation } from '@univerjs/sheets';
import { describe, expect, it } from 'vitest';
@@ -28,8 +28,6 @@ import {
} from '../utils/decimal';
import { mergeNumfmtMutations } from '../utils/mutation';
-const cellToRange = (row: number, col: number) =>
- ({ startRow: row, endRow: row, startColumn: col, endColumn: col }) as IRange;
describe('test numfmt utils function', () => {
it('getCurrencyType', () => {
expect(getCurrencyType(`_(${currencySymbols[3]} 123123`)).toBe(currencySymbols[3]);
diff --git a/packages/sheets-ui/src/controllers/clipboard/utils.ts b/packages/sheets-ui/src/controllers/clipboard/utils.ts
index 16de4aad51..6a7fa2b9f6 100644
--- a/packages/sheets-ui/src/controllers/clipboard/utils.ts
+++ b/packages/sheets-ui/src/controllers/clipboard/utils.ts
@@ -15,7 +15,7 @@
*/
import type { ICellData, IDocumentBody, IMutationInfo, IParagraph, IRange, Nullable } from '@univerjs/core';
-import { IUniverInstanceService, ObjectMatrix, Range, Rectangle, Tools } from '@univerjs/core';
+import { cellToRange, IUniverInstanceService, ObjectMatrix, Range, Rectangle, Tools } from '@univerjs/core';
import type {
IAddWorksheetMergeMutationParams,
IMoveRangeMutationParams,
@@ -115,12 +115,6 @@ export function getMoveRangeMutations(
if (fromWorksheet && toWorksheet) {
const fromCellValue = new ObjectMatrix<Nullable<ICellData>>();
const newFromCellValue = new ObjectMatrix<Nullable<ICellData>>();
- const cellToRange = (row: number, col: number) => ({
- startRow: row,
- endRow: row,
- startColumn: col,
- endColumn: col,
- });
const fromCellMatrix = fromWorksheet.getCellMatrix();
const toCellMatrix = toWorksheet.getCellMatrix();
diff --git a/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts b/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
index 264664e28b..4d6c2d71a1 100644
--- a/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
+++ b/packages/sheets/src/basics/__tests__/rangeMerge.spec.ts
@@ -14,13 +14,11 @@
* limitations under the License.
*/
-import type { IRange } from '@univerjs/core';
+import { cellToRange, type IRange } from '@univerjs/core';
import { describe, expect, it } from 'vitest';
import { rangeMerge, RangeMergeUtil } from '../rangeMerge';
-const cellToRange = (row: number, col: number) =>
- ({ startColumn: col, endColumn: col, endRow: row, startRow: row }) as IRange;
describe('test rangeMerge', () => {
it('simple row', () => {
const testRange: IRange[] = [cellToRange(2, 2), cellToRange(3, 2), cellToRange(4, 2)];
diff --git a/packages/sheets/src/commands/commands/__tests__/move-rows-cols.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/move-rows-cols.command.spec.ts
index b0fbd8e876..6bb48545c1 100644
--- a/packages/sheets/src/commands/commands/__tests__/move-rows-cols.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/move-rows-cols.command.spec.ts
@@ -16,6 +16,7 @@
import type { ICellData, IRange, IWorkbookData, Nullable, Univer } from '@univerjs/core';
import {
+ cellToRange,
ICommandService,
IUniverInstanceService,
LocaleType,
@@ -38,9 +39,6 @@ import type { IMoveColsCommandParams, IMoveRowsCommandParams } from '../move-row
import { MoveColsCommand, MoveRowsCommand } from '../move-rows-cols.command';
import { createCommandTestBed } from './create-command-test-bed';
-const cellToRange = (row: number, col: number) =>
- ({ startRow: row, endRow: row, startColumn: col, endColumn: col }) as IRange;
-
describe('Test move rows cols', () => {
let univer: Univer;
let get: Injector['get'];
diff --git a/packages/sheets/src/commands/commands/move-range.command.ts b/packages/sheets/src/commands/commands/move-range.command.ts
index bd5a672c61..53c9fba098 100644
--- a/packages/sheets/src/commands/commands/move-range.command.ts
+++ b/packages/sheets/src/commands/commands/move-range.command.ts
@@ -16,6 +16,7 @@
import type { ICellData, ICommand, IMutationInfo, IRange, Nullable } from '@univerjs/core';
import {
+ cellToRange,
CommandType,
ErrorService,
ICommandService,
@@ -141,12 +142,6 @@ export function getMoveRangeUndoRedoMutations(
const fromCellValue = new ObjectMatrix<Nullable<ICellData>>();
const newFromCellValue = new ObjectMatrix<Nullable<ICellData>>();
- const cellToRange = (row: number, col: number) => ({
- startRow: row,
- endRow: row,
- startColumn: col,
- endColumn: col,
- });
Range.foreach(fromRange, (row, col) => {
fromCellValue.setValue(row, col, fromCellMatrix.getValue(row, col));
diff --git a/packages/sheets/src/commands/mutations/numfmt-mutation.ts b/packages/sheets/src/commands/mutations/numfmt-mutation.ts
index 9075dabcb6..b711779cef 100644
--- a/packages/sheets/src/commands/mutations/numfmt-mutation.ts
+++ b/packages/sheets/src/commands/mutations/numfmt-mutation.ts
@@ -15,7 +15,7 @@
*/
import type { ICommand, IMutationInfo, IRange } from '@univerjs/core';
-import { CommandType, Range } from '@univerjs/core';
+import { cellToRange, CommandType, Range } from '@univerjs/core';
import type { IAccessor } from '@wendellhu/redi';
import { rangeMerge } from '../../basics/rangeMerge';
@@ -191,11 +191,3 @@ export const transformCellsToRange = (
});
return { unitId, subUnitId, refMap, values };
};
-
-const cellToRange = (row: number, col: number) =>
- ({
- startRow: row,
- endRow: row,
- startColumn: col,
- endColumn: col,
- }) as IRange;
diff --git a/packages/sheets/src/services/__tests__/numfmt.service.test.ts b/packages/sheets/src/services/__tests__/numfmt.service.test.ts
index a9fc0ca316..bceb1a2ee0 100644
--- a/packages/sheets/src/services/__tests__/numfmt.service.test.ts
+++ b/packages/sheets/src/services/__tests__/numfmt.service.test.ts
@@ -15,7 +15,7 @@
*/
import type { Univer } from '@univerjs/core';
-import { ICommandService, IUniverInstanceService } from '@univerjs/core';
+import { cellToRange, ICommandService, IUniverInstanceService } from '@univerjs/core';
import type { IRemoveNumfmtMutationParams, ISetNumfmtMutationParams } from '@univerjs/sheets';
import type { Injector } from '@wendellhu/redi';
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
@@ -25,7 +25,6 @@ import { NumfmtService } from '../numfmt/numfmt.service';
import { INumfmtService } from '../numfmt/type';
import { createTestBase } from './util';
-const cellToRange = (row: number, col: number) => ({ startRow: row, endRow: row, startColumn: col, endColumn: col });
describe('test numfmt service', () => {
let univer: Univer;
let get: Injector['get'];