Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
chore(sheet): set null cell util (#2490)
Dushusir8a1bf8d2667f
diff --git a/packages/sheets-ui/src/commands/commands/auto-fill.command.ts b/packages/sheets-ui/src/commands/commands/auto-fill.command.tsindex ae59087b8a..a2408e4151 100644--- a/packages/sheets-ui/src/commands/commands/auto-fill.command.ts+++ b/packages/sheets-ui/src/commands/commands/auto-fill.command.ts@@ -17,10 +17,9 @@import type { ICommand, IRange } from '@univerjs/core';import { CommandType, ICommandService, IUndoRedoService, IUniverInstanceService } from '@univerjs/core';import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';-import { getSheetCommandTarget, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SetSelectionsOperation } from '@univerjs/sheets';+import { generateNullCellValue, getSheetCommandTarget, SetRangeValuesMutation, SetRangeValuesUndoMutationFactory, SetSelectionsOperation } from '@univerjs/sheets';import type { IAccessor } from '@wendellhu/redi';-import { generateNullCellValue } from '../../services/auto-fill/tools';import { IAutoFillService } from '../../services/auto-fill/auto-fill.service';export interface IAutoFillCommandParams {diff --git a/packages/sheets-ui/src/controllers/auto-fill.controller.ts b/packages/sheets-ui/src/controllers/auto-fill.controller.tsindex e7266cf870..3a0d056a08 100644--- a/packages/sheets-ui/src/controllers/auto-fill.controller.ts+++ b/packages/sheets-ui/src/controllers/auto-fill.controller.ts@@ -81,7 +81,7 @@ import { ISelectionRenderService } from '../services/selection/selection-render.import { SetCellEditVisibleOperation } from '../commands/operations/cell-edit.operation';import { SetZoomRatioOperation } from '../commands/operations/set-zoom-ratio.operation';import type { IDiscreteRange } from './utils/range-tools';-import { discreteRangeToRange, generateNullCellValue, rangeToDiscreteRange } from './utils/range-tools';+import { discreteRangeToRange, generateNullCellValueRowCol, rangeToDiscreteRange } from './utils/range-tools';@OnLifecycle(LifecycleStages.Steady, AutoFillController)export class AutoFillController extends Disposable {@@ -852,7 +852,7 @@ export class AutoFillController extends Disposable {const clearMutationParams: ISetRangeValuesMutationParams = {subUnitId,unitId,- cellValue: generateNullCellValue([target]),+ cellValue: generateNullCellValueRowCol([target]),};const undoClearMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(accessor,diff --git a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.tsindex e072e7a9e9..e40e0a454e 100644--- a/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts+++ b/packages/sheets-ui/src/controllers/clipboard/clipboard.controller.ts@@ -22,7 +22,6 @@ import type {IObjectMatrixPrimitiveType,IRange,IRowData,- Nullable,Workbook,Worksheet,} from '@univerjs/core';@@ -789,18 +788,3 @@ export class SheetClipboardController extends RxDisposable {return worksheet;}}--// Generate cellValue from range and set null-function generateNullCellValue(range: IRange[]): IObjectMatrixPrimitiveType<Nullable<ICellData>> {- const cellValue = new ObjectMatrix<Nullable<ICellData>>();- range.forEach((range: IRange) => {- const { startRow, startColumn, endRow, endColumn } = range;- for (let i = startRow; i <= endRow; i++) {- for (let j = startColumn; j <= endColumn; j++) {- cellValue.setValue(i, j, null);- }- }- });-- return cellValue.getData();-}diff --git a/packages/sheets-ui/src/controllers/utils/range-tools.ts b/packages/sheets-ui/src/controllers/utils/range-tools.tsindex 155d482ca6..535fbc5404 100644--- a/packages/sheets-ui/src/controllers/utils/range-tools.ts+++ b/packages/sheets-ui/src/controllers/utils/range-tools.ts@@ -96,7 +96,7 @@ export function virtualizeDiscreteRanges(ranges: IDiscreteRange[]): {};}-export function generateNullCellValue(range: IDiscreteRange[]): IObjectMatrixPrimitiveType<ICellData> {+export function generateNullCellValueRowCol(range: IDiscreteRange[]): IObjectMatrixPrimitiveType<ICellData> {const cellValue = new ObjectMatrix<ICellData>();range.forEach((r) => {const { rows, cols } = r;@@ -107,6 +107,7 @@ export function generateNullCellValue(range: IDiscreteRange[]): IObjectMatrixPrip: null,f: null,si: null,+ custom: null,});});});diff --git a/packages/sheets-ui/src/index.ts b/packages/sheets-ui/src/index.tsindex 3c6006b297..2c812adadd 100644--- a/packages/sheets-ui/src/index.ts+++ b/packages/sheets-ui/src/index.ts@@ -50,7 +50,7 @@ export { useActiveWorkbook, useActiveWorksheet } from './components/hook';export { whenSheetEditorFocused } from './controllers/shortcuts/utils';export type { IEditorBridgeServiceParam } from './services/editor-bridge.service';export { AutoFillService, IAutoFillService } from './services/auto-fill/auto-fill.service';-export { getAutoFillRepeatRange, generateNullCellValue } from './services/auto-fill/tools';+export { getAutoFillRepeatRange } from './services/auto-fill/tools';export type { ICopyDataPiece, ISheetAutoFillHook } from './services/auto-fill/type';export { APPLY_TYPE, DATA_TYPE, type IAutoFillRule } from './services/auto-fill/type';export { type ICopyDataInTypeIndexInfo } from './services/auto-fill/type';diff --git a/packages/sheets-ui/src/services/auto-fill/tools.ts b/packages/sheets-ui/src/services/auto-fill/tools.tsindex 3892f02f5d..434a314569 100644--- a/packages/sheets-ui/src/services/auto-fill/tools.ts+++ b/packages/sheets-ui/src/services/auto-fill/tools.ts@@ -14,8 +14,8 @@* limitations under the License.*/-import type { ICellData, IObjectMatrixPrimitiveType, IRange, Nullable } from '@univerjs/core';-import { CellValueType, Direction, ObjectMatrix, Tools } from '@univerjs/core';+import type { ICellData, IRange, Nullable } from '@univerjs/core';+import { CellValueType, Direction, Tools } from '@univerjs/core';export const chnNumChar = { 零: 0, 一: 1, 二: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9 };export const chnNumChar2 = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];@@ -781,23 +781,3 @@ export function getAutoFillRepeatRange(sourceRange: IRange, targetRange: IRange)return repeats;}--// Generate cellValue from range and set v/p/f/si to null-export function generateNullCellValue(range: IRange[]): IObjectMatrixPrimitiveType<ICellData> {- const cellValue = new ObjectMatrix<ICellData>();- range.forEach((r: IRange) => {- const { startRow, startColumn, endRow, endColumn } = r;- for (let i = startRow; i <= endRow; i++) {- for (let j = startColumn; j <= endColumn; j++) {- cellValue.setValue(i, j, {- v: null,- p: null,- f: null,- si: null,- });- }- }- });-- return cellValue.getData();-}diff --git a/packages/sheets/src/basics/__tests__/utils.spec.ts b/packages/sheets/src/basics/__tests__/utils.spec.tsnew file mode 100644index 0000000000..9908508f62--- /dev/null+++ b/packages/sheets/src/basics/__tests__/utils.spec.ts@@ -0,0 +1,76 @@+/**+ * 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 { cellToRange, type IRange } from '@univerjs/core';+import { describe, expect, it } from 'vitest';++import { generateNullCell, generateNullCellStyle, generateNullCellValue } from '../utils';++describe('Test utils', () => {+ it('Test generateNullCell', () => {+ const range: IRange[] = [cellToRange(0, 0), cellToRange(1, 1)];+ const result = generateNullCell(range);+ expect(result).toEqual({+ 0: {+ 0: null,+ },+ 1: {+ 1: null,+ },+ });+ });++ it('Test generateNullCellValue', () => {+ const range: IRange[] = [cellToRange(0, 0), cellToRange(1, 1)];+ const result = generateNullCellValue(range);+ expect(result).toEqual({+ 0: {+ 0: {+ v: null,+ p: null,+ f: null,+ si: null,+ custom: null,+ },+ },+ 1: {+ 1: { v: null,+ p: null,+ f: null,+ si: null,+ custom: null,+ },+ },+ });+ });++ it('Test generateNullCellStyle', () => {+ const range: IRange[] = [cellToRange(0, 0), cellToRange(1, 1)];+ const result = generateNullCellStyle(range);+ expect(result).toEqual({+ 0: {+ 0: {+ s: null,+ },+ },+ 1: {+ 1: {+ s: null,+ },+ },+ });+ });+});diff --git a/packages/sheets/src/basics/utils.ts b/packages/sheets/src/basics/utils.tsindex 0c6f0cb521..b9693b7d95 100644--- a/packages/sheets/src/basics/utils.ts+++ b/packages/sheets/src/basics/utils.ts@@ -14,6 +14,10 @@* limitations under the License.*/+import type { ICellData, IObjectMatrixPrimitiveType, IRange } from '@univerjs/core';+import { ObjectMatrix } from '@univerjs/core';+import type { Nullable } from 'vitest';+export const groupByKey = <T = Record<string, unknown>>(arr: T[], key: string, blankKey = '') => {return arr.reduce((result, current) => {@@ -47,3 +51,67 @@ export const createUniqueKey = (initValue = 0) => {return _initValue++;};};++/**+ * Generate cellValue from range and set null+ * @param range+ * @returns+ */+export function generateNullCell(range: IRange[]): IObjectMatrixPrimitiveType<Nullable<ICellData>> {+ const cellValue = new ObjectMatrix<Nullable<ICellData>>();+ range.forEach((range: IRange) => {+ const { startRow, startColumn, endRow, endColumn } = range;+ for (let i = startRow; i <= endRow; i++) {+ for (let j = startColumn; j <= endColumn; j++) {+ cellValue.setValue(i, j, null);+ }+ }+ });++ return cellValue.clone();+}++/**+ * Generate cellValue from range and set v/p/f/si/custom to null+ * @param range+ * @returns+ */+export function generateNullCellValue(range: IRange[]): IObjectMatrixPrimitiveType<ICellData> {+ const cellValue = new ObjectMatrix<ICellData>();+ range.forEach((range: IRange) => {+ const { startRow, startColumn, endRow, endColumn } = range;+ for (let i = startRow; i <= endRow; i++) {+ for (let j = startColumn; j <= endColumn; j++) {+ cellValue.setValue(i, j, {+ v: null,+ p: null,+ f: null,+ si: null,+ custom: null,+ });+ }+ }+ });++ return cellValue.clone();+}++// Generate cellValue from range and set s to null+export function generateNullCellStyle(ranges: IRange[]): IObjectMatrixPrimitiveType<ICellData> {+ const cellValue = new ObjectMatrix<ICellData>();++ ranges.forEach((range: IRange) => {+ const { startRow, startColumn, endRow, endColumn } = range;++ for (let i = startRow; i <= endRow; i++) {+ for (let j = startColumn; j <= endColumn; j++) {+ cellValue.setValue(i, j, {+ s: null,+ });+ }+ }+ });++ return cellValue.clone();+}+diff --git a/packages/sheets/src/commands/commands/clear-selection-all.command.ts b/packages/sheets/src/commands/commands/clear-selection-all.command.tsindex a5f2e3cbe8..d3e5a5d40c 100644--- a/packages/sheets/src/commands/commands/clear-selection-all.command.ts+++ b/packages/sheets/src/commands/commands/clear-selection-all.command.ts@@ -14,13 +14,12 @@* limitations under the License.*/-import type { ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange, Nullable, Workbook } from '@univerjs/core';+import type { ICommand, IMutationInfo, Workbook } from '@univerjs/core';import {CommandType,ICommandService,IUndoRedoService,IUniverInstanceService,- ObjectMatrix,sequenceExecute,UniverInstanceType,} from '@univerjs/core';@@ -30,6 +29,7 @@ import { SelectionManagerService } from '../../services/selection-manager.servicimport { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';+import { generateNullCell } from '../../basics/utils';/*** The command to clear all in current selected ranges.@@ -62,7 +62,7 @@ export const ClearSelectionAllCommand: ICommand = {const clearMutationParams: ISetRangeValuesMutationParams = {subUnitId,unitId,- cellValue: generateNullCellValue(selections),+ cellValue: generateNullCell(selections),};const undoClearMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(accessor,@@ -100,17 +100,3 @@ export const ClearSelectionAllCommand: ICommand = {},};-// Generate cellValue from range and set null-function generateNullCellValue(range: IRange[]): IObjectMatrixPrimitiveType<Nullable<ICellData>> {- const cellValue = new ObjectMatrix<Nullable<ICellData>>();- range.forEach((range: IRange) => {- const { startRow, startColumn, endRow, endColumn } = range;- for (let i = startRow; i <= endRow; i++) {- for (let j = startColumn; j <= endColumn; j++) {- cellValue.setValue(i, j, null);- }- }- });-- return cellValue.clone();-}diff --git a/packages/sheets/src/commands/commands/clear-selection-content.command.ts b/packages/sheets/src/commands/commands/clear-selection-content.command.tsindex 944c8d03a9..56461fa9bd 100644--- a/packages/sheets/src/commands/commands/clear-selection-content.command.ts+++ b/packages/sheets/src/commands/commands/clear-selection-content.command.ts@@ -14,13 +14,12 @@* limitations under the License.*/-import type { ICellData, ICommand, IObjectMatrixPrimitiveType, IRange, Workbook } from '@univerjs/core';+import type { ICommand, Workbook } from '@univerjs/core';import {CommandType,ICommandService,IUndoRedoService,IUniverInstanceService,- ObjectMatrix,sequenceExecute,UniverInstanceType,} from '@univerjs/core';@@ -30,6 +29,7 @@ import { SelectionManagerService } from '../../services/selection-manager.servicimport { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';+import { generateNullCellValue } from '../../basics/utils';/*** The command to clear content in current selected ranges.@@ -87,24 +87,3 @@ export const ClearSelectionContentCommand: ICommand = {return false;},};--// Generate cellValue from range and set v/p/f/si to null-function generateNullCellValue(range: IRange[]): IObjectMatrixPrimitiveType<ICellData> {- const cellValue = new ObjectMatrix<ICellData>();- range.forEach((range: IRange) => {- const { startRow, startColumn, endRow, endColumn } = range;- for (let i = startRow; i <= endRow; i++) {- for (let j = startColumn; j <= endColumn; j++) {- cellValue.setValue(i, j, {- v: null,- p: null,- f: null,- si: null,- custom: null,- });- }- }- });-- return cellValue.clone();-}diff --git a/packages/sheets/src/commands/commands/clear-selection-format.command.ts b/packages/sheets/src/commands/commands/clear-selection-format.command.tsindex 8e7d092b1e..6c7fcc868b 100644--- a/packages/sheets/src/commands/commands/clear-selection-format.command.ts+++ b/packages/sheets/src/commands/commands/clear-selection-format.command.ts@@ -14,13 +14,12 @@* limitations under the License.*/-import type { ICellData, ICommand, IMutationInfo, IObjectMatrixPrimitiveType, IRange, Workbook } from '@univerjs/core';+import type { ICommand, IMutationInfo, Workbook } from '@univerjs/core';import {CommandType,ICommandService,IUndoRedoService,IUniverInstanceService,- ObjectMatrix,sequenceExecute,UniverInstanceType,} from '@univerjs/core';@@ -30,6 +29,7 @@ import { SelectionManagerService } from '../../services/selection-manager.servicimport { SheetInterceptorService } from '../../services/sheet-interceptor/sheet-interceptor.service';import type { ISetRangeValuesMutationParams } from '../mutations/set-range-values.mutation';import { SetRangeValuesMutation, SetRangeValuesUndoMutationFactory } from '../mutations/set-range-values.mutation';+import { generateNullCellStyle } from '../../basics/utils';/*** The command to clear content in current selected ranges.@@ -64,7 +64,7 @@ export const ClearSelectionFormatCommand: ICommand = {const clearMutationParams: ISetRangeValuesMutationParams = {subUnitId,unitId,- cellValue: generateNullCellValue(selections),+ cellValue: generateNullCellStyle(selections),};const undoClearMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(accessor,@@ -104,21 +104,3 @@ export const ClearSelectionFormatCommand: ICommand = {},};-// Generate cellValue from range and set s to null-export function generateNullCellValue(ranges: IRange[]): IObjectMatrixPrimitiveType<ICellData> {- const cellValue = new ObjectMatrix<ICellData>();-- ranges.forEach((range: IRange) => {- const { startRow, startColumn, endRow, endColumn } = range;-- for (let i = startRow; i <= endRow; i++) {- for (let j = startColumn; j <= endColumn; j++) {- cellValue.setValue(i, j, {- s: null,- });- }- }- });-- return cellValue.clone();-}diff --git a/packages/sheets/src/commands/utils/handle-range-mutation.ts b/packages/sheets/src/commands/utils/handle-range-mutation.tsindex 981304989e..d13cd58dfc 100644--- a/packages/sheets/src/commands/utils/handle-range-mutation.ts+++ b/packages/sheets/src/commands/utils/handle-range-mutation.ts@@ -31,6 +31,7 @@ import {SetRangeValuesUndoMutationFactory,} from '../mutations/set-range-values.mutation';import { getSheetMutationTarget } from '../commands/utils/target-util';+import { generateNullCell } from '../../basics/utils';/*** Generate undo mutation of a `InsertRangeMutation`@@ -196,7 +197,7 @@ export function getRemoveRangeMutations(accessor: IAccessor, params: IDeleteRangconst setRangeValuesMutationParams: ISetRangeValuesMutationParams = {subUnitId,unitId,- cellValue: generateNullCellValue([range]),+ cellValue: generateNullCell([range]),};const undoSetRangeValuesMutationParams: ISetRangeValuesMutationParams = SetRangeValuesUndoMutationFactory(accessor,@@ -436,17 +437,3 @@ export function handleDeleteRangeMutation<T>(}}}--export function generateNullCellValue(range: IRange[]): IObjectMatrixPrimitiveType<Nullable<ICellData>> {- const cellValue = new ObjectMatrix<Nullable<ICellData>>();- range.forEach((range: IRange) => {- const { startRow, startColumn, endRow, endColumn } = range;- for (let i = startRow; i <= endRow; i++) {- for (let j = startColumn; j <= endColumn; j++) {- cellValue.setValue(i, j, null);- }- }- });-- return cellValue.getData();-}diff --git a/packages/sheets/src/index.ts b/packages/sheets/src/index.tsindex f36bb39f5b..48f7678d08 100644--- a/packages/sheets/src/index.ts+++ b/packages/sheets/src/index.ts@@ -378,3 +378,4 @@ export { RangeProtectionPermissionEditPoint } from './services/permission/permisexport { RangeProtectionPermissionViewPoint } from './services/permission/permission-point/range/view';export type { IAddWorksheetProtectionParams } from './commands/mutations/add-worksheet-protection.mutation';+export { generateNullCellValue } from './basics/utils';