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): range api (#996)

Dushusirac1b1f89ba42
diff --git a/examples/src/uniscript/china-id-validation.js b/examples/src/uniscript/china-id-validation.js
index c70c2d8629..7123a8479f 100644
--- a/examples/src/uniscript/china-id-validation.js
+++ b/examples/src/uniscript/china-id-validation.js
@@ -59,7 +59,7 @@ function validIDNum(szID) {
const redColor = 'rgb(240, 55, 31)';
-const activeSheet = Univer.getCurrentSheet().getActiveSheet();
+const activeSheet = univerAPI.getCurrentSheet().getActiveSheet();
const range = activeSheet.getRange(1, 1, 50, 1);
const row = range.getRow();
const col = range.getColumn();
diff --git a/examples/src/uniscript/draw-univer-logo.js b/examples/src/uniscript/draw-univer-logo.js
index cd238bed06..6316d317bb 100644
--- a/examples/src/uniscript/draw-univer-logo.js
+++ b/examples/src/uniscript/draw-univer-logo.js
@@ -16,7 +16,7 @@
/* eslint-disable */
-const newSheet = Univer.getCurrentSheet().create('Draw Univer Logo', 80, 80);
+const newSheet = univerAPI.getCurrentSheet().create('Draw Univer Logo', 80, 80);
newSheet.setRowHeights(0, 80, 6);
newSheet.setColumnWidths(0, 80, 6);
diff --git a/packages/core/src/types/interfaces/__tests__/i-cell-data.spec.ts b/packages/core/src/types/interfaces/__tests__/i-cell-data.spec.ts
new file mode 100644
index 0000000000..2b87b547fa
--- /dev/null
+++ b/packages/core/src/types/interfaces/__tests__/i-cell-data.spec.ts
@@ -0,0 +1,46 @@
+/**
+ * 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 { isCellV, isICellData } from '../i-cell-data';
+
+describe('Test cell data', () => {
+ it('function isICellData', () => {
+ expect(isICellData({ s: '1' })).toBeTruthy();
+ expect(isICellData({ p: { id: '1' } })).toBeTruthy();
+ expect(isICellData({ v: '1' })).toBeTruthy();
+ expect(isICellData({ f: '=SUM(1)' })).toBeTruthy();
+ expect(isICellData({ si: '1' })).toBeTruthy();
+ expect(isICellData({ t: 1 })).toBeTruthy();
+
+ expect(isICellData({ a: '1' })).toBeFalsy();
+ expect(isICellData(null)).toBeFalsy();
+ expect(isICellData(undefined)).toBeFalsy();
+ expect(isICellData({})).toBeFalsy();
+ });
+
+ it('function isCellV', () => {
+ expect(isCellV('1')).toBeTruthy();
+ expect(isCellV(1)).toBeTruthy();
+ expect(isCellV(true)).toBeTruthy();
+
+ expect(isCellV({ v: '1' })).toBeFalsy();
+ expect(isCellV(null)).toBeFalsy();
+ expect(isCellV(undefined)).toBeFalsy();
+ expect(isCellV({})).toBeFalsy();
+ });
+});
diff --git a/packages/core/src/types/interfaces/i-cell-data.ts b/packages/core/src/types/interfaces/i-cell-data.ts
index bb75d87db0..404cd848f5 100644
--- a/packages/core/src/types/interfaces/i-cell-data.ts
+++ b/packages/core/src/types/interfaces/i-cell-data.ts
@@ -53,12 +53,13 @@ export interface ICellDataForSheetInterceptor extends ICellData {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isICellData(value: any): value is ICellData {
return (
- (value &&
- ((value as ICellData).s !== undefined ||
- (value as ICellData).p !== undefined ||
- (value as ICellData).v !== undefined)) ||
- (value as ICellData).f !== undefined ||
- (value as ICellData).si !== undefined
+ value &&
+ ((value as ICellData).s !== undefined ||
+ (value as ICellData).p !== undefined ||
+ (value as ICellData).v !== undefined ||
+ (value as ICellData).t !== undefined ||
+ (value as ICellData).f !== undefined ||
+ (value as ICellData).si !== undefined)
);
}
@@ -98,3 +99,7 @@ export function isNullCell(cell: Nullable<ICellData>) {
return true;
}
+
+export function isCellV(cell: Nullable<ICellData | ICellV>) {
+ return cell != null && (typeof cell === 'string' || typeof cell === 'number' || typeof cell === 'boolean');
+}
diff --git a/packages/facade/src/apis/sheet/__tests__/create-test-bed.ts b/packages/facade/src/apis/sheet/__tests__/create-test-bed.ts
new file mode 100644
index 0000000000..e2d1a37715
--- /dev/null
+++ b/packages/facade/src/apis/sheet/__tests__/create-test-bed.ts
@@ -0,0 +1,88 @@
+/**
+ * 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 { IWorkbookData, Workbook } from '@univerjs/core';
+import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
+import { SelectionManagerService, SheetInterceptorService } from '@univerjs/sheets';
+import type { Dependency } from '@wendellhu/redi';
+import { Inject, Injector } from '@wendellhu/redi';
+
+import { FUniver } from '../../facade';
+
+const TEST_WORKBOOK_DATA_DEMO: IWorkbookData = {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ cellData: {},
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ styles: {},
+};
+
+export interface ITestBed {
+ univer: Univer;
+ get: Injector['get'];
+ sheet: Workbook;
+ univerAPI: FUniver;
+}
+
+export function createTestBed(workbookConfig?: IWorkbookData, dependencies?: Dependency[]): ITestBed {
+ const univer = new Univer();
+ const injector = univer.__getInjector();
+
+ class TestSpyPlugin extends Plugin {
+ static override type = PluginType.Sheet;
+
+ constructor(
+ _config: undefined,
+ @Inject(Injector) override readonly _injector: Injector
+ ) {
+ super('test-plugin');
+
+ this._injector = _injector;
+ }
+
+ override onStarting(injector: Injector): void {
+ injector.add([SelectionManagerService]);
+ injector.add([SheetInterceptorService]);
+
+ dependencies?.forEach((d) => injector.add(d));
+ }
+ }
+
+ univer.registerPlugin(TestSpyPlugin);
+ const sheet = univer.createUniverSheet(workbookConfig || TEST_WORKBOOK_DATA_DEMO);
+
+ const univerInstanceService = injector.get(IUniverInstanceService);
+ univerInstanceService.focusUniverInstance('test');
+ const logService = injector.get(ILogService);
+
+ logService.setLogLevel(LogLevel.SILENT); // change this to `LogLevel.VERBOSE` to debug tests via logs
+
+ const univerAPI = FUniver.newAPI(injector);
+
+ return {
+ univer,
+ get: injector.get.bind(injector),
+ sheet,
+ univerAPI,
+ };
+}
diff --git a/packages/facade/src/apis/sheet/__tests__/f-range.spec.ts b/packages/facade/src/apis/sheet/__tests__/f-range.spec.ts
new file mode 100644
index 0000000000..598befe7e8
--- /dev/null
+++ b/packages/facade/src/apis/sheet/__tests__/f-range.spec.ts
@@ -0,0 +1,176 @@
+/**
+ * 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 { ICellData, IStyleData, Nullable, Univer } from '@univerjs/core';
+import { ICommandService, IUniverInstanceService } from '@univerjs/core';
+import { SetRangeValuesCommand, SetRangeValuesMutation } from '@univerjs/sheets';
+import type { Injector } from '@wendellhu/redi';
+import { beforeEach, describe, expect, it } from 'vitest';
+
+import type { FUniver } from '../../facade';
+import { createTestBed } from './create-test-bed';
+
+describe('Test FRange', () => {
+ let univer: Univer;
+ let get: Injector['get'];
+ let commandService: ICommandService;
+ let univerAPI: FUniver;
+ let getValueByPosition: (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number
+ ) => Nullable<ICellData>;
+ let getStyleByPosition: (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number
+ ) => Nullable<IStyleData>;
+
+ beforeEach(() => {
+ const testBed = createTestBed();
+ univer = testBed.univer;
+ get = testBed.get;
+ univerAPI = testBed.univerAPI;
+
+ commandService = get(ICommandService);
+ commandService.registerCommand(SetRangeValuesCommand);
+ commandService.registerCommand(SetRangeValuesMutation);
+
+ getValueByPosition = (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number
+ ): Nullable<ICellData> =>
+ get(IUniverInstanceService)
+ .getUniverSheetInstance('test')
+ ?.getSheetBySheetId('sheet1')
+ ?.getRange(startRow, startColumn, endRow, endColumn)
+ .getValue();
+
+ getStyleByPosition = (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number
+ ): Nullable<IStyleData> => {
+ const value = getValueByPosition(startRow, startColumn, endRow, endColumn);
+ const styles = get(IUniverInstanceService).getUniverSheetInstance('test')?.getStyles();
+ if (value && styles) {
+ return styles.getStyleByCell(value);
+ }
+ };
+ });
+
+ it('Range setValue', () => {
+ const activeSheet = univerAPI.getCurrentSheet()?.getActiveSheet();
+
+ // A1 sets the number
+ const range1 = activeSheet?.getRange(0, 0, 1, 1);
+ range1?.setValue(1);
+
+ expect(activeSheet?.getRange(0, 0, 1, 1)?.getValue()).toBe(1);
+
+ // B1:C2 sets the string
+ const range2 = activeSheet?.getRange(0, 1, 2, 2);
+ range2?.setValue({ v: 'test' });
+
+ expect(activeSheet?.getRange(0, 1, 1, 1)?.getValue()).toBe('test');
+ expect(activeSheet?.getRange(0, 2, 1, 1)?.getValue()).toBe('test');
+ expect(activeSheet?.getRange(1, 1, 1, 1)?.getValue()).toBe('test');
+ expect(activeSheet?.getRange(1, 2, 1, 1)?.getValue()).toBe('test');
+
+ // D1:E2 sets numbers and background color
+ const range3 = activeSheet?.getRange(0, 3, 2, 2);
+ range3?.setValue({
+ v: 2,
+ s: {
+ bg: { rgb: 'red' },
+ },
+ });
+
+ expect(activeSheet?.getRange(0, 3, 1, 1)?.getValue()).toBe(2);
+ expect(activeSheet?.getRange(0, 4, 1, 1)?.getValue()).toBe(2);
+ expect(activeSheet?.getRange(1, 3, 1, 1)?.getValue()).toBe(2);
+ expect(activeSheet?.getRange(1, 4, 1, 1)?.getValue()).toBe(2);
+
+ expect(getStyleByPosition(0, 3, 0, 3)?.bg?.rgb).toBe('red');
+ expect(getStyleByPosition(0, 4, 0, 4)?.bg?.rgb).toBe('red');
+ expect(getStyleByPosition(1, 3, 1, 3)?.bg?.rgb).toBe('red');
+ expect(getStyleByPosition(1, 4, 1, 4)?.bg?.rgb).toBe('red');
+ });
+
+ it('Range setValues', () => {
+ const activeSheet = univerAPI.getCurrentSheet()?.getActiveSheet();
+
+ // B3:C4 sets value
+ const range1 = activeSheet?.getRange(2, 1, 2, 2);
+ range1?.setValues([
+ [1, 1],
+ [1, 1],
+ ]);
+
+ expect(activeSheet?.getRange(2, 1, 1, 1)?.getValue()).toBe(1);
+ expect(activeSheet?.getRange(2, 2, 1, 1)?.getValue()).toBe(1);
+ expect(activeSheet?.getRange(3, 1, 1, 1)?.getValue()).toBe(1);
+ expect(activeSheet?.getRange(3, 2, 1, 1)?.getValue()).toBe(1);
+
+ // D3:E4 sets value and background color
+ const range2 = activeSheet?.getRange(2, 3, 2, 2);
+ range2?.setValues({
+ 2: {
+ 3: {
+ v: 3,
+ s: {
+ bg: { rgb: 'yellow' },
+ },
+ },
+ 4: {
+ v: 4,
+ s: {
+ bg: { rgb: 'green' },
+ },
+ },
+ },
+ 3: {
+ 3: {
+ v: 5,
+ s: {
+ bg: { rgb: 'orange' },
+ },
+ },
+ 4: {
+ v: 6,
+ s: {
+ bg: { rgb: 'red' },
+ },
+ },
+ },
+ });
+
+ expect(activeSheet?.getRange(2, 3, 1, 1)?.getValue()).toBe(3);
+ expect(activeSheet?.getRange(2, 4, 1, 1)?.getValue()).toBe(4);
+ expect(activeSheet?.getRange(3, 3, 1, 1)?.getValue()).toBe(5);
+ expect(activeSheet?.getRange(3, 4, 1, 1)?.getValue()).toBe(6);
+
+ expect(getStyleByPosition(2, 3, 2, 3)?.bg?.rgb).toBe('yellow');
+ expect(getStyleByPosition(2, 4, 2, 4)?.bg?.rgb).toBe('green');
+ expect(getStyleByPosition(3, 3, 3, 3)?.bg?.rgb).toBe('orange');
+ expect(getStyleByPosition(3, 4, 3, 4)?.bg?.rgb).toBe('red');
+ });
+});
diff --git a/packages/facade/src/apis/sheet/__tests__/utils.spec.ts b/packages/facade/src/apis/sheet/__tests__/utils.spec.ts
new file mode 100644
index 0000000000..40c880bc72
--- /dev/null
+++ b/packages/facade/src/apis/sheet/__tests__/utils.spec.ts
@@ -0,0 +1,85 @@
+/**
+ * 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 { covertCellValue, covertCellValues } from '../utils';
+
+describe('Test utils', () => {
+ it('function covertCellValue', () => {
+ expect(covertCellValue('=SUM(1)')).toStrictEqual({ f: '=SUM(1)' });
+
+ expect(covertCellValue('1')).toStrictEqual({ v: '1' });
+ expect(covertCellValue(1)).toStrictEqual({ v: 1 });
+ expect(covertCellValue(true)).toStrictEqual({ v: true });
+
+ expect(covertCellValue({})).toStrictEqual({});
+ });
+
+ it('function covertCellValues', () => {
+ expect(
+ covertCellValues(
+ [
+ [1, 2],
+ [3, 4],
+ ],
+ { startRow: 1, startColumn: 1, endRow: 2, endColumn: 2 }
+ )
+ ).toStrictEqual({
+ 1: { 1: { v: 1 }, 2: { v: 2 } },
+ 2: { 1: { v: 3 }, 2: { v: 4 } },
+ });
+
+ expect(
+ covertCellValues(
+ {
+ 1: { 1: 1, 2: 2 },
+ 2: { 1: 3, 2: 4 },
+ },
+ { startRow: 1, startColumn: 1, endRow: 2, endColumn: 2 }
+ )
+ ).toStrictEqual({
+ 1: { 1: { v: 1 }, 2: { v: 2 } },
+ 2: { 1: { v: 3 }, 2: { v: 4 } },
+ });
+
+ expect(
+ covertCellValues(
+ [
+ [{ v: 1 }, { v: 2 }],
+ [{ v: 3 }, { v: 4 }],
+ ],
+ { startRow: 1, startColumn: 1, endRow: 2, endColumn: 2 }
+ )
+ ).toStrictEqual({
+ 1: { 1: { v: 1 }, 2: { v: 2 } },
+ 2: { 1: { v: 3 }, 2: { v: 4 } },
+ });
+
+ expect(
+ covertCellValues(
+ {
+ 1: { 1: { v: 1 }, 2: { v: 2 } },
+ 2: { 1: { v: 3 }, 2: { v: 4 } },
+ },
+ { startRow: 1, startColumn: 1, endRow: 2, endColumn: 2 }
+ )
+ ).toStrictEqual({
+ 1: { 1: { v: 1 }, 2: { v: 2 } },
+ 2: { 1: { v: 3 }, 2: { v: 4 } },
+ });
+ });
+});
diff --git a/packages/facade/src/apis/sheet/f-range.ts b/packages/facade/src/apis/sheet/f-range.ts
index af2f001771..7efdbf7a07 100644
--- a/packages/facade/src/apis/sheet/f-range.ts
+++ b/packages/facade/src/apis/sheet/f-range.ts
@@ -14,12 +14,22 @@
* limitations under the License.
*/
-import type { ICellV, IColorStyle, IRange, Workbook, Worksheet } from '@univerjs/core';
+import type {
+ ICellData,
+ ICellV,
+ IColorStyle,
+ IObjectMatrixPrimitiveType,
+ IRange,
+ Workbook,
+ Worksheet,
+} from '@univerjs/core';
import { ICommandService } from '@univerjs/core';
import type { ISetStyleCommandParams } from '@univerjs/sheets';
-import { SetStyleCommand } from '@univerjs/sheets';
+import { SetRangeValuesCommand, SetStyleCommand } from '@univerjs/sheets';
import { Inject, Injector } from '@wendellhu/redi';
+import { covertCellValue, covertCellValues } from './utils';
+
export class FRange {
constructor(
private readonly _workbook: Workbook,
@@ -62,4 +72,40 @@ export class FRange {
},
} as ISetStyleCommandParams<IColorStyle>);
}
+
+ /**
+ * The value can be a number, string, boolean, or standard cell format. If it begins with `=`, it is interpreted as a formula. The value is tiled to all cells in the range.
+ * @param value
+ */
+ setValue(value: ICellV | ICellData): void {
+ const realValue = covertCellValue(value);
+
+ if (!realValue) {
+ throw new Error('Invalid value');
+ }
+
+ this._commandService.executeCommand(SetRangeValuesCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ range: this._range,
+ value: realValue,
+ });
+ }
+
+ /**
+ * Sets a different value for each cell in the range. The value can be a two-dimensional array or a standard range matrix (must match the dimensions of this range), consisting of numbers, strings, Boolean values or Composed of standard cell formats. If a value begins with `=`, it is interpreted as a formula.
+ * @param value
+ */
+ setValues(
+ value: ICellV[][] | IObjectMatrixPrimitiveType<ICellV> | ICellData[][] | IObjectMatrixPrimitiveType<ICellData>
+ ): void {
+ const realValue = covertCellValues(value, this._range);
+
+ this._commandService.executeCommand(SetRangeValuesCommand.id, {
+ unitId: this._workbook.getUnitId(),
+ subUnitId: this._worksheet.getSheetId(),
+ range: this._range,
+ value: realValue,
+ });
+ }
}
diff --git a/packages/facade/src/apis/sheet/utils.ts b/packages/facade/src/apis/sheet/utils.ts
new file mode 100644
index 0000000000..b667065e8c
--- /dev/null
+++ b/packages/facade/src/apis/sheet/utils.ts
@@ -0,0 +1,71 @@
+/**
+ * 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 { ICellData, ICellV, IObjectMatrixPrimitiveType, IRange, Nullable } from '@univerjs/core';
+import { isCellV, isFormulaString, isICellData, ObjectMatrix, Tools } from '@univerjs/core';
+
+/**
+ * covert cell value to cell data
+ * @param value
+ * @returns
+ */
+export function covertCellValue(value: ICellV | ICellData): ICellData {
+ if (isFormulaString(value)) {
+ return {
+ f: value as string,
+ };
+ }
+ if (isCellV(value)) {
+ return {
+ v: value as Nullable<ICellV>,
+ };
+ }
+ if (isICellData(value)) {
+ return value;
+ }
+
+ // maybe {}
+ return value as ICellData;
+}
+
+/**
+ * covert cell value array or matrix to cell data
+ * @param value
+ * @param range
+ * @returns
+ */
+export function covertCellValues(
+ value: ICellV[][] | IObjectMatrixPrimitiveType<ICellV> | 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 | ICellV>);
+ valueMatrix.forValue((r, c, v) => {
+ cellValue.setValue(r, c, covertCellValue(v));
+ });
+ }
+
+ return cellValue.getMatrix();
+}
diff --git a/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts b/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts
index f9a587eb90..5f6ec8eb81 100644
--- a/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts
+++ b/packages/sheets/src/commands/commands/__tests__/set-range-values.command.spec.ts
@@ -38,7 +38,13 @@ describe('Test set range values commands', () => {
let get: Injector['get'];
let commandService: ICommandService;
let selectionManager: SelectionManagerService;
- let getValue: () => any;
+ let getValue: () => Nullable<ICellData>;
+ let getValues: (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number
+ ) => Nullable<Array<Array<Nullable<ICellData>>>>;
let getStyle: () => any;
beforeEach(() => {
@@ -71,6 +77,18 @@ describe('Test set range values commands', () => {
?.getRange(0, 0, 0, 0)
.getValue();
+ getValues = (
+ startRow: number,
+ startColumn: number,
+ endRow: number,
+ endColumn: number
+ ): Nullable<Array<Array<Nullable<ICellData>>>> =>
+ get(IUniverInstanceService)
+ .getUniverSheetInstance('test')
+ ?.getSheetBySheetId('sheet1')
+ ?.getRange(startRow, startColumn, endRow, endColumn)
+ .getValues();
+
getStyle = (): Nullable<IStyleData> => {
const value = getValue();
const styles = get(IUniverInstanceService).getUniverSheetInstance('test')?.getStyles();
@@ -147,7 +165,88 @@ describe('Test set range values commands', () => {
// redo
expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
expect(getValue()).toStrictEqual(getParams().value);
+
+ // reset
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
});
+
+ it('will tile all values when there is a selected range', async () => {
+ function getParams() {
+ const params: ISetRangeValuesCommandParams = {
+ range: {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 1,
+ endColumn: 1,
+ },
+ value: {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ };
+
+ return params;
+ }
+
+ expect(await commandService.executeCommand(SetRangeValuesCommand.id, getParams())).toBeTruthy();
+ expect(getValues(0, 0, 1, 1)).toStrictEqual([
+ [
+ {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ ],
+ [
+ {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ ],
+ ]);
+ // undo
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ expect(getValue()).toStrictEqual({
+ v: 'A1',
+ t: CellValueType.STRING,
+ });
+
+ // redo
+ expect(await commandService.executeCommand(RedoCommand.id)).toBeTruthy();
+ expect(getValues(0, 0, 1, 1)).toStrictEqual([
+ [
+ {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ ],
+ [
+ {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ {
+ v: 'a1',
+ t: CellValueType.STRING,
+ },
+ ],
+ ]);
+
+ // reset
+ expect(await commandService.executeCommand(UndoCommand.id)).toBeTruthy();
+ });
+
it('set formats', async () => {
// set IStyleBase, the original cell has no style information
function getParamsStyleBase() {
diff --git a/packages/sheets/src/commands/commands/set-range-values.command.ts b/packages/sheets/src/commands/commands/set-range-values.command.ts
index c9de7cd3f0..66939f1a66 100644
--- a/packages/sheets/src/commands/commands/set-range-values.command.ts
+++ b/packages/sheets/src/commands/commands/set-range-values.command.ts
@@ -85,9 +85,13 @@ export const SetRangeValuesCommand: ICommand = {
}
} else if (isICellData(value)) {
for (let i = 0; i < currentSelections.length; i++) {
- const { startRow, startColumn } = currentSelections[i];
+ const { startRow, startColumn, endRow, endColumn } = currentSelections[i];
- cellValue.setValue(startRow, startColumn, value);
+ for (let r = startRow; r <= endRow; r++) {
+ for (let c = startColumn; c <= endColumn; c++) {
+ cellValue.setValue(r, c, value);
+ }
+ }
}
} else {
realCellValue = value as IObjectMatrixPrimitiveType<ICellData>;
diff --git a/packages/uniscript/src/services/script-execution.service.ts b/packages/uniscript/src/services/script-execution.service.ts
index e8f9dca6e0..e42a91a0bf 100644
--- a/packages/uniscript/src/services/script-execution.service.ts
+++ b/packages/uniscript/src/services/script-execution.service.ts
@@ -34,7 +34,7 @@ export class UniscriptExecutionService extends Disposable {
const apiInstance = FUniver.newAPI(this._injector);
// eslint-disable-next-line @typescript-eslint/no-implied-eval
- const scriptFunction = new Function('Univer', `(() => {${code}})()`);
+ const scriptFunction = new Function('univerAPI', `(() => {${code}})()`);
try {
scriptFunction(apiInstance);