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: move function covertCellValue and covertCellValues to core package (#5233)

wpxp123456654119b0f3f7
diff --git a/examples/src/sheets/custom-plugin/import-csv-button.ts b/examples/src/sheets/custom-plugin/import-csv-button.ts
index 80c5c8f068..d29d58c394 100644
--- a/examples/src/sheets/custom-plugin/import-csv-button.ts
+++ b/examples/src/sheets/custom-plugin/import-csv-button.ts
@@ -18,6 +18,7 @@ import type { ICommand, IMutationInfo, Workbook } from '@univerjs/core';
import type { ISetRangeValuesMutationParams, ISetWorksheetColumnCountMutationParams, ISetWorksheetRowCountMutationParams } from '@univerjs/sheets';
import {
CommandType,
+ covertCellValues,
ICommandService,
Inject,
Injector,
@@ -36,7 +37,6 @@ import {
SetWorksheetRowCountMutation,
SetWorksheetRowCountUndoMutationFactory,
} from '@univerjs/sheets';
-import { covertCellValues } from '@univerjs/sheets/facade';
import {
ComponentManager,
IMenuManagerService,
diff --git a/packages/core/src/shared/common.ts b/packages/core/src/shared/common.ts
index f6be67943d..5c0b6cb3c7 100644
--- a/packages/core/src/shared/common.ts
+++ b/packages/core/src/shared/common.ts
@@ -14,12 +14,13 @@
* limitations under the License.
*/
-import type { ICellData, ICellDataForSheetInterceptor, ICellWithCoord, IRange, IRangeWithCoord, ISelectionCell } from '../sheets/typedef';
+import type { CellValue, ICellData, ICellDataForSheetInterceptor, ICellWithCoord, IRange, IRangeWithCoord, ISelectionCell } from '../sheets/typedef';
import type { Worksheet } from '../sheets/worksheet';
import type { IDocumentData } from '../types/interfaces/i-document-data';
import type { IColorStyle, IStyleData } from '../types/interfaces/i-style-data';
+import type { IObjectMatrixPrimitiveType } from './object-matrix';
import type { Nullable } from './types';
-import { RANGE_TYPE } from '../sheets/typedef';
+import { isCellV, isICellData, RANGE_TYPE } from '../sheets/typedef';
import {
BaselineOffset,
BorderStyleTypes,
@@ -29,6 +30,7 @@ import {
WrapStrategy,
} from '../types/enum';
import { ColorBuilder } from './color/color';
+import { ObjectMatrix } from './object-matrix';
import { Tools } from './tools';
/**
@@ -548,3 +550,64 @@ export function isValidRange(range: IRange, worksheet?: Worksheet): boolean {
export function cellToRange(row: number, col: number): IRange {
return { startRow: row, endRow: row, startColumn: col, endColumn: col };
}
+
+/**
+ * Covert cell value to cell data.
+ * @param {CellValue | ICellData} value - The cell value.
+ * @returns {ICellData} The cell data.
+ */
+export function covertCellValue(value: CellValue | ICellData): ICellData {
+ if (isFormulaString(value)) {
+ return {
+ f: value as string,
+ v: null,
+ p: null,
+ };
+ }
+ if (isCellV(value)) {
+ return {
+ v: value as Nullable<CellValue>,
+ p: null,
+ f: null,
+ };
+ }
+ if (isICellData(value)) {
+ return value;
+ }
+
+ // maybe {}
+ return value as ICellData;
+}
+
+/**
+ * Covert cell value array or matrix to cell data.
+ * @param {CellValue[][] | IObjectMatrixPrimitiveType<CellValue> | ICellData[][] | IObjectMatrixPrimitiveType<ICellData>} value - The cell value array or matrix.
+ * @param {IRange} range - The range.
+ * @returns {IObjectMatrixPrimitiveType<ICellData>} The cell data matrix.
+ */
+export function covertCellValues(
+ value:
+ | CellValue[][]
+ | IObjectMatrixPrimitiveType<CellValue>
+ | ICellData[][]
+ | IObjectMatrixPrimitiveType<ICellData>,
+ range: IRange
+): IObjectMatrixPrimitiveType<ICellData> {
+ const cellValue = new ObjectMatrix<ICellData>();
+ const { startRow, startColumn, endRow, endColumn } = range;
+
+ if (Tools.isArray(value)) {
+ for (let r = 0; r <= endRow - startRow; r++) {
+ for (let c = 0; c <= endColumn - startColumn; c++) {
+ cellValue.setValue(r + startRow, c + startColumn, covertCellValue(value[r][c]));
+ }
+ }
+ } else {
+ const valueMatrix = new ObjectMatrix(value as IObjectMatrixPrimitiveType<ICellData | CellValue>);
+ valueMatrix.forValue((r, c, v) => {
+ cellValue.setValue(r, c, covertCellValue(v));
+ });
+ }
+
+ return cellValue.getMatrix();
+}
diff --git a/packages/sheets/src/facade/__tests__/utils.spec.ts b/packages/sheets/src/facade/__tests__/utils.spec.ts
index f10d8de1a4..53dc23343a 100644
--- a/packages/sheets/src/facade/__tests__/utils.spec.ts
+++ b/packages/sheets/src/facade/__tests__/utils.spec.ts
@@ -14,8 +14,8 @@
* limitations under the License.
*/
+import { covertCellValue, covertCellValues } from '@univerjs/core';
import { describe, expect, it } from 'vitest';
-import { covertCellValue, covertCellValues } from '../utils';
describe('Test utils', () => {
it('function covertCellValue', () => {
diff --git a/packages/sheets/src/facade/f-range.ts b/packages/sheets/src/facade/f-range.ts
index 956583d27b..3e18ad272d 100644
--- a/packages/sheets/src/facade/f-range.ts
+++ b/packages/sheets/src/facade/f-range.ts
@@ -18,12 +18,12 @@ import type { AbsoluteRefType, BorderStyleTypes, BorderType, CellValue, CustomDa
import type { ISetBorderBasicCommandParams, ISetHorizontalTextAlignCommandParams, ISetRangeValuesCommandParams, ISetSelectionsOperationParams, ISetStyleCommandParams, ISetTextRotationCommandParams, ISetTextWrapCommandParams, ISetVerticalTextAlignCommandParams, IStyleTypeValue, SplitDelimiterEnum } from '@univerjs/sheets';
import type { IFacadeClearOptions } from './f-worksheet';
import type { FHorizontalAlignment, FVerticalAlignment } from './utils';
-import { BooleanNumber, DEFAULT_STYLES, Dimension, ICommandService, Inject, Injector, isNullCell, Rectangle, RichTextValue, TextStyleValue, WrapStrategy } from '@univerjs/core';
+import { BooleanNumber, covertCellValue, covertCellValues, DEFAULT_STYLES, Dimension, ICommandService, Inject, Injector, isNullCell, Rectangle, RichTextValue, TextStyleValue, WrapStrategy } from '@univerjs/core';
import { FBaseInitialable } from '@univerjs/core/facade';
import { FormulaDataModel, serializeRange, serializeRangeWithSheet } from '@univerjs/engine-formula';
import { addMergeCellsUtil, ClearSelectionAllCommand, ClearSelectionContentCommand, ClearSelectionFormatCommand, DeleteRangeMoveLeftCommand, DeleteRangeMoveUpCommand, DeleteWorksheetRangeThemeStyleCommand, getAddMergeMutationRangeByType, getPrimaryForRange, InsertRangeMoveDownCommand, InsertRangeMoveRightCommand, RemoveWorksheetMergeCommand, SetBorderBasicCommand, SetHorizontalTextAlignCommand, SetRangeValuesCommand, SetSelectionsOperation, SetStyleCommand, SetTextRotationCommand, SetTextWrapCommand, SetVerticalTextAlignCommand, SetWorksheetRangeThemeStyleCommand, SheetRangeThemeService, SplitTextToColumnsCommand } from '@univerjs/sheets';
import { FWorkbook } from './f-workbook';
-import { covertCellValue, covertCellValues, transformCoreHorizontalAlignment, transformCoreVerticalAlignment, transformFacadeHorizontalAlignment, transformFacadeVerticalAlignment } from './utils';
+import { transformCoreHorizontalAlignment, transformCoreVerticalAlignment, transformFacadeHorizontalAlignment, transformFacadeVerticalAlignment } from './utils';
export type FontLine = 'none' | 'underline' | 'line-through';
export type FontStyle = 'normal' | 'italic';
diff --git a/packages/sheets/src/facade/f-worksheet.ts b/packages/sheets/src/facade/f-worksheet.ts
index 8f10f1c1df..7c0415182b 100644
--- a/packages/sheets/src/facade/f-worksheet.ts
+++ b/packages/sheets/src/facade/f-worksheet.ts
@@ -18,14 +18,14 @@ import type { CellValue, CustomData, ICellData, IColumnData, IColumnRange, IDisp
import type { ISetColDataCommandParams, ISetGridlinesColorCommandParams, ISetRangeValuesMutationParams, ISetRowDataCommandParams, ISetTextWrapCommandParams, IToggleGridlinesCommandParams } from '@univerjs/sheets';
import type { FDefinedName } from './f-defined-name';
import type { FWorkbook } from './f-workbook';
-import { BooleanNumber, Direction, ICommandService, ILogService, Inject, Injector, ObjectMatrix, RANGE_TYPE, WrapStrategy } from '@univerjs/core';
+import { BooleanNumber, covertCellValue, 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, 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';
-import { covertCellValue, covertToColRange, covertToRowRange } from './utils';
+import { covertToColRange, covertToRowRange } from './utils';
export interface IFacadeClearOptions {
contentsOnly?: boolean;
diff --git a/packages/sheets/src/facade/index.ts b/packages/sheets/src/facade/index.ts
index cdc26b0c86..ef874f072d 100644
--- a/packages/sheets/src/facade/index.ts
+++ b/packages/sheets/src/facade/index.ts
@@ -28,4 +28,3 @@ export { FWorkbook } from './f-workbook';
// eslint-disable-next-line perfectionist/sort-exports
export type * from './f-univer';
export { FWorksheet } from './f-worksheet';
-export { covertCellValues } from './utils';
diff --git a/packages/sheets/src/facade/utils.ts b/packages/sheets/src/facade/utils.ts
index 046f98264b..3040f3a4a3 100644
--- a/packages/sheets/src/facade/utils.ts
+++ b/packages/sheets/src/facade/utils.ts
@@ -14,15 +14,10 @@
* limitations under the License.
*/
-import type { CellValue, ICellData, IObjectMatrixPrimitiveType, IRange, IRangeWithCoord, Nullable, Worksheet } from '@univerjs/core';
+import type { IRange, IRangeWithCoord, Worksheet } from '@univerjs/core';
import {
HorizontalAlign,
- isCellV,
- isFormulaString,
- isICellData,
- ObjectMatrix,
RANGE_TYPE,
- Tools,
VerticalAlign,
} from '@univerjs/core';
@@ -102,67 +97,6 @@ export function transformCoreVerticalAlignment(value: VerticalAlign): FVerticalA
}
}
-/**
- * Covert cell value to cell data.
- * @param {CellValue | ICellData} value - The cell value.
- * @returns {ICellData} The cell data.
- */
-export function covertCellValue(value: CellValue | ICellData): ICellData {
- if (isFormulaString(value)) {
- return {
- f: value as string,
- v: null,
- p: null,
- };
- }
- if (isCellV(value)) {
- return {
- v: value as Nullable<CellValue>,
- p: null,
- f: null,
- };
- }
- if (isICellData(value)) {
- return value;
- }
-
- // maybe {}
- return value as ICellData;
-}
-
-/**
- * Covert cell value array or matrix to cell data.
- * @param {CellValue[][] | IObjectMatrixPrimitiveType<CellValue> | ICellData[][] | IObjectMatrixPrimitiveType<ICellData>} value - The cell value array or matrix.
- * @param {IRange} range - The range.
- * @returns {IObjectMatrixPrimitiveType<ICellData>} The cell data matrix.
- */
-export function covertCellValues(
- value:
- | CellValue[][]
- | IObjectMatrixPrimitiveType<CellValue>
- | ICellData[][]
- | IObjectMatrixPrimitiveType<ICellData>,
- range: IRange
-): IObjectMatrixPrimitiveType<ICellData> {
- const cellValue = new ObjectMatrix<ICellData>();
- const { startRow, startColumn, endRow, endColumn } = range;
-
- if (Tools.isArray(value)) {
- for (let r = 0; r <= endRow - startRow; r++) {
- for (let c = 0; c <= endColumn - startColumn; c++) {
- cellValue.setValue(r + startRow, c + startColumn, covertCellValue(value[r][c]));
- }
- }
- } else {
- const valueMatrix = new ObjectMatrix(value as IObjectMatrixPrimitiveType<ICellData | CellValue>);
- valueMatrix.forValue((r, c, v) => {
- cellValue.setValue(r, c, covertCellValue(v));
- });
- }
-
- return cellValue.getMatrix();
-}
-
/**
* Judge whether the range is merged.
* @param {IRangeWithCoord} mergeInfo - The merge info.