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.

fix(formula): insert function bug (#1002)

Dushusirf722fc2e9bba
diff --git a/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts b/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts
index ddf26aa07c..58d16ce2e8 100644
--- a/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts
+++ b/packages/sheets-formula/src/commands/operations/__tests__/insert-function.operation.spec.ts
@@ -27,7 +27,12 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
import { InsertFunctionCommand } from '../../commands/insert-function.command';
import type { IInsertFunctionOperationParams } from '../insert-function.operation';
-import { InsertFunctionOperation } from '../insert-function.operation';
+import {
+ InsertFunctionOperation,
+ isMultiRowsColumnsRange,
+ isNumberCell,
+ isSingleCell,
+} from '../insert-function.operation';
import { createCommandTestBed } from './create-command-test-bed';
describe('Test insert function operation', () => {
@@ -140,5 +145,75 @@ describe('Test insert function operation', () => {
expect(result).toBeFalsy();
});
});
+
+ describe('function isNumberCell', () => {
+ it('should return true when cell type is number', () => {
+ const cell = { t: 2, v: 1 };
+ expect(isNumberCell(cell)).toBeTruthy();
+ });
+
+ it('should return true when cell is number', () => {
+ const cell = { v: 1 };
+ expect(isNumberCell(cell)).toBeTruthy();
+ });
+
+ it('should return false when cell is string number', () => {
+ const cell = { v: '1' };
+ expect(isNumberCell(cell)).toBeFalsy();
+ });
+
+ it('should return false when cell is string', () => {
+ const cell = { v: 'test' };
+ expect(isNumberCell(cell)).toBeFalsy();
+ });
+
+ it('should return false when cell is null', () => {
+ const cell = null;
+ expect(isNumberCell(cell)).toBeFalsy();
+ });
+
+ it('should return false when cell is undefined', () => {
+ const cell = undefined;
+ expect(isNumberCell(cell)).toBeFalsy();
+ });
+
+ it('should return false when cell is empty object', () => {
+ const cell = {};
+ expect(isNumberCell(cell)).toBeFalsy();
+ });
+ });
+
+ describe('function isSingleCell', () => {
+ it('should return true when startRow === endRow and startColumn === endColumn', () => {
+ const range = { startRow: 1, startColumn: 1, endRow: 1, endColumn: 1 };
+ expect(isSingleCell(range)).toBeTruthy();
+ });
+
+ it('should return false when startRow !== endRow', () => {
+ const range = { startRow: 1, startColumn: 1, endRow: 2, endColumn: 1 };
+ expect(isSingleCell(range)).toBeFalsy();
+ });
+
+ it('should return false when startColumn !== endColumn', () => {
+ const range = { startRow: 1, startColumn: 1, endRow: 1, endColumn: 2 };
+ expect(isSingleCell(range)).toBeFalsy();
+ });
+ });
+ describe('function isMultiRowsColumnsRange', () => {
+ it('should return true when startRow !== endRow and startColumn !== endColumn', () => {
+ const range = { startRow: 1, startColumn: 1, endRow: 2, endColumn: 2 };
+ expect(isMultiRowsColumnsRange(range)).toBeTruthy();
+ });
+
+ it('should return false when startRow === endRow', () => {
+ const range = { startRow: 1, startColumn: 1, endRow: 1, endColumn: 2 };
+ expect(isMultiRowsColumnsRange(range)).toBeFalsy();
+ });
+
+ it('should return false when startColumn === endColumn', () => {
+ const range = { startRow: 1, startColumn: 1, endRow: 2, endColumn: 1 };
+ expect(isMultiRowsColumnsRange(range)).toBeFalsy();
+ });
+ });
});
});
diff --git a/packages/sheets-formula/src/commands/operations/insert-function.operation.ts b/packages/sheets-formula/src/commands/operations/insert-function.operation.ts
index 26aedbcb17..edfcf8b181 100644
--- a/packages/sheets-formula/src/commands/operations/insert-function.operation.ts
+++ b/packages/sheets-formula/src/commands/operations/insert-function.operation.ts
@@ -23,7 +23,6 @@ import {
IUniverInstanceService,
Rectangle,
serializeRange,
- Tools,
} from '@univerjs/core';
import {
getCellAtRowCol,
@@ -73,36 +72,57 @@ export const InsertFunctionOperation: ICommand = {
let editRange: IRange | null = null;
let editRow = 0;
let editColumn = 0;
-
- currentSelections.some((selection) => {
- const { range, primary } = selection;
-
+ let editFormulaRangeString = '';
+
+ // Whether or not there is a matching refRange, single range with one cell or multiple rows and columns, enter edit mode
+ if (
+ currentSelections.length === 1 &&
+ (isSingleCell(currentSelections[0].range) || isMultiRowsColumnsRange(currentSelections[0].range))
+ ) {
+ const { range, primary } = currentSelections[0];
const row = primary?.actualRow ?? range.startRow;
const column = primary?.actualColumn ?? range.startColumn;
+ editRange = range;
+ editRow = row;
+ editColumn = column;
+
const refRange = findRefRange(cellMatrix, row, column);
- if (!refRange) {
- editRange = range;
- editRow = row;
- editColumn = column;
- return true;
+ if (refRange) {
+ editFormulaRangeString = serializeRange(refRange);
}
-
- const rangeString = serializeRange(refRange);
- const formulaString = `=${value}(${rangeString})`;
-
- list.push({
- range,
- primary: {
- row,
- column,
- },
- formula: formulaString,
+ } else {
+ currentSelections.some((selection) => {
+ const { range, primary } = selection;
+
+ const row = primary?.actualRow ?? range.startRow;
+ const column = primary?.actualColumn ?? range.startColumn;
+
+ const refRange = findRefRange(cellMatrix, row, column);
+
+ if (!refRange) {
+ editRange = range;
+ editRow = row;
+ editColumn = column;
+ return true;
+ }
+
+ const rangeString = serializeRange(refRange);
+ const formulaString = `=${value}(${rangeString})`;
+
+ list.push({
+ range,
+ primary: {
+ row,
+ column,
+ },
+ formula: formulaString,
+ });
+
+ return false;
});
-
- return false;
- });
+ }
if (editRange) {
// set current position
@@ -133,7 +153,7 @@ export const InsertFunctionOperation: ICommand = {
// TODO@DR-Univer: Maybe setTimeout can be removed
setTimeout(() => {
// edit cell
- formulaInputService.inputFormula(`=${value}(`);
+ formulaInputService.inputFormula(`=${value}(${editFormulaRangeString}`);
}, 0);
}
@@ -214,6 +234,22 @@ function findStartColumn(cellMatrix: ObjectMatrix<Nullable<ICellData>>, row: num
return column;
}
-function isNumberCell(cell: Nullable<ICellData>) {
- return cell && (Tools.isStringNumber(cell.v) || getCellValueType(cell) === CellValueType.NUMBER);
+export function isNumberCell(cell: Nullable<ICellData>) {
+ return cell && (cell.t === CellValueType.NUMBER || getCellValueType(cell) === CellValueType.NUMBER);
+}
+
+/**
+ * Check if a single cell
+ * @param range
+ */
+export function isSingleCell(range: IRange) {
+ return range.startRow === range.endRow && range.startColumn === range.endColumn;
+}
+
+/**
+ * Check if there is a multi-row, multi-column range
+ * @param range
+ */
+export function isMultiRowsColumnsRange(range: IRange) {
+ return range.startRow !== range.endRow && range.startColumn !== range.endColumn;
}