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.

test(formula): test case for sumif (#702)

Dushusir9a17d7257b74
diff --git a/packages/engine-formula/src/functions/__tests__/create-command-test-bed.ts b/packages/engine-formula/src/functions/__tests__/create-command-test-bed.ts
index 4e41a5444e..83652818f2 100644
--- a/packages/engine-formula/src/functions/__tests__/create-command-test-bed.ts
+++ b/packages/engine-formula/src/functions/__tests__/create-command-test-bed.ts
@@ -1,8 +1,18 @@
import type { IWorkbookData } from '@univerjs/core';
-import { ILogService, IUniverInstanceService, LocaleType, LogLevel, Plugin, PluginType, Univer } from '@univerjs/core';
+import {
+ ILogService,
+ IUniverInstanceService,
+ LocaleType,
+ LogLevel,
+ ObjectMatrix,
+ Plugin,
+ PluginType,
+ Univer,
+} from '@univerjs/core';
import type { Dependency } from '@wendellhu/redi';
import { Inject, Injector } from '@wendellhu/redi';
+import type { ISheetData } from '../../basics/common';
import { LexerTreeBuilder } from '../../engine/analysis/lexer';
import { AstTreeBuilder } from '../../engine/analysis/parser';
import { AstRootNodeFactory } from '../../engine/ast-node/ast-root-node';
@@ -36,21 +46,6 @@ const TEST_WORKBOOK_DATA: IWorkbookData = {
v: 1,
},
},
- 1: {
- 0: {
- v: 4,
- },
- },
- 2: {
- 0: {
- v: 44,
- },
- },
- 3: {
- 0: {
- v: 444,
- },
- },
},
},
},
@@ -131,9 +126,25 @@ export function createCommandTestBed(workbookConfig?: IWorkbookData, dependencie
const logService = get(ILogService);
logService.setLogLevel(LogLevel.SILENT); // change this to `true` to debug tests via logs
+ const sheetData: ISheetData = {};
+ const workbook = univerInstanceService.getCurrentUniverSheetInstance();
+ const unitId = workbook.getUnitId();
+ const sheetId = workbook.getActiveSheet().getSheetId();
+ workbook.getSheets().forEach((sheet) => {
+ const sheetConfig = sheet.getConfig();
+ sheetData[sheet.getSheetId()] = {
+ cellData: new ObjectMatrix(sheetConfig.cellData),
+ rowCount: sheetConfig.rowCount,
+ columnCount: sheetConfig.columnCount,
+ };
+ });
+
return {
univer,
get,
sheet,
+ unitId,
+ sheetId,
+ sheetData,
};
}
diff --git a/packages/engine-formula/src/functions/math/sumif/__tests__/sumif.spec.ts b/packages/engine-formula/src/functions/math/sumif/__tests__/sumif.spec.ts
index 8c81f58e98..0a9c53b2fe 100644
--- a/packages/engine-formula/src/functions/math/sumif/__tests__/sumif.spec.ts
+++ b/packages/engine-formula/src/functions/math/sumif/__tests__/sumif.spec.ts
@@ -1,43 +1,76 @@
-import type { ICellData, Nullable, Univer } from '@univerjs/core';
-import { ICommandService, IUniverInstanceService } from '@univerjs/core';
+import type { IWorkbookData, Univer } from '@univerjs/core';
+import { LocaleType } from '@univerjs/core';
import type { Injector } from '@wendellhu/redi';
-import { afterEach, beforeEach, describe, it } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it } from 'vitest';
-import type { LexerTreeBuilder } from '../../../../engine/analysis/lexer';
-import type { CalculateFormulaService } from '../../../../services/calculate-formula.service';
+import type { ISheetData } from '../../../../basics/common';
+import { RangeReferenceObject } from '../../../../engine/reference-object/range-reference-object';
+import { ValueObjectFactory } from '../../../../engine/value-object/array-value-object';
+import type { BaseValueObject } from '../../../../engine/value-object/base-value-object';
+import { IFunctionService } from '../../../../services/function.service';
import { createCommandTestBed } from '../../../__tests__/create-command-test-bed';
+import { FUNCTION_NAMES_MATH } from '../../function-names';
+import { Sumif } from '../sumif';
+
+const TEST_WORKBOOK_DATA: IWorkbookData = {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ cellData: {
+ 0: {
+ 0: {
+ v: 1,
+ },
+ },
+ 1: {
+ 0: {
+ v: 4,
+ },
+ },
+ 2: {
+ 0: {
+ v: 44,
+ },
+ },
+ 3: {
+ 0: {
+ v: 444,
+ },
+ },
+ },
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ styles: {},
+};
describe('test sumif', () => {
let univer: Univer;
+ let injector: Injector;
let get: Injector['get'];
- let commandService: ICommandService;
- let calculateFormulaService: CalculateFormulaService;
- let lexerTreeBuilder: LexerTreeBuilder;
- let getValue: (
- startRow: number,
- startColumn: number,
- endRow: number,
- endColumn: number
- ) => Nullable<ICellData> | undefined;
+ let functionService: IFunctionService;
+ let unitId: string;
+ let sheetId: string;
+ let sheetData: ISheetData = {};
beforeEach(() => {
- const testBed = createCommandTestBed();
+ const testBed = createCommandTestBed(TEST_WORKBOOK_DATA);
univer = testBed.univer;
+ injector = univer.__getInjector();
get = testBed.get;
+ unitId = testBed.unitId;
+ sheetId = testBed.sheetId;
+ sheetData = testBed.sheetData;
- commandService = get(ICommandService);
-
- getValue = (
- startRow: number,
- startColumn: number,
- endRow: number,
- endColumn: number
- ): Nullable<ICellData> | undefined =>
- get(IUniverInstanceService)
- .getUniverSheetInstance('test')
- ?.getSheetBySheetId('sheet1')
- ?.getRange(startRow, startColumn, endRow, endColumn)
- .getValue();
+ functionService = get(IFunctionService);
+
+ // register sumif
+ const sumif = new Sumif(injector, FUNCTION_NAMES_MATH.SUMIF);
+ functionService.registerExecutors(sumif);
});
afterEach(() => {
@@ -45,10 +78,26 @@ describe('test sumif', () => {
});
it('range and criteria', async () => {
- const params = {
- value: {
- f: '=SUMIF(A1:A4, ">40")',
- },
+ // range A1:A4
+ const range = {
+ startRow: 0,
+ startColumn: 0,
+ endRow: 3,
+ endColumn: 0,
};
+
+ const rangeRef = new RangeReferenceObject(range, sheetId, unitId);
+ rangeRef.setUnitData({
+ [unitId]: sheetData,
+ });
+
+ // criteria >40
+ const criteriaRef = ValueObjectFactory.create('>40');
+
+ // calculate
+ const executor = functionService.getExecutor(FUNCTION_NAMES_MATH.SUMIF);
+ const resultObject = executor?.calculate(rangeRef, criteriaRef) as BaseValueObject;
+ const value = resultObject?.getValue();
+ expect(value).toBe(488);
});
});
diff --git a/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx b/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx
index 2d4c307f7c..7d781a0cbe 100644
--- a/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx
+++ b/packages/sheets-formula/src/views/prompt/help-function/HelpFunction.tsx
@@ -108,7 +108,10 @@ export function HelpFunction() {
<div
className={styles.formulaHelpFunctionContent}
- style={{ height: contentVisible ? 'unset' : 0 }}
+ style={{
+ height: contentVisible ? 'unset' : 0,
+ padding: contentVisible ? 'revert-layer' : 0,
+ }}
>
<div className={styles.formulaHelpFunctionContentInner}>
<Params
diff --git a/packages/sheets-formula/src/views/prompt/help-function/index.module.less b/packages/sheets-formula/src/views/prompt/help-function/index.module.less
index 0e740038f9..889dd43d35 100644
--- a/packages/sheets-formula/src/views/prompt/help-function/index.module.less
+++ b/packages/sheets-formula/src/views/prompt/help-function/index.module.less
@@ -1,5 +1,6 @@
-@width: 340px;
-@max-height: 300px;
+@width: 250px;
+@max-height: 350px;
+@icon-size: 24px;
.formula-help-function {
user-select: none;
@@ -23,7 +24,7 @@
justify-content: space-between;
box-sizing: border-box;
- padding: var(--padding-base) var(--padding-xl);
+ padding: var(--padding-base) var(--padding-lg);
font-size: var(--font-size-xs);
font-weight: 500;
@@ -42,14 +43,16 @@
align-items: center;
justify-content: center;
- box-sizing: border-box;
- width: fit-content;
- height: fit-content;
- margin-left: var(--margin-sm);
- padding: var(--padding-base) var(--padding-xs);
+ width: @icon-size;
+ height: @icon-size;
+ margin-left: var(--margin-xs);
+ padding: 0;
+ font-size: var(--font-size-lg);
color: rgb(var(--text-color-secondary));
+ background-color: transparent;
+ border: none;
border-radius: var(--border-radius-base);
outline: none;
@@ -66,10 +69,10 @@
overflow-y: auto;
box-sizing: border-box;
max-height: @max-height;
- padding: 0 var(--padding-xl) var(--padding-xl);
+ padding: 0 var(--padding-lg) var(--padding-base) var(--padding-lg);
&-inner {
- margin-top: var(--padding-xl);
+ margin-top: var(--margin-sm);
}
&-params {
diff --git a/packages/sheets-formula/src/views/prompt/search-function/SearchFunction.tsx b/packages/sheets-formula/src/views/prompt/search-function/SearchFunction.tsx
index 58942579a9..0a5ba02591 100644
--- a/packages/sheets-formula/src/views/prompt/search-function/SearchFunction.tsx
+++ b/packages/sheets-formula/src/views/prompt/search-function/SearchFunction.tsx
@@ -49,6 +49,7 @@ export function SearchFunction() {
setOffset([left, top + height]);
setVisible(visible);
setActive(0); // Reset active state
+ updatedActive = 0;
});
const subscribeNavigate = promptService.navigate$.subscribe((params: INavigateParam) => {
diff --git a/packages/sheets-formula/src/views/prompt/search-function/index.module.less b/packages/sheets-formula/src/views/prompt/search-function/index.module.less
index b73f215255..374341f35c 100644
--- a/packages/sheets-formula/src/views/prompt/search-function/index.module.less
+++ b/packages/sheets-formula/src/views/prompt/search-function/index.module.less
@@ -1,10 +1,15 @@
-@width: 340px;
+@width: 250px;
+@max-height: 400px;
+
.formula-search-function {
+ overflow-y: auto;
+
box-sizing: border-box;
width: @width;
+ max-height: @max-height;
margin: 0;
- padding: var(--padding-base);
+ padding: var(--padding-sm);
line-height: 20px;
list-style: none;