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(formula): add support for AGGREGATE formula (#6177)

wpxp123456d0e1838f4cee
diff --git a/packages/engine-formula/src/basics/statistical.ts b/packages/engine-formula/src/basics/statistical.ts
index 318de2fc7b..8a81ee0e48 100644
--- a/packages/engine-formula/src/basics/statistical.ts
+++ b/packages/engine-formula/src/basics/statistical.ts
@@ -14,10 +14,17 @@
* limitations under the License.
*/
+import type { ICellData, IObjectArrayPrimitiveType, IRowData, Nullable, ObjectMatrix } from '@univerjs/core';
+import type { BaseReferenceObject, FunctionVariantType } from '../engine/reference-object/base-reference-object';
+import type { MultiAreaReferenceObject } from '../engine/reference-object/multi-area-reference-object';
import type { ArrayValueObject } from '../engine/value-object/array-value-object';
import type { BaseValueObject } from '../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
+import type { FormulaDataModel } from '../models/formula-data.model';
+import { BooleanNumber, isRealNum } from '@univerjs/core';
+import { createNewArray } from '../engine/utils/array-object';
import { ErrorValueObject } from '../engine/value-object/base-value-object';
+import { NumberValueObject } from '../engine/value-object/primitive-object';
+import { FUNCTION_NAMES_MATH } from '../functions/math/function-names';
import { erf, erfcINV } from './engineering';
import { ErrorType } from './error-type';
import { calculateCombin, calculateFactorial, calculateMmult, inverseMatrixByLUD, inverseMatrixByUSV, matrixTranspose } from './math';
@@ -1074,3 +1081,505 @@ export function getKnownsArrayCoefficients(knownYsValues: number[][], knownXsVal
XTXInverse,
};
}
+
+//#region Aggregate functions
+
+/**
+ * Parse aggregate data references into multi-area refs and normal refs, currently only used in functions like SUBTOTAL and AGGREGATE.
+ * If there is any invalid reference, return isError as true.
+ */
+export function parseAggregateDataRefs(refs: FunctionVariantType[]) {
+ const multiAreaRefs: MultiAreaReferenceObject[] = [];
+ const normalRefs: BaseReferenceObject[] = [];
+
+ let isError = false;
+
+ for (let i = 0; i < refs.length; i++) {
+ const ref = refs[i];
+
+ if (ref.isReferenceObject()) {
+ if ((ref as BaseReferenceObject).isMultiArea()) {
+ multiAreaRefs.push(ref as MultiAreaReferenceObject);
+ } else {
+ normalRefs.push(ref as BaseReferenceObject);
+ }
+ } else {
+ isError = true;
+ break;
+ }
+ }
+
+ return {
+ isError,
+ multiAreaRefs,
+ normalRefs,
+ };
+}
+
+export enum AggregateFunctionType {
+ AVERAGE = 'AVERAGE',
+ COUNT = 'COUNT',
+ COUNTA = 'COUNTA',
+ MAX = 'MAX',
+ MIN = 'MIN',
+ PRODUCT = 'PRODUCT',
+ STDEV = 'STDEV',
+ STDEV_S = 'STDEV.S',
+ STDEVP = 'STDEVP',
+ STDEV_P = 'STDEV.P',
+ SUM = 'SUM',
+ VAR = 'VAR',
+ VAR_S = 'VAR.S',
+ VARP = 'VARP',
+ VAR_P = 'VAR.P',
+ MEDIAN = 'MEDIAN',
+ MODE_SNGL = 'MODE.SNGL',
+ // LARGE = 'LARGE',
+ // SMALL = 'SMALL',
+ // PERCENTILE_EXC = 'PERCENTILE.EXC',
+ // PERCENTILE_INC = 'PERCENTILE.INC',
+ // QUARTILE_EXC = 'QUARTILE.EXC',
+ // QUARTILE_INC = 'QUARTILE.INC',
+}
+
+// Check if the row is hidden
+function isRowHidden(rowData: IObjectArrayPrimitiveType<Partial<IRowData>>, rowIndex: number): boolean {
+ const row = rowData[rowIndex];
+ if (!row) {
+ return false;
+ }
+ return row.hd === BooleanNumber.TRUE;
+}
+
+// Check if the cell is a nested SUBTOTAL or AGGREGATE result
+function isNestedAggregateOrSubtotal(
+ cellData: ObjectMatrix<ICellData>,
+ rowIndex: number,
+ columnIndex: number,
+ sheetId: string,
+ unitId: string,
+ formulaDataModel: FormulaDataModel
+): boolean {
+ const cellValue = cellData.getValue(rowIndex, columnIndex);
+ if (cellValue?.f || cellValue?.si) {
+ const formulaString = formulaDataModel.getFormulaStringByCell(rowIndex, columnIndex, sheetId, unitId);
+ // match 'SUBTOTAL(' or 'AGGREGATE(' for simple check
+ if (formulaString && (formulaString.indexOf(`${FUNCTION_NAMES_MATH.SUBTOTAL}(`) > -1 || formulaString.indexOf(`${FUNCTION_NAMES_MATH.AGGREGATE}(`) > -1)) {
+ return true;
+ }
+ }
+ return false;
+}
+
+export type modeSnglValueCountMapType = Record<number, {
+ count: number;
+ order: number;
+}>;
+
+export function getModeSnglResult(valueCountMap: modeSnglValueCountMapType, valueMaxCount: number): BaseValueObject {
+ const result = Object.entries(valueCountMap)
+ .filter(([_, { count }]) => count === valueMaxCount)
+ .sort((a, b) => a[1].order - b[1].order)
+ .map(([value]) => +value);
+
+ return NumberValueObject.create(result[0]);
+}
+
+export interface IAggregateIgnoreOptions {
+ ignoreRowHidden: boolean; // whether to ignore hidden rows
+ ignoreErrorValues: boolean; // whether to ignore error values
+ ignoreNested: boolean; // whether to ignore nested SUBTOTAL or AGGREGATE results
+}
+
+// eslint-disable-next-line max-lines-per-function,complexity
+export function getAggregateResult(
+ options: {
+ type: AggregateFunctionType; // type of aggregate function
+ formulaDataModel: FormulaDataModel;
+ } & IAggregateIgnoreOptions,
+ refs: BaseReferenceObject[]
+): BaseValueObject {
+ const { type, ignoreRowHidden, ignoreErrorValues, ignoreNested, formulaDataModel } = options;
+ const valueObjects: BaseValueObject[] = [];
+
+ let count = 0;
+ let counta = 0;
+ let sum = 0;
+ let n = 0;
+ let max = 0;
+ let min = 0;
+ let product = 1;
+
+ const valueCountMap: modeSnglValueCountMapType = {};
+ let valueMaxCount = 1;
+ let valueCountOrder = 0;
+
+ for (let i = 0; i < refs.length; i++) {
+ const ref = refs[i];
+
+ const filteredOutRows = ref.getFilteredOutRows();
+ const rowData = ref.getRowData();
+ const unitId = ref.getUnitId();
+ const sheetId = ref.getSheetId();
+ const unitData = ref.getUnitData();
+ const cellData = unitData[unitId]?.[sheetId]?.cellData;
+
+ let errorValueObject: Nullable<BaseValueObject>;
+
+ ref.iterator((valueObject, rowIndex, columnIndex) => {
+ // Filtered rows are always excluded.
+ if (filteredOutRows.includes(rowIndex)) {
+ return true; // continue
+ }
+
+ if (ignoreRowHidden && isRowHidden(rowData, rowIndex)) {
+ return true; // continue
+ }
+
+ if (ignoreNested && isNestedAggregateOrSubtotal(cellData, rowIndex, columnIndex, sheetId, unitId, formulaDataModel)) {
+ return true; // continue
+ }
+
+ // COUNT function only counts number cells.
+ if (type === AggregateFunctionType.COUNT) {
+ if (valueObject?.isNumber()) count++;
+ return true; // continue
+ }
+
+ // COUNTA function counts all non-blank cells.
+ if (type === AggregateFunctionType.COUNTA) {
+ if (valueObject !== null && valueObject !== undefined && !valueObject.isNull()) counta++;
+ return true; // continue
+ }
+
+ if (valueObject?.isError()) {
+ if (ignoreErrorValues) return true; // continue
+ // If the value is error, return directly.
+ errorValueObject = valueObject;
+ return false; // break
+ }
+
+ // 'test', ' ', blank cell, TRUE and FALSE are ignored
+ if (!valueObject || valueObject.isNull() || valueObject.isBoolean() || valueObject.isString()) {
+ return true; // continue
+ }
+
+ let value = valueObject.getValue();
+
+ if (!isRealNum(value)) {
+ return true; // continue
+ }
+
+ value = +value;
+ n++;
+
+ if (type === AggregateFunctionType.MAX) {
+ max = n === 1 ? value : Math.max(max, value);
+ return true; // continue
+ }
+
+ if (type === AggregateFunctionType.MIN) {
+ min = n === 1 ? value : Math.min(min, value);
+ return true; // continue
+ }
+
+ if (type === AggregateFunctionType.MODE_SNGL) {
+ if (!valueCountMap[value]) {
+ valueCountMap[value] = { count: 1, order: valueCountOrder++ };
+ } else {
+ valueCountMap[value].count++;
+ if (valueCountMap[value].count > valueMaxCount) {
+ valueMaxCount = valueCountMap[value].count;
+ }
+ }
+ return true; // continue
+ }
+
+ sum += value;
+ product *= value;
+ valueObjects.push(valueObject);
+ });
+
+ if (errorValueObject?.isError()) {
+ return errorValueObject;
+ }
+ }
+
+ switch (type) {
+ case AggregateFunctionType.AVERAGE:
+ if (n === 0) return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
+ return NumberValueObject.create(sum / n);
+ case AggregateFunctionType.COUNT:
+ return NumberValueObject.create(count);
+ case AggregateFunctionType.COUNTA:
+ return NumberValueObject.create(counta);
+ case AggregateFunctionType.MAX:
+ return NumberValueObject.create(max);
+ case AggregateFunctionType.MIN:
+ return NumberValueObject.create(min);
+ case AggregateFunctionType.PRODUCT:
+ return NumberValueObject.create(n === 0 ? 0 : product);
+ case AggregateFunctionType.STDEV:
+ case AggregateFunctionType.STDEV_S:
+ if (n < 2) return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
+ return createNewArray([valueObjects], 1, n).std(1);
+ case AggregateFunctionType.STDEVP:
+ case AggregateFunctionType.STDEV_P:
+ if (n === 0) return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
+ return createNewArray([valueObjects], 1, n).std();
+ case AggregateFunctionType.SUM:
+ return NumberValueObject.create(sum);
+ case AggregateFunctionType.VAR:
+ case AggregateFunctionType.VAR_S:
+ if (n < 2) return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
+ return createNewArray([valueObjects], 1, n).var(1);
+ case AggregateFunctionType.VARP:
+ case AggregateFunctionType.VAR_P:
+ if (n === 0) return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
+ return createNewArray([valueObjects], 1, n).var();
+ case AggregateFunctionType.MEDIAN:
+ if (n === 0) return ErrorValueObject.create(ErrorType.NUM);
+ return getMedianResult(valueObjects.map((vo) => +vo.getValue()));
+ case AggregateFunctionType.MODE_SNGL:
+ if (valueCountOrder === 0 || valueMaxCount === 1) return ErrorValueObject.create(ErrorType.NA);
+ return getModeSnglResult(valueCountMap, valueMaxCount);
+ default:
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+}
+
+// eslint-disable-next-line max-lines-per-function,complexity
+export function getArrayValuesByAggregateIgnoreOptions(
+ array: FunctionVariantType,
+ options?: IAggregateIgnoreOptions,
+ formulaDataModel?: FormulaDataModel
+): number[] | ErrorValueObject {
+ const { ignoreRowHidden = false, ignoreErrorValues = false, ignoreNested = false } = options ?? {};
+ const values: number[] = [];
+
+ if (array.isReferenceObject()) {
+ const filteredOutRows = (array as BaseReferenceObject).getFilteredOutRows();
+ const rowData = (array as BaseReferenceObject).getRowData();
+ const unitId = (array as BaseReferenceObject).getUnitId();
+ const sheetId = (array as BaseReferenceObject).getSheetId();
+ const unitData = (array as BaseReferenceObject).getUnitData();
+ const cellData = unitData[unitId]?.[sheetId]?.cellData;
+
+ let errorValueObject: Nullable<BaseValueObject>;
+
+ (array as BaseReferenceObject).iterator((valueObject, rowIndex, columnIndex) => {
+ // Filtered rows are always excluded.
+ if (filteredOutRows.includes(rowIndex)) {
+ return true; // continue
+ }
+
+ if (ignoreRowHidden && isRowHidden(rowData, rowIndex)) {
+ return true; // continue
+ }
+
+ if (valueObject?.isError()) {
+ if (ignoreErrorValues) return true; // continue
+ // If the value is error, return directly.
+ errorValueObject = valueObject;
+ return false; // break
+ }
+
+ if (ignoreNested && formulaDataModel && isNestedAggregateOrSubtotal(cellData, rowIndex, columnIndex, sheetId, unitId, formulaDataModel)) {
+ return true; // continue
+ }
+
+ if (!valueObject || valueObject.isNull() || valueObject.isBoolean() || valueObject.isString()) {
+ return true; // continue
+ }
+
+ const value = valueObject.getValue();
+
+ if (!isRealNum(value)) {
+ return true; // continue
+ }
+
+ values.push(+value);
+ });
+
+ if (errorValueObject) {
+ return errorValueObject as ErrorValueObject;
+ }
+ } else {
+ const rowCount = array.isArray() ? (array as ArrayValueObject).getRowCount() : 1;
+ const columnCount = array.isArray() ? (array as ArrayValueObject).getColumnCount() : 1;
+
+ for (let r = 0; r < rowCount; r++) {
+ for (let c = 0; c < columnCount; c++) {
+ const valueObject = (array.isArray() ? (array as ArrayValueObject).get(r, c) : array) as BaseValueObject;
+
+ if (valueObject.isError()) {
+ if (ignoreErrorValues) continue;
+ return valueObject as ErrorValueObject;
+ }
+
+ if (!valueObject || valueObject.isNull() || valueObject.isBoolean() || valueObject.isString()) {
+ continue;
+ }
+
+ const value = valueObject.getValue();
+
+ if (!isRealNum(value)) {
+ continue;
+ }
+
+ values.push(+value);
+ }
+ }
+ }
+
+ if (values.length === 0) {
+ return ErrorValueObject.create(ErrorType.NUM);
+ }
+
+ return values;
+}
+
+// MEDIAN(array): returns the median of the given numbers.
+export function getMedianResult(array: number[]): BaseValueObject {
+ const n = array.length;
+
+ array.sort((a, b) => a - b);
+
+ let result: number;
+
+ if (n % 2 === 0) {
+ const mid = n / 2;
+ result = (array[mid - 1] + array[mid]) / 2;
+ } else {
+ result = array[Math.floor(n / 2)];
+ }
+
+ return NumberValueObject.create(result);
+}
+
+// LARGE(array, k): returns the k-th largest value in a data set.
+export function getLargeResult(array: number[], k: number): BaseValueObject {
+ if (k < 1 || k > array.length) {
+ return ErrorValueObject.create(ErrorType.NUM);
+ }
+
+ array.sort((a, b) => b - a);
+
+ const _k = Math.ceil(k);
+
+ return NumberValueObject.create(array[_k - 1]);
+}
+
+// SMALL(array, k): returns the k-th smallest value in a data set.
+export function getSmallResult(array: number[], k: number): BaseValueObject {
+ if (k < 1 || k > array.length) {
+ return ErrorValueObject.create(ErrorType.NUM);
+ }
+
+ array.sort((a, b) => a - b);
+
+ const _k = Math.floor(k);
+
+ return NumberValueObject.create(array[_k - 1]);
+}
+
+// PERCENTILE.INC(array, k): returns the k-th percentile of values in a range, where k is in the range 0..1, inclusive.
+export function getPercentileIncResult(array: number[], k: number): BaseValueObject {
+ const n = array.length;
+
+ if (k < 0 || k > 1) {
+ return ErrorValueObject.create(ErrorType.NUM);
+ }
+
+ array.sort((a, b) => a - b);
+
+ const kIndex = k * (n - 1);
+ const integerPart = Math.floor(kIndex);
+ const fractionPart = kIndex - integerPart;
+
+ if (fractionPart === 0) {
+ return NumberValueObject.create(array[integerPart]);
+ }
+
+ const result = array[integerPart] + fractionPart * (array[integerPart + 1] - array[integerPart]);
+
+ return NumberValueObject.create(result);
+}
+
+// PERCENTILE.EXC(array, k): returns the k-th percentile of values in a range, where k is in the range 0..1, exclusive.
+export function getPercentileExcResult(array: number[], k: number): BaseValueObject {
+ const n = array.length;
+
+ if (k < 1 / (n + 1) || k > 1 - 1 / (n + 1)) {
+ return ErrorValueObject.create(ErrorType.NUM);
+ }
+
+ array.sort((a, b) => a - b);
+
+ const kIndex = k * (n + 1) - 1;
+ const integerPart = Math.floor(kIndex);
+ const fractionPart = kIndex - integerPart;
+
+ if (fractionPart === 0) {
+ return NumberValueObject.create(array[integerPart]);
+ }
+
+ const result = array[integerPart] + fractionPart * (array[integerPart + 1] - array[integerPart]);
+
+ return NumberValueObject.create(result);
+}
+
+// QUARTILE.INC(array, quart): returns the quartile of a data set, based on percentile values from 0..1, inclusive.
+export function getQuartileIncResult(array: number[], quart: number): BaseValueObject {
+ const n = array.length;
+
+ if (quart < 0 || quart > 4) {
+ return ErrorValueObject.create(ErrorType.NUM);
+ }
+
+ array.sort((a, b) => a - b);
+
+ const k = quart / 4;
+ const kIndex = k * (n - 1);
+ const integerPart = Math.floor(kIndex);
+ const fractionPart = kIndex - integerPart;
+
+ if (fractionPart === 0) {
+ return NumberValueObject.create(array[integerPart]);
+ }
+
+ const result = array[integerPart] + fractionPart * (array[integerPart + 1] - array[integerPart]);
+
+ return NumberValueObject.create(result);
+}
+
+// QUARTILE.EXC(array, quart): returns the quartile of a data set, based on percentile values from 0..1, exclusive.
+export function getQuartileExcResult(array: number[], quart: number): BaseValueObject {
+ const n = array.length;
+
+ if (quart <= 0 || quart >= 4) {
+ return ErrorValueObject.create(ErrorType.NUM);
+ }
+
+ array.sort((a, b) => a - b);
+
+ const k = quart / 4;
+
+ if (k < 1 / (n + 1) || k > 1 - 1 / (n + 1)) {
+ return ErrorValueObject.create(ErrorType.NUM);
+ }
+
+ const kIndex = k * (n + 1) - 1;
+ const integerPart = Math.floor(kIndex);
+ const fractionPart = kIndex - integerPart;
+
+ if (fractionPart === 0) {
+ return NumberValueObject.create(array[integerPart]);
+ }
+
+ const result = array[integerPart] + fractionPart * (array[integerPart + 1] - array[integerPart]);
+
+ return NumberValueObject.create(result);
+}
+
+//#endregion
diff --git a/packages/engine-formula/src/engine/value-object/array-value-object.ts b/packages/engine-formula/src/engine/value-object/array-value-object.ts
index 05097fae4b..9ad944eacc 100644
--- a/packages/engine-formula/src/engine/value-object/array-value-object.ts
+++ b/packages/engine-formula/src/engine/value-object/array-value-object.ts
@@ -1200,6 +1200,11 @@ export class ArrayValueObject extends BaseValueObject {
return sum.divided(ddof === 0 ? count : count.minusBy(1));
}
+ /**
+ * TODO: @DR-Univer
+ * This calculation method contains an error, please note.
+ * Please refer `getMedianResult` function in /engine-formula/src/basics/statistical.ts
+ */
override median(): BaseValueObject {
const numberArray = this.flattenPosition().numberArray;
diff --git a/packages/engine-formula/src/functions/math/aggregate/__tests__/index.spec.ts b/packages/engine-formula/src/functions/math/aggregate/__tests__/index.spec.ts
new file mode 100644
index 0000000000..7f1cf88a54
--- /dev/null
+++ b/packages/engine-formula/src/functions/math/aggregate/__tests__/index.spec.ts
@@ -0,0 +1,427 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 { Injector, IWorkbookData } from '@univerjs/core';
+import type { LexerNode } from '../../../../engine/analysis/lexer-node';
+import type { BaseAstNode } from '../../../../engine/ast-node/base-ast-node';
+import type { BaseValueObject } from '../../../../engine/value-object/base-value-object';
+import { BooleanNumber, CellValueType, LocaleType } from '@univerjs/core';
+import { beforeEach, describe, expect, it } from 'vitest';
+import { ErrorType } from '../../../../basics/error-type';
+import { Lexer } from '../../../../engine/analysis/lexer';
+import { AstTreeBuilder } from '../../../../engine/analysis/parser';
+import { Interpreter } from '../../../../engine/interpreter/interpreter';
+import { generateExecuteAstNodeData } from '../../../../engine/utils/ast-node-tool';
+import { IFormulaCurrentConfigService } from '../../../../services/current-data.service';
+import { IFunctionService } from '../../../../services/function.service';
+import { IFormulaRuntimeService } from '../../../../services/runtime.service';
+import { createFunctionTestBed, getObjectValue } from '../../../__tests__/create-function-test-bed';
+import { FUNCTION_NAMES_MATH } from '../../function-names';
+import { Aggregate } from '../index';
+
+const getTestWorkbookData = (): IWorkbookData => {
+ return {
+ id: 'test',
+ appVersion: '3.0.0-alpha',
+ sheets: {
+ sheet1: {
+ id: 'sheet1',
+ cellData: {
+ 0: {
+ 0: {
+ v: 1,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: 2,
+ t: CellValueType.NUMBER,
+ },
+ 2: {
+ v: ErrorType.NAME,
+ t: CellValueType.STRING,
+ },
+ },
+ 1: {
+ 0: {
+ v: 3,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: 4,
+ t: CellValueType.NUMBER,
+ },
+ 2: {
+ v: 'B2',
+ t: CellValueType.STRING,
+ },
+ 3: {
+ v: 'R2C2',
+ t: CellValueType.STRING,
+ },
+ },
+ 2: {
+ 0: {
+ v: 1,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: ' ',
+ t: CellValueType.STRING,
+ },
+ 2: {
+ v: 1.23,
+ t: CellValueType.NUMBER,
+ },
+ 3: {
+ v: true,
+ t: CellValueType.BOOLEAN,
+ },
+ 4: {
+ v: false,
+ t: CellValueType.BOOLEAN,
+ },
+ },
+ 3: {
+ 0: {
+ v: 0,
+ t: CellValueType.NUMBER,
+ },
+ 1: {
+ v: '100',
+ },
+ 2: {
+ v: '2.34',
+ },
+ 3: {
+ v: 'test',
+ t: CellValueType.STRING,
+ },
+ 4: {
+ v: -3,
+ t: CellValueType.NUMBER,
+ },
+ },
+ },
+ rowData: {
+ 3: {
+ hd: BooleanNumber.TRUE,
+ },
+ },
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ styles: {},
+ };
+};
+
+describe('Test aggregate', () => {
+ let get: Injector['get'];
+ let lexer: Lexer;
+ let astTreeBuilder: AstTreeBuilder;
+ let interpreter: Interpreter;
+ let calculate: (formula: string) => (string | number | boolean | null)[][] | string | number | boolean;
+
+ beforeEach(() => {
+ const testBed = createFunctionTestBed(getTestWorkbookData());
+
+ get = testBed.get;
+
+ lexer = get(Lexer);
+ astTreeBuilder = get(AstTreeBuilder);
+ interpreter = get(Interpreter);
+
+ const functionService = get(IFunctionService);
+
+ const formulaCurrentConfigService = get(IFormulaCurrentConfigService);
+
+ const formulaRuntimeService = get(IFormulaRuntimeService);
+
+ formulaCurrentConfigService.load({
+ formulaData: {},
+ arrayFormulaCellData: {},
+ arrayFormulaRange: {},
+ forceCalculate: false,
+ dirtyRanges: [],
+ dirtyNameMap: {},
+ dirtyDefinedNameMap: {},
+ dirtyUnitFeatureMap: {},
+ excludedCell: {},
+ allUnitData: {
+ [testBed.unitId]: testBed.sheetData,
+ },
+ dirtyUnitOtherFormulaMap: {},
+ });
+
+ const sheetItem = testBed.sheetData[testBed.sheetId];
+
+ formulaRuntimeService.setCurrent(
+ 0,
+ 0,
+ sheetItem.rowCount,
+ sheetItem.columnCount,
+ testBed.sheetId,
+ testBed.unitId
+ );
+
+ functionService.registerExecutors(
+ new Aggregate(FUNCTION_NAMES_MATH.AGGREGATE)
+ );
+
+ calculate = (formula: string) => {
+ const lexerNode = lexer.treeBuilder(formula);
+
+ const astNode = astTreeBuilder.parse(lexerNode as LexerNode);
+
+ const result = interpreter.execute(generateExecuteAstNodeData(astNode as BaseAstNode));
+
+ return getObjectValue(result as BaseValueObject, true);
+ };
+ });
+
+ describe('Aggregate common', () => {
+ it('FunctionNum is normal number, refs is non-reference object', () => {
+ // number
+ let result = calculate('=AGGREGATE(1,,1)');
+ expect(result).toBe(ErrorType.VALUE);
+
+ // string normal
+ result = calculate('=AGGREGATE(2,,"test")');
+ expect(result).toBe(ErrorType.VALUE);
+
+ // string number
+ result = calculate('=AGGREGATE(3,,"1")');
+ expect(result).toBe(ErrorType.VALUE);
+
+ // boolean true
+ result = calculate('=AGGREGATE(4,,true)');
+ expect(result).toBe(ErrorType.VALUE);
+
+ // boolean false
+ result = calculate('=AGGREGATE(5,,false)');
+ expect(result).toBe(ErrorType.VALUE);
+ });
+
+ it('FunctionNum is normal number, refs is error or blank cell', () => {
+ // error
+ let result = calculate('=AGGREGATE(7,,C1)');
+ expect(result).toBe(ErrorType.NAME);
+
+ // null
+ result = calculate('=AGGREGATE(1,,D1)');
+ expect(result).toBe(ErrorType.DIV_BY_ZERO);
+
+ result = calculate('=AGGREGATE(2,,D1)');
+ expect(result).toBe(0);
+
+ result = calculate('=AGGREGATE(3,,D1)');
+ expect(result).toBe(0);
+
+ result = calculate('=AGGREGATE(4,,D1)');
+ expect(result).toBe(0);
+
+ result = calculate('=AGGREGATE(5,,D1)');
+ expect(result).toBe(0);
+
+ result = calculate('=AGGREGATE(6,,D1)');
+ expect(result).toBe(0);
+
+ result = calculate('=AGGREGATE(7,,D1)');
+ expect(result).toBe(ErrorType.DIV_BY_ZERO);
+
+ result = calculate('=AGGREGATE(8,,D1)');
+ expect(result).toBe(ErrorType.DIV_BY_ZERO);
+
+ result = calculate('=AGGREGATE(9,,D1)');
+ expect(result).toBe(0);
+
+ result = calculate('=AGGREGATE(10,,D1)');
+ expect(result).toBe(ErrorType.DIV_BY_ZERO);
+
+ result = calculate('=AGGREGATE(11,,D1)');
+ expect(result).toBe(ErrorType.DIV_BY_ZERO);
+
+ result = calculate('=AGGREGATE(12,,D1)');
+ expect(result).toBe(ErrorType.NUM);
+
+ result = calculate('=AGGREGATE(13,,D1)');
+ expect(result).toBe(ErrorType.NA);
+ });
+
+ it('FunctionNum is normal number, ref1 is array, ref2 is array includes error', () => {
+ const result = calculate('=AGGREGATE(9,,A1:B2,C1:C2)');
+ expect(result).toBe(ErrorType.NAME);
+ });
+
+ it('FunctionNum ia array', () => {
+ const result = calculate('=AGGREGATE(A1:B2,,A1:B2)');
+ expect(result).toStrictEqual([
+ [2.5, 4],
+ [4, 4],
+ ]);
+ });
+ });
+
+ describe('Aggregate every function, function number is single number', () => {
+ it('Average, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(1,,A1:B2,A3:F4)');
+ expect(result).toBe(11.157);
+ });
+ it('Count, Var1 is array, var2 is array', () => {
+ let result = calculate('=AGGREGATE(2,,A1:B2,A3:F4)');
+ expect(result).toBe(10);
+ result = calculate('=AGGREGATE(2,,B1:C1)');
+ expect(result).toBe(1);
+ });
+ it('Counta, Var1 is array, var2 is array', () => {
+ let result = calculate('=AGGREGATE(3,,A1:B2,A3:F4)');
+ expect(result).toBe(14);
+ result = calculate('=AGGREGATE(3,,B1:C1)');
+ expect(result).toBe(2);
+ });
+ it('Max, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(4,,A1:B2,A3:F4)');
+ expect(result).toBe(100);
+ });
+ it('Min, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(5,,A1:B2,A3:F4)');
+ expect(result).toBe(-3);
+ });
+ it('Product, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(6,,A1:B2,A3:F4)');
+ expect(typeof result === 'number' && Math.abs(result)).toBe(0);
+ });
+ it('Stdev.s, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(7,,A1:B2,A3:F4)');
+ expect(result).toBe(31.273350405);
+ });
+ it('Stdev.p, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(8,,A1:B2,A3:F4)');
+ expect(result).toBe(29.6685052033);
+ });
+ it('sum, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(9,,A1:B2,A3:F4)');
+ expect(result).toBe(111.57);
+ });
+ it('Var.s, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(10,,A1:B2,A3:F4)');
+ expect(result).toBeCloseTo(978.0224456, 7);
+ });
+ it('Var.p, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(11,,A1:B2,A3:F4)');
+ expect(result).toBe(880.220201);
+ });
+ it('Median, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(12,,A1:B2,A3:F4)');
+ expect(result).toBe(1.615);
+ });
+ it('Mode.sngl, Var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(13,,A1:B2,A3:F4)');
+ expect(result).toBe(1);
+ });
+ });
+
+ describe('Aggregate every function, function number is array', () => {
+ it('Function num is array, var1 is array, var2 is array', () => {
+ const result = calculate('=AGGREGATE(A1:B2,,A1:B2,A3:F4)');
+ expect(result).toStrictEqual([
+ [11.157, 10],
+ [14, 100],
+ ]);
+ });
+ });
+
+ describe('Aggregate every function, including hidden row', () => {
+ it('Function num is normal number, var1 is array', () => {
+ let result = calculate('=AGGREGATE(1,5,A3:F4)');
+ expect(result).toBe(1.115);
+
+ result = calculate('=AGGREGATE(2,5,A3:F4)');
+ expect(result).toBe(2);
+
+ result = calculate('=AGGREGATE(3,5,A3:F4)');
+ expect(result).toBe(5);
+
+ result = calculate('=AGGREGATE(4,5,A3:F4)');
+ expect(result).toBe(1.23);
+
+ result = calculate('=AGGREGATE(5,5,A3:F4)');
+ expect(result).toBe(1);
+
+ result = calculate('=AGGREGATE(6,5,A3:F4)');
+ expect(result).toBe(1.23);
+
+ result = calculate('=AGGREGATE(7,5,A3:F4)');
+ expect(result).toBeCloseTo(0.16263456, 7);
+
+ result = calculate('=AGGREGATE(8,5,A3:F4)');
+ expect(result).toBe(0.115);
+
+ result = calculate('=AGGREGATE(9,5,A3:F4)');
+ expect(result).toBe(2.23);
+
+ result = calculate('=AGGREGATE(10,5,A3:F4)');
+ expect(result).toBe(0.02645);
+
+ result = calculate('=AGGREGATE(11,5,A3:F4)');
+ expect(result).toBe(0.013225);
+
+ result = calculate('=AGGREGATE(12,5,A3:F4)');
+ expect(result).toBe(1.115);
+
+ result = calculate('=AGGREGATE(13,5,A3:F4)');
+ expect(result).toBe(ErrorType.NA);
+ });
+ });
+
+ describe('Aggregate function num 14-19', () => {
+ it('Refs is not match array and k', () => {
+ const result = calculate('=AGGREGATE(14,,A1:B2,2,3)');
+ expect(result).toBe(ErrorType.VALUE);
+ });
+
+ it('Test LARGE function', () => {
+ const result = calculate('=AGGREGATE(14,,A1:B2,2)');
+ expect(result).toBe(3);
+ });
+
+ it('Test SMALL function', () => {
+ const result = calculate('=AGGREGATE(15,,A1:B2,2)');
+ expect(result).toBe(2);
+ });
+
+ it('Test PERCENTILE.INC function', () => {
+ const result = calculate('=AGGREGATE(16,,A1:B2,0.5)');
+ expect(result).toBe(2.5);
+ });
+
+ it('Test QUARTILE.INC function', () => {
+ const result = calculate('=AGGREGATE(17,,A1:B2,2)');
+ expect(result).toBe(2.5);
+ });
+
+ it('Test PERCENTILE.EXC function', () => {
+ const result = calculate('=AGGREGATE(18,,A1:B2,0.5)');
+ expect(result).toBe(2.5);
+ });
+
+ it('Test QUARTILE.EXC function', () => {
+ const result = calculate('=AGGREGATE(19,,A1:B2,2)');
+ expect(result).toBe(2.5);
+ });
+ });
+});
diff --git a/packages/engine-formula/src/functions/math/aggregate/index.ts b/packages/engine-formula/src/functions/math/aggregate/index.ts
new file mode 100644
index 0000000000..728d710040
--- /dev/null
+++ b/packages/engine-formula/src/functions/math/aggregate/index.ts
@@ -0,0 +1,300 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 { BaseReferenceObject, FunctionVariantType } from '../../../engine/reference-object/base-reference-object';
+import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
+import type { FormulaDataModel } from '../../../models/formula-data.model';
+import { ErrorType } from '../../../basics/error-type';
+import { AggregateFunctionType, getAggregateResult, getArrayValuesByAggregateIgnoreOptions, getLargeResult, getPercentileExcResult, getPercentileIncResult, getQuartileExcResult, getQuartileIncResult, getSmallResult, parseAggregateDataRefs } from '../../../basics/statistical';
+import { expandArrayValueObject } from '../../../engine/utils/array-object';
+import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { BaseFunction } from '../../base-function';
+
+const AggregateFunctionMap: Record<number, AggregateFunctionType> = {
+ 1: AggregateFunctionType.AVERAGE,
+ 2: AggregateFunctionType.COUNT,
+ 3: AggregateFunctionType.COUNTA,
+ 4: AggregateFunctionType.MAX,
+ 5: AggregateFunctionType.MIN,
+ 6: AggregateFunctionType.PRODUCT,
+ 7: AggregateFunctionType.STDEV_S,
+ 8: AggregateFunctionType.STDEV_P,
+ 9: AggregateFunctionType.SUM,
+ 10: AggregateFunctionType.VAR_S,
+ 11: AggregateFunctionType.VAR_P,
+ 12: AggregateFunctionType.MEDIAN,
+ 13: AggregateFunctionType.MODE_SNGL,
+};
+
+export class Aggregate extends BaseFunction {
+ override minParams = 3;
+
+ override maxParams = 255;
+
+ override needsReferenceObject = true;
+
+ override needsFilteredOutRows = true;
+
+ override needsFormulaDataModel = true;
+
+ override calculate(functionNum: FunctionVariantType, options: FunctionVariantType, ...refs: FunctionVariantType[]) {
+ const { isError, multiAreaRefs, normalRefs } = parseAggregateDataRefs(refs);
+
+ let _functionNum: BaseValueObject;
+
+ if (functionNum.isReferenceObject()) {
+ _functionNum = (functionNum as BaseReferenceObject).toArrayValueObject();
+ } else {
+ _functionNum = functionNum as BaseValueObject;
+ }
+
+ let _options: BaseValueObject;
+
+ if (options.isReferenceObject()) {
+ _options = (options as BaseReferenceObject).toArrayValueObject();
+ } else {
+ _options = options as BaseValueObject;
+ }
+
+ const maxRowLength = Math.max(
+ _functionNum.isArray() ? (_functionNum as ArrayValueObject).getRowCount() : 1,
+ _options.isArray() ? (_options as ArrayValueObject).getRowCount() : 1
+ );
+ const maxColumnLength = Math.max(
+ _functionNum.isArray() ? (_functionNum as ArrayValueObject).getColumnCount() : 1,
+ _options.isArray() ? (_options as ArrayValueObject).getColumnCount() : 1
+ );
+
+ const functionNumArray = expandArrayValueObject(maxRowLength, maxColumnLength, _functionNum, ErrorValueObject.create(ErrorType.NA));
+ const optionsArray = expandArrayValueObject(maxRowLength, maxColumnLength, _options, ErrorValueObject.create(ErrorType.NA));
+
+ const resultArray = functionNumArray.mapValue((functionNumObject, rowIndex, columnIndex) => {
+ if (functionNumObject.isError()) {
+ return functionNumObject;
+ }
+
+ const optionsObject = optionsArray.get(rowIndex, columnIndex) as BaseValueObject;
+
+ if (optionsObject.isError()) {
+ return optionsObject;
+ }
+
+ // AGGREGATE function does not support the multi-area reference.
+ if (multiAreaRefs.length > 0) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ return this._handleSingleObject(functionNumObject, optionsObject, refs, isError, normalRefs);
+ });
+
+ if ((resultArray as ArrayValueObject).getRowCount() === 1 && (resultArray as ArrayValueObject).getColumnCount() === 1) {
+ return (resultArray as ArrayValueObject).get(0, 0) as BaseValueObject;
+ }
+
+ return resultArray;
+ }
+
+ private _handleSingleObject(
+ functionNum: BaseValueObject,
+ options: BaseValueObject,
+ refs: FunctionVariantType[],
+ isError: boolean,
+ normalRefs: BaseReferenceObject[]
+ ): BaseValueObject {
+ let _functionNum = functionNum;
+
+ if (functionNum.isString()) {
+ _functionNum = functionNum.convertToNumberObjectValue();
+ }
+
+ if (_functionNum.isError()) {
+ return _functionNum;
+ }
+
+ const functionNumValue = Math.floor(+_functionNum.getValue());
+
+ // Validate function number
+ if (functionNumValue < 1 || functionNumValue > 19) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ /**
+ * For function numbers 1-13, if the refs has invalid reference, return #VALUE! error.
+ */
+ if (functionNumValue >= 1 && functionNumValue <= 13 && isError) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ /**
+ * For function numbers 14-19: LARGE, SMALL, PERCENTILE.INC, QUARTILE.INC, PERCENTILE.EXC, QUARTILE.EXC.
+ * They require exactly two arguments: the data array and the k value.
+ */
+ if (functionNumValue >= 14 && functionNumValue <= 19 && refs.length !== 2) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ let _options = options;
+
+ if (options.isString()) {
+ _options = options.convertToNumberObjectValue();
+ }
+
+ if (_options.isError()) {
+ return _options;
+ }
+
+ const optionsValue = Math.floor(+_options.getValue());
+
+ if (optionsValue < 0 || optionsValue > 7) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ const aggregateOptions = this._getAggregateOptions(optionsValue);
+
+ if (functionNumValue >= 14 && functionNumValue <= 19) {
+ return this._handleLargeSmallPercentileQuartile(
+ functionNumValue,
+ aggregateOptions,
+ refs[0],
+ refs[1]
+ );
+ }
+
+ return getAggregateResult({
+ type: AggregateFunctionMap[functionNumValue],
+ ...aggregateOptions,
+ formulaDataModel: this._formulaDataModel as FormulaDataModel,
+ }, normalRefs);
+ }
+
+ private _getAggregateOptions(options: number): {
+ ignoreRowHidden: boolean;
+ ignoreErrorValues: boolean;
+ ignoreNested: boolean;
+ } {
+ switch (options) {
+ case 1:
+ return {
+ ignoreRowHidden: true,
+ ignoreErrorValues: false,
+ ignoreNested: true,
+ };
+ case 2:
+ return {
+ ignoreRowHidden: false,
+ ignoreErrorValues: true,
+ ignoreNested: true,
+ };
+ case 3:
+ return {
+ ignoreRowHidden: true,
+ ignoreErrorValues: true,
+ ignoreNested: true,
+ };
+ case 4:
+ return {
+ ignoreRowHidden: false,
+ ignoreErrorValues: false,
+ ignoreNested: false,
+ };
+ case 5:
+ return {
+ ignoreRowHidden: true,
+ ignoreErrorValues: false,
+ ignoreNested: false,
+ };
+ case 6:
+ return {
+ ignoreRowHidden: false,
+ ignoreErrorValues: true,
+ ignoreNested: false,
+ };
+ case 7:
+ return {
+ ignoreRowHidden: true,
+ ignoreErrorValues: true,
+ ignoreNested: false,
+ };
+ case 0:
+ default:
+ return {
+ ignoreRowHidden: false,
+ ignoreErrorValues: false,
+ ignoreNested: true,
+ };
+ }
+ }
+
+ private _handleLargeSmallPercentileQuartile(
+ functionNum: number,
+ aggregateOptions: {
+ ignoreRowHidden: boolean;
+ ignoreErrorValues: boolean;
+ ignoreNested: boolean;
+ },
+ array: FunctionVariantType,
+ kOrQuart: FunctionVariantType
+ ): BaseValueObject {
+ let _kOrQuart: BaseValueObject;
+
+ if (kOrQuart.isReferenceObject()) {
+ _kOrQuart = (kOrQuart as BaseReferenceObject).toArrayValueObject();
+ } else {
+ _kOrQuart = kOrQuart as BaseValueObject;
+ }
+
+ if (_kOrQuart.isError()) {
+ return _kOrQuart;
+ }
+
+ if (_kOrQuart.isArray()) {
+ const rowCount = (_kOrQuart as ArrayValueObject).getRowCount();
+ const columnCount = (_kOrQuart as ArrayValueObject).getColumnCount();
+
+ if (rowCount > 1 || columnCount > 1) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ _kOrQuart = (_kOrQuart as ArrayValueObject).get(0, 0) as BaseValueObject;
+ }
+
+ const arrayValues = getArrayValuesByAggregateIgnoreOptions(array, aggregateOptions, this._formulaDataModel as FormulaDataModel);
+
+ if (!Array.isArray(arrayValues)) {
+ return arrayValues as ErrorValueObject;
+ }
+
+ const kOrQuartValue = +_kOrQuart.getValue();
+
+ switch (functionNum) {
+ case 14: // LARGE
+ return getLargeResult(arrayValues, kOrQuartValue);
+ case 15: // SMALL
+ return getSmallResult(arrayValues, kOrQuartValue);
+ case 16: // PERCENTILE.INC
+ return getPercentileIncResult(arrayValues, kOrQuartValue);
+ case 17: // QUARTILE.INC
+ return getQuartileIncResult(arrayValues, kOrQuartValue);
+ case 18: // PERCENTILE.EXC
+ return getPercentileExcResult(arrayValues, kOrQuartValue);
+ case 19: // QUARTILE.EXC
+ return getQuartileExcResult(arrayValues, kOrQuartValue);
+ default:
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+ }
+}
diff --git a/packages/engine-formula/src/functions/math/function-map.ts b/packages/engine-formula/src/functions/math/function-map.ts
index 0df9034e70..703146a214 100644
--- a/packages/engine-formula/src/functions/math/function-map.ts
+++ b/packages/engine-formula/src/functions/math/function-map.ts
@@ -19,6 +19,7 @@ import { Acos } from './acos';
import { Acosh } from './acosh';
import { Acot } from './acot';
import { Acoth } from './acoth';
+import { Aggregate } from './aggregate';
import { Arabic } from './arabic';
import { Asin } from './asin';
import { Asinh } from './asinh';
@@ -102,6 +103,7 @@ export const functionMath = [
[Acosh, FUNCTION_NAMES_MATH.ACOSH],
[Acot, FUNCTION_NAMES_MATH.ACOT],
[Acoth, FUNCTION_NAMES_MATH.ACOTH],
+ [Aggregate, FUNCTION_NAMES_MATH.AGGREGATE],
[Arabic, FUNCTION_NAMES_MATH.ARABIC],
[Asin, FUNCTION_NAMES_MATH.ASIN],
[Asinh, FUNCTION_NAMES_MATH.ASINH],
diff --git a/packages/engine-formula/src/functions/math/subtotal/__tests__/index.spec.ts b/packages/engine-formula/src/functions/math/subtotal/__tests__/index.spec.ts
index 81ed309196..af38afce0e 100644
--- a/packages/engine-formula/src/functions/math/subtotal/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/math/subtotal/__tests__/index.spec.ts
@@ -16,10 +16,8 @@
import type { Injector, IWorkbookData } from '@univerjs/core';
import type { LexerNode } from '../../../../engine/analysis/lexer-node';
-
import type { BaseAstNode } from '../../../../engine/ast-node/base-ast-node';
-import type { ArrayValueObject } from '../../../../engine/value-object/array-value-object';
-import type { BaseValueObject, ErrorValueObject } from '../../../../engine/value-object/base-value-object';
+import type { BaseValueObject } from '../../../../engine/value-object/base-value-object';
import { BooleanNumber, CellValueType, LocaleType } from '@univerjs/core';
import { beforeEach, describe, expect, it } from 'vitest';
import { ErrorType } from '../../../../basics/error-type';
@@ -27,11 +25,10 @@ import { Lexer } from '../../../../engine/analysis/lexer';
import { AstTreeBuilder } from '../../../../engine/analysis/parser';
import { Interpreter } from '../../../../engine/interpreter/interpreter';
import { generateExecuteAstNodeData } from '../../../../engine/utils/ast-node-tool';
-import { stripErrorMargin } from '../../../../engine/utils/math-kit';
import { IFormulaCurrentConfigService } from '../../../../services/current-data.service';
import { IFunctionService } from '../../../../services/function.service';
import { IFormulaRuntimeService } from '../../../../services/runtime.service';
-import { createFunctionTestBed } from '../../../__tests__/create-function-test-bed';
+import { createFunctionTestBed, getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_MATH } from '../../function-names';
import { Subtotal } from '../index';
@@ -192,12 +189,7 @@ describe('Test subtotal', () => {
const result = interpreter.execute(generateExecuteAstNodeData(astNode as BaseAstNode));
- if ((result as ErrorValueObject).isError()) {
- return (result as ErrorValueObject).getValue();
- } else if ((result as ArrayValueObject).isArray()) {
- return (result as ArrayValueObject).toValue();
- }
- return (result as BaseValueObject).getValue();
+ return getObjectValue(result as BaseValueObject, true);
};
});
@@ -309,15 +301,15 @@ describe('Test subtotal', () => {
});
it('Stdev.s, Var1 is array, var2 is array', () => {
const result = calculate('=SUBTOTAL(7,A1:B2,A3:F4)');
- expect(result).toBe(31.27335040502625);
+ expect(result).toBe(31.273350405);
});
it('Stdev.p, Var1 is array, var2 is array', () => {
const result = calculate('=SUBTOTAL(8,A1:B2,A3:F4)');
- expect(result).toBe(29.668505203329676);
+ expect(result).toBe(29.6685052033);
});
it('sum, Var1 is array, var2 is array', () => {
const result = calculate('=SUBTOTAL(9,A1:B2,A3:F4)');
- expect(stripErrorMargin(Number(result))).toBe(111.57);
+ expect(result).toBe(111.57);
});
it('Var.s, Var1 is array, var2 is array', () => {
const result = calculate('=SUBTOTAL(10,A1:B2,A3:F4)');
@@ -325,7 +317,7 @@ describe('Test subtotal', () => {
});
it('Var.p, Var1 is array, var2 is array', () => {
const result = calculate('=SUBTOTAL(11,A1:B2,A3:F4)');
- expect(stripErrorMargin(Number(result))).toBe(880.220201);
+ expect(result).toBe(880.220201);
});
});
@@ -359,16 +351,16 @@ describe('Test subtotal', () => {
expect(result).toBeCloseTo(0.16263456, 7);
result = calculate('=SUBTOTAL(108,A3:F4)');
- expect(stripErrorMargin(Number(result))).toBe(0.115);
+ expect(result).toBe(0.115);
result = calculate('=SUBTOTAL(109,A3:F4)');
expect(result).toBe(2.23);
result = calculate('=SUBTOTAL(110,A3:F4)');
- expect(stripErrorMargin(Number(result))).toBe(0.02645);
+ expect(result).toBe(0.02645);
result = calculate('=SUBTOTAL(111,A3:F4)');
- expect(stripErrorMargin(Number(result))).toBe(0.013225);
+ expect(result).toBe(0.013225);
});
});
});
diff --git a/packages/engine-formula/src/functions/math/subtotal/index.ts b/packages/engine-formula/src/functions/math/subtotal/index.ts
index 142e064a6f..af35687c30 100644
--- a/packages/engine-formula/src/functions/math/subtotal/index.ts
+++ b/packages/engine-formula/src/functions/math/subtotal/index.ts
@@ -14,46 +14,35 @@
* limitations under the License.
*/
-import type { IObjectArrayPrimitiveType, IRowData, Nullable } from '@univerjs/core';
import type { BaseReferenceObject, FunctionVariantType } from '../../../engine/reference-object/base-reference-object';
-import type { MultiAreaValue } from '../../../engine/reference-object/multi-area-reference-object';
+import type { MultiAreaReferenceObject, MultiAreaValue } from '../../../engine/reference-object/multi-area-reference-object';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import type { FormulaDataModel } from '../../../models/formula-data.model';
-import { BooleanNumber } from '@univerjs/core';
import { ErrorType } from '../../../basics/error-type';
+import { AggregateFunctionType, getAggregateResult, parseAggregateDataRefs } from '../../../basics/statistical';
import { createNewArray } from '../../../engine/utils/array-object';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
-enum FunctionNum {
- AVERAGE = 1,
- COUNT = 2,
- COUNTA = 3,
- MAX = 4,
- MIN = 5,
- PRODUCT = 6,
- STDEV = 7,
- STDEVP = 8,
- SUM = 9,
- VAR = 10,
- VARP = 11,
+interface IMultiAreaInfo {
+ ref: FunctionVariantType;
+ rowAreas: MultiAreaValue[]; // Each element represents a row
}
-enum FunctionNumIgnoreHidden {
- AVERAGE = 101,
- COUNT = 102,
- COUNTA = 103,
- MAX = 104,
- MIN = 105,
- PRODUCT = 106,
- STDEV = 107,
- STDEVP = 108,
- SUM = 109,
- VAR = 110,
- VARP = 111,
-}
+const AggregateFunctionMap: Record<number, AggregateFunctionType> = {
+ 1: AggregateFunctionType.AVERAGE,
+ 2: AggregateFunctionType.COUNT,
+ 3: AggregateFunctionType.COUNTA,
+ 4: AggregateFunctionType.MAX,
+ 5: AggregateFunctionType.MIN,
+ 6: AggregateFunctionType.PRODUCT,
+ 7: AggregateFunctionType.STDEV,
+ 8: AggregateFunctionType.STDEVP,
+ 9: AggregateFunctionType.SUM,
+ 10: AggregateFunctionType.VAR,
+ 11: AggregateFunctionType.VARP,
+};
export class Subtotal extends BaseFunction {
override minParams = 2;
@@ -67,505 +56,200 @@ export class Subtotal extends BaseFunction {
override needsFormulaDataModel = true;
override calculate(functionNum: FunctionVariantType, ...refs: FunctionVariantType[]) {
- if (functionNum.isError()) {
- return functionNum;
- }
-
- const multiAreaRefs: FunctionVariantType[] = [];
- const normalRefs: FunctionVariantType[] = [];
-
- refs.forEach((ref) => {
- if (ref.isReferenceObject() && (ref as BaseReferenceObject).isMultiArea?.()) {
- multiAreaRefs.push(ref);
- } else {
- normalRefs.push(ref);
- }
- });
-
- // --------- 没有 multi-area:保持原来的逻辑 ---------
- if (multiAreaRefs.length === 0) {
- if (functionNum.isReferenceObject()) {
- return (functionNum as BaseReferenceObject)
- .toArrayValueObject()
- .mapValue((valueObject) => this._handleSingleObject(valueObject, ...refs));
- }
-
- return this._handleSingleObject(functionNum as BaseValueObject, ...refs);
- }
-
- // --------- 有 multi-area:按“行”展开 ---------
-
- // 1. 把 functionNum 变成一维 BaseValueObject 列表(按行)
- let fnList: BaseValueObject[] = [];
+ let _functionNum: BaseValueObject;
if (functionNum.isReferenceObject()) {
- const fnArray = (functionNum as BaseReferenceObject).toArrayValueObject().flatten();
- fnList = fnArray.getArrayValue()[0] as BaseValueObject[];
- } else if (functionNum.isArray()) {
- const fnArray = (functionNum as ArrayValueObject).flatten();
- fnList = fnArray.getArrayValue()[0] as BaseValueObject[];
+ _functionNum = (functionNum as BaseReferenceObject).toArrayValueObject();
} else {
- fnList = [functionNum as BaseValueObject];
- }
-
- // 2. 把 MultiAreaReferenceObject 的二维 areas 映射成「按行的代表 Area」
- // 每一行我们取该行中第一个非 error 的 area,保持“表格按行展开”的语义
- interface MultiAreaInfo {
- ref: FunctionVariantType;
- rowAreas: MultiAreaValue[]; // 每个元素代表一行
+ _functionNum = functionNum as BaseValueObject;
}
- const multiAreaInfoList: MultiAreaInfo[] = multiAreaRefs.map((ref) => {
- const refObj = ref as BaseReferenceObject & { getAreas?: () => MultiAreaValue[][] | MultiAreaValue[] };
+ const { isError, multiAreaRefs, normalRefs } = parseAggregateDataRefs(refs);
- let rowAreas: MultiAreaValue[] = [];
+ // If there is multi-area reference, we need to expand the result by "rows".
+ if (!isError && multiAreaRefs.length > 0) {
+ return this._handleMultiAreaRefs(_functionNum, multiAreaRefs, refs as BaseReferenceObject[]);
+ }
- if (typeof refObj.getAreas === 'function') {
- const raw = refObj.getAreas();
+ // If there is no multi-area reference, keep the original logic.
+ if (_functionNum.isArray()) {
+ const resultArray = (_functionNum as ArrayValueObject).mapValue((valueObject) => this._handleSingleObject(valueObject, {
+ isError,
+ refs: normalRefs,
+ }));
- // 新版 MultiAreaReferenceObject:MultiAreaValue[][]
- if (Array.isArray(raw) && raw.length > 0 && Array.isArray(raw[0])) {
- const areas2D = raw as MultiAreaValue[][];
- rowAreas = areas2D.map((row) => {
- if (!row || row.length === 0) {
- // 这行完全没 area,就占位一下,用原 ref 本身
- return refObj as MultiAreaValue;
- }
- // 找这一行第一个非 error 的 area,没有就用第一个
- const firstNonError = row.find((a) => !a.isError());
- return (firstNonError ?? row[0]) as MultiAreaValue;
- });
- } else {
- // 兼容老的 getAreas(): MultiAreaValue[]
- rowAreas = raw as MultiAreaValue[];
- }
- } else {
- // 理论上 isMultiArea() 就应该有 getAreas,这里兜个底
- rowAreas = [refObj as MultiAreaValue];
+ if ((resultArray as ArrayValueObject).getRowCount() === 1 && (resultArray as ArrayValueObject).getColumnCount() === 1) {
+ return (resultArray as ArrayValueObject).get(0, 0) as BaseValueObject;
}
- return {
- ref,
- rowAreas,
- };
- });
+ return resultArray;
+ }
- const multiAreaInfoMap = new Map<FunctionVariantType, MultiAreaInfo>();
- multiAreaInfoList.forEach((info) => {
- multiAreaInfoMap.set(info.ref, info);
+ return this._handleSingleObject(_functionNum, {
+ isError,
+ refs: normalRefs,
});
+ }
- // 3. 计算要展开多少“行”
- const maxAreasLen = multiAreaInfoList.reduce(
- (max, info) => Math.max(max, info.rowAreas.length || 1),
- 1
- );
- const maxLen = Math.max(fnList.length || 1, maxAreasLen);
-
- const rowResults: BaseValueObject[][] = [];
-
- // 4. 对每一行 index:
- // - 取对应的 functionNum 值(不够就用最后一个)
- // - 对每个 multi-area ref:取对应行的代表 Area(不够行就用最后一行)
- // - normalRefs 直接复用
- for (let index = 0; index < maxLen; index++) {
- const fnIndex = index < fnList.length ? index : fnList.length - 1;
- const fnValue = fnList[fnIndex];
-
- const refsForRow: FunctionVariantType[] = refs.map((ref) => {
- const info = multiAreaInfoMap.get(ref);
- if (info) {
- const rowIndex = index < info.rowAreas.length ? index : info.rowAreas.length - 1;
- const area = info.rowAreas[rowIndex];
-
- // area 可能是 ErrorValueObject 或 BaseReferenceObject,
- // 都是 FunctionVariantType 合法成员,直接返回即可
- return area as FunctionVariantType;
- }
-
- // 非 multi-area 引用:所有行共用同一个
- return ref;
- });
-
- const rowResult = this._handleSingleObject(fnValue, ...refsForRow);
- rowResults.push([rowResult]);
- }
-
- // 5. 只有一行就直接返回标量,多行返回 ArrayValueObject(按行展开)
- if (rowResults.length === 1) {
- return rowResults[0][0];
+ private _handleSingleObject(
+ functionNum: BaseValueObject,
+ options: {
+ isError: boolean;
+ refs: BaseReferenceObject[];
}
+ ): BaseValueObject {
+ const { isError = false, refs } = options;
- const arrayResult = createNewArray(rowResults, rowResults.length, 1);
- return arrayResult;
- }
-
- // eslint-disable-next-line max-lines-per-function, complexity
- private _handleSingleObject(functionNum: Nullable<BaseValueObject>, ...refs: FunctionVariantType[]) {
- const indexNum = this._getIndexNumValue(functionNum);
- let result;
+ let _functionNum = functionNum;
- if (indexNum instanceof ErrorValueObject) {
- return indexNum;
+ if (functionNum.isString()) {
+ _functionNum = functionNum.convertToNumberObjectValue();
}
- switch (indexNum) {
- case FunctionNum.AVERAGE:
- result = this._average(false, ...refs);
- break;
- case FunctionNum.COUNT:
- result = this._count(false, ...refs);
- break;
- case FunctionNum.COUNTA:
- result = this._counta(false, ...refs);
- break;
- case FunctionNum.MAX:
- result = this._max(false, ...refs);
- break;
- case FunctionNum.MIN:
- result = this._min(false, ...refs);
- break;
- case FunctionNum.PRODUCT:
- result = this._product(false, ...refs);
- break;
- case FunctionNum.STDEV:
- result = this._stdev(false, ...refs);
- break;
- case FunctionNum.STDEVP:
- result = this._stdevp(false, ...refs);
- break;
- case FunctionNum.SUM:
- result = this._sum(false, ...refs);
- break;
- case FunctionNum.VAR:
- result = this._var(false, ...refs);
- break;
- case FunctionNum.VARP:
- result = this._varp(false, ...refs);
- break;
- case FunctionNumIgnoreHidden.AVERAGE:
- result = this._average(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.COUNT:
- result = this._count(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.COUNTA:
- result = this._counta(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.MAX:
- result = this._max(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.MIN:
- result = this._min(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.PRODUCT:
- result = this._product(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.STDEV:
- result = this._stdev(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.STDEVP:
- result = this._stdevp(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.SUM:
- result = this._sum(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.VAR:
- result = this._var(true, ...refs);
- break;
- case FunctionNumIgnoreHidden.VARP:
- result = this._varp(true, ...refs);
- break;
- default:
- result = ErrorValueObject.create(ErrorType.VALUE);
+ if (_functionNum.isError()) {
+ return _functionNum;
}
- return result as BaseValueObject;
- }
-
- private _getIndexNumValue(indexNum: Nullable<BaseValueObject>) {
- // null, true, false, 0 , 1, ' 1',
- const indexNumValue = indexNum ? Number(indexNum.getValue()) : 0;
-
- if (Number.isNaN(indexNumValue)) {
+ // If the refs has invalid reference, return #VALUE! error.
+ if (isError) {
return ErrorValueObject.create(ErrorType.VALUE);
}
- const indexNumValueInt = Math.floor(indexNumValue);
+ let functionNumValue = Math.floor(+_functionNum.getValue());
- // 1-11, or 101-111
- if ((indexNumValueInt >= 1 && indexNumValueInt <= 11) || (indexNumValueInt >= 101 && indexNumValueInt <= 111)) {
- return indexNumValueInt;
+ if (functionNumValue < 1 || (functionNumValue > 11 && functionNumValue < 101) || functionNumValue > 111) {
+ return ErrorValueObject.create(ErrorType.VALUE);
}
- return ErrorValueObject.create(ErrorType.VALUE);
- }
+ let ignoreRowHidden = false;
- private _average(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- const flattenArray = this._flattenRefArray(ignoreHidden, ...refs);
-
- if (flattenArray.isError()) {
- return flattenArray;
+ if (functionNumValue >= 101) {
+ functionNumValue -= 100;
+ ignoreRowHidden = true;
}
- return flattenArray.mean();
+ return getAggregateResult({
+ type: AggregateFunctionMap[functionNumValue],
+ ignoreRowHidden,
+ ignoreErrorValues: false,
+ ignoreNested: true,
+ formulaDataModel: this._formulaDataModel as FormulaDataModel,
+ }, refs);
}
- private _count(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- let accumulatorAll: BaseValueObject = NumberValueObject.create(0);
- for (let i = 0; i < refs.length; i++) {
- const variant = refs[i];
-
- if (!variant.isReferenceObject()) {
- return ErrorValueObject.create(ErrorType.VALUE);
- }
-
- const filteredOutRows = (variant as BaseReferenceObject).getFilteredOutRows();
- const rowData = (variant as BaseReferenceObject).getRowData();
-
- (variant as BaseReferenceObject).iterator((valueObject, rowIndex) => {
- // Filtered rows are always excluded.
- if (filteredOutRows.includes(rowIndex)) {
- return true; // continue
- }
+ private _handleMultiAreaRefs(functionNum: BaseValueObject, multiAreaRefs: MultiAreaReferenceObject[], refs: BaseReferenceObject[]) {
+ const rowCount = functionNum.isArray() ? (functionNum as ArrayValueObject).getRowCount() : 1;
+ const columnCount = functionNum.isArray() ? (functionNum as ArrayValueObject).getColumnCount() : 1;
+ const { multiAreaInfoMap, maxAreasLen } = this._getMultiAreaInfo(multiAreaRefs);
+ const maxLen = Math.max(rowCount, maxAreasLen);
- if (ignoreHidden && this._isRowHidden(rowData, rowIndex)) {
- return true;
- }
-
- if (valueObject?.isNumber()) {
- accumulatorAll = accumulatorAll.plusBy(1);
+ const results: BaseValueObject[][] = [];
+ /**
+ * For each row index:
+ * - Take the corresponding functionNum value (use the last one if not enough)
+ * - For each multi-area ref: take the representative Area for the corresponding row (use the last row if not enough)
+ * - normalRefs are reused directly
+ */
+ for (let index = 0; index < maxLen; index++) {
+ const fnRowIndex = index < rowCount ? index : rowCount - 1;
+ const rowResults: BaseValueObject[] = [];
+
+ /**
+ * For each column in functionNum should produce a result.
+ */
+ for (let c = 0; c < columnCount; c++) {
+ /**
+ * If the functionNum is multi-row and the current row index exceeds its row count, return #N/A error:
+ * For example, `=SUBTOTAL(A1:B2,OFFSET(J$16,ROW(J$16:J$107)-MIN(ROW($J$16:$J$107)),0))`.
+ *
+ * If the functionNum is only one row, all rows share the same functionNum value:
+ * For example, `=SUBTOTAL(A1:B1,OFFSET(J$16,ROW(J$16:J$107)-MIN(ROW($J$16:$J$107)),0))`.
+ */
+ if (rowCount > 1 && index >= rowCount) {
+ rowResults.push(ErrorValueObject.create(ErrorType.NA));
+ continue;
}
- });
- }
-
- return accumulatorAll;
- }
- private _counta(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- let accumulatorAll: BaseValueObject = NumberValueObject.create(0);
- for (let i = 0; i < refs.length; i++) {
- const variant = refs[i];
+ const fnValueObject = functionNum.isArray() ? (functionNum as ArrayValueObject).get(fnRowIndex, c) as BaseValueObject : functionNum;
+ const refsForRow: BaseReferenceObject[] = [];
- if (!variant.isReferenceObject()) {
- return ErrorValueObject.create(ErrorType.VALUE);
- }
+ for (let refIndex = 0; refIndex < refs.length; refIndex++) {
+ const ref = refs[refIndex];
+ const info = multiAreaInfoMap.get(ref);
- const filteredOutRows = (variant as BaseReferenceObject).getFilteredOutRows();
- const rowData = (variant as BaseReferenceObject).getRowData();
+ if (info) {
+ const rowIndex = index < info.rowAreas.length ? index : info.rowAreas.length - 1;
+ const area = info.rowAreas[rowIndex];
- (variant as BaseReferenceObject).iterator((valueObject, rowIndex) => {
- // Filtered rows are always excluded.
- if (filteredOutRows.includes(rowIndex)) {
- return true; // continue
- }
+ if (area.isError()) {
+ rowResults.push(area as ErrorValueObject);
+ break;
+ }
- if (ignoreHidden && this._isRowHidden(rowData, rowIndex)) {
- return true;
- }
+ /**
+ * The area may be an ErrorValueObject or a BaseReferenceObject,
+ * both of which are valid members of FunctionVariantType and can be returned directly.
+ */
+ refsForRow.push(area as BaseReferenceObject);
+ continue;
+ }
- if (valueObject == null || valueObject.isNull()) {
- return true;
+ // Non multi-area reference: all rows share the same one
+ refsForRow.push(ref);
}
- accumulatorAll = accumulatorAll.plusBy(1);
- });
- }
-
- return accumulatorAll;
- }
-
- private _max(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- const flattenArray = this._flattenRefArray(ignoreHidden, ...refs);
-
- if (flattenArray.isError()) {
- return flattenArray;
- }
-
- if (this._isBlankArrayObject(flattenArray)) {
- return NumberValueObject.create(0);
- }
-
- return flattenArray.max();
- }
-
- private _min(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- const flattenArray = this._flattenRefArray(ignoreHidden, ...refs);
-
- if (flattenArray.isError()) {
- return flattenArray;
- }
-
- if (this._isBlankArrayObject(flattenArray)) {
- return NumberValueObject.create(0);
- }
-
- return flattenArray.min();
- }
-
- private _product(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- const flattenArray = this._flattenRefArray(ignoreHidden, ...refs);
-
- if (flattenArray.isError()) {
- return flattenArray;
- }
-
- if (this._isBlankArrayObject(flattenArray)) {
- return NumberValueObject.create(0);
- }
-
- let result: NumberValueObject = NumberValueObject.create(1);
- (flattenArray as ArrayValueObject).iterator((valueObject) => {
- result = result.multiply(
- valueObject as BaseValueObject
- ) as NumberValueObject;
- });
-
- return result;
- }
-
- private _stdev(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- const flattenArray = this._flattenRefArray(ignoreHidden, ...refs);
-
- if (flattenArray.isError()) {
- return flattenArray;
- }
-
- if (this._isBlankArrayObject(flattenArray)) {
- return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
- }
-
- return flattenArray.std(1);
- }
-
- private _stdevp(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- const flattenArray = this._flattenRefArray(ignoreHidden, ...refs);
-
- if (flattenArray.isError()) {
- return flattenArray;
- }
-
- if (this._isBlankArrayObject(flattenArray)) {
- return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
- }
-
- return flattenArray.std();
- }
-
- private _sum(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- const flattenArray = this._flattenRefArray(ignoreHidden, ...refs);
-
- if (flattenArray.isError()) {
- return flattenArray;
- }
-
- return flattenArray.sum();
- }
-
- private _var(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- const flattenArray = this._flattenRefArray(ignoreHidden, ...refs);
-
- if (flattenArray.isError()) {
- return flattenArray;
- }
-
- if (this._isBlankArrayObject(flattenArray)) {
- return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
- }
-
- return flattenArray.var(1);
- }
-
- private _varp(ignoreHidden: boolean, ...refs: FunctionVariantType[]) {
- const flattenArray = this._flattenRefArray(ignoreHidden, ...refs);
+ const rowResult = this._handleSingleObject(fnValueObject, {
+ isError: false,
+ refs: refsForRow,
+ });
+ rowResults.push(rowResult);
+ }
- if (flattenArray.isError()) {
- return flattenArray;
+ results.push(rowResults);
}
- if (this._isBlankArrayObject(flattenArray)) {
- return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
+ // If there is only one row, return the scalar directly; for multiple rows, return an ArrayValueObject (expanded by row).
+ if (results.length === 1) {
+ return results[0][0];
}
- return flattenArray.var();
+ return createNewArray(results, results.length, columnCount);
}
- private _flattenRefArray(ignoreHidden: boolean, ...variants: FunctionVariantType[]) {
- const flattenValues: BaseValueObject[][] = [];
- flattenValues[0] = [];
-
- for (let i = 0; i < variants.length; i++) {
- const variant = variants[i];
-
- if (variant.isError()) {
- return variant as ErrorValueObject;
- }
-
- if (!variant.isReferenceObject()) {
- return ErrorValueObject.create(ErrorType.VALUE);
- }
-
- const filteredOutRows = (variant as BaseReferenceObject).getFilteredOutRows();
- const rowData = (variant as BaseReferenceObject).getRowData();
- const unitId = (variant as BaseReferenceObject).getUnitId();
- const sheetId = (variant as BaseReferenceObject).getSheetId();
- const unitData = (variant as BaseReferenceObject).getUnitData();
- const cellData = unitData[unitId]?.[sheetId]?.cellData;
-
- let errorValue: Nullable<BaseValueObject>;
-
- (variant as BaseReferenceObject).iterator((valueObject, rowIndex, columnIndex) => {
- // Filtered rows are always excluded.
- if (filteredOutRows.includes(rowIndex)) {
- return true; // continue
- }
-
- if (ignoreHidden && this._isRowHidden(rowData, rowIndex)) {
- return true; // continue
- }
+ private _getMultiAreaInfo(multiAreaRefs: MultiAreaReferenceObject[]) {
+ /**
+ * Map the two-dimensional areas of MultiAreaReferenceObject into "representative Area by row".
+ * For each row, we take the first non-error area to maintain the semantics of "table expanded by row".
+ */
+ const multiAreaInfoMap = new Map<BaseReferenceObject, IMultiAreaInfo>();
- // Ignore other SUBTOTAL formula results
- const cellValue = cellData.getValue(rowIndex, columnIndex);
- if (cellValue?.f || cellValue?.si) {
- const formulaString = (this._formulaDataModel as FormulaDataModel).getFormulaStringByCell(rowIndex, columnIndex, sheetId, unitId);
+ // Calculate the maximum number of rows to expand.
+ let maxAreasLen = 1;
- // match 'SUBTOTAL(' for simple check
- if (formulaString && formulaString.indexOf(`${this.name}(`) > -1) {
- return true; // continue
- }
+ multiAreaRefs.forEach((ref) => {
+ const rowAreas: MultiAreaValue[] = ref.getAreas().map((row) => {
+ if (!row || row.length === 0) {
+ // If the row has no area, use the original ref itself as a placeholder.
+ return ref;
}
- // 'test', ' ', blank cell, TRUE and FALSE are ignored
- if (valueObject == null || valueObject.isNull() || valueObject.isString() || valueObject.isBoolean()) {
- return true;
- }
+ // Find the first non-error area in this row, or use the first one if none exist.
+ const firstNonError = row.find((a) => !a.isError());
- if (valueObject.isError()) {
- errorValue = valueObject;
- return false; // break
- }
-
- flattenValues[0].push(valueObject);
+ return firstNonError ?? row[0];
});
- if (errorValue?.isError()) {
- return errorValue;
- }
- }
-
- return createNewArray(flattenValues, 1, flattenValues[0].length);
- }
-
- private _isRowHidden(rowData: IObjectArrayPrimitiveType<Partial<IRowData>>, rowIndex: number) {
- const row = rowData[rowIndex];
- if (!row) {
- return false;
- }
+ multiAreaInfoMap.set(ref, {
+ ref,
+ rowAreas,
+ });
- return row.hd === BooleanNumber.TRUE;
- }
+ maxAreasLen = Math.max(maxAreasLen, rowAreas.length || 1);
+ });
- private _isBlankArrayObject(arrayObject: BaseValueObject) {
- return arrayObject.getArrayValue()[0].length === 0;
+ return {
+ multiAreaInfoMap,
+ maxAreasLen,
+ };
}
}
diff --git a/packages/engine-formula/src/functions/statistical/large/index.ts b/packages/engine-formula/src/functions/statistical/large/index.ts
index 215e7b837b..6694fedc71 100644
--- a/packages/engine-formula/src/functions/statistical/large/index.ts
+++ b/packages/engine-formula/src/functions/statistical/large/index.ts
@@ -15,12 +15,9 @@
*/
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
-import { ErrorType } from '../../../basics/error-type';
+import type { BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { getArrayValuesByAggregateIgnoreOptions, getLargeResult } from '../../../basics/statistical';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
-import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
export class Large extends BaseFunction {
@@ -29,7 +26,7 @@ export class Large extends BaseFunction {
override maxParams = 2;
override calculate(array: BaseValueObject, k: BaseValueObject): BaseValueObject {
- const arrayValues = this._getValues(array);
+ const arrayValues = getArrayValuesByAggregateIgnoreOptions(array);
if (k.isArray()) {
const resultArray = (k as ArrayValueObject).mapValue((kObject) => this._handleSingleObject(arrayValues, kObject));
@@ -45,8 +42,8 @@ export class Large extends BaseFunction {
}
private _handleSingleObject(array: number[] | ErrorValueObject, k: BaseValueObject): BaseValueObject {
- if (array instanceof ErrorValueObject) {
- return array;
+ if (!Array.isArray(array)) {
+ return array as ErrorValueObject;
}
const { isError, errorObject, variants } = checkVariantsErrorIsStringToNumber(k);
@@ -56,50 +53,8 @@ export class Large extends BaseFunction {
}
const [kObject] = variants as BaseValueObject[];
+ const kValue = +kObject.getValue();
- let kValue = +kObject.getValue();
-
- if (kValue < 1 || kValue > array.length) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- kValue = Math.ceil(kValue);
-
- return NumberValueObject.create(array[kValue - 1]);
- }
-
- private _getValues(array: BaseValueObject): number[] | ErrorValueObject {
- const rowCount = array.isArray() ? (array as ArrayValueObject).getRowCount() : 1;
- const columnCount = array.isArray() ? (array as ArrayValueObject).getColumnCount() : 1;
-
- const values: number[] = [];
-
- for (let r = 0; r < rowCount; r++) {
- for (let c = 0; c < columnCount; c++) {
- const valueObject = array.isArray() ? (array as ArrayValueObject).get(r, c) as BaseValueObject : array;
-
- if (valueObject.isError()) {
- return valueObject as ErrorValueObject;
- }
-
- if (valueObject.isNull() || valueObject.isBoolean() || valueObject.isString()) {
- continue;
- }
-
- const value = valueObject.getValue();
-
- if (!isRealNum(value)) {
- continue;
- }
-
- values.push(+value);
- }
- }
-
- if (values.length === 0) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- return values.sort((a, b) => b - a);
+ return getLargeResult(array, kValue);
}
}
diff --git a/packages/engine-formula/src/functions/statistical/median/index.ts b/packages/engine-formula/src/functions/statistical/median/index.ts
index d706aebce4..11eb2eab04 100644
--- a/packages/engine-formula/src/functions/statistical/median/index.ts
+++ b/packages/engine-formula/src/functions/statistical/median/index.ts
@@ -18,8 +18,8 @@ import type { ArrayValueObject } from '../../../engine/value-object/array-value-
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
import { ErrorType } from '../../../basics/error-type';
+import { getMedianResult } from '../../../basics/statistical';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
export class Median extends BaseFunction {
@@ -91,21 +91,6 @@ export class Median extends BaseFunction {
return ErrorValueObject.create(ErrorType.NUM);
}
- return this._getResult(values.sort((a, b) => a - b));
- }
-
- private _getResult(values: number[]): BaseValueObject {
- const n = values.length;
-
- let result: number;
-
- if (n % 2 === 0) {
- const mid = n / 2;
- result = (values[mid - 1] + values[mid]) / 2;
- } else {
- result = values[Math.floor(n / 2)];
- }
-
- return NumberValueObject.create(result);
+ return getMedianResult(values);
}
}
diff --git a/packages/engine-formula/src/functions/statistical/mode-sngl/index.ts b/packages/engine-formula/src/functions/statistical/mode-sngl/index.ts
index 2c79b33484..8bb03738f0 100644
--- a/packages/engine-formula/src/functions/statistical/mode-sngl/index.ts
+++ b/packages/engine-formula/src/functions/statistical/mode-sngl/index.ts
@@ -14,29 +14,25 @@
* limitations under the License.
*/
+import type { modeSnglValueCountMapType } from '../../../basics/statistical';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { isRealNum } from '@univerjs/core';
import { ErrorType } from '../../../basics/error-type';
+import { getModeSnglResult } from '../../../basics/statistical';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
-type valueMapType = Record<number, {
- count: number;
- order: number;
-}>;
-
export class ModeSngl extends BaseFunction {
override minParams = 1;
override maxParams = 255;
override calculate(...variants: BaseValueObject[]): BaseValueObject {
- const valueMap: valueMapType = {};
+ const valueCountMap: modeSnglValueCountMapType = {};
let order = 0;
- let maxCount = 1;
+ let valueMaxCount = 1;
for (let i = 0; i < variants.length; i++) {
const variant = variants[i];
@@ -74,32 +70,23 @@ export class ModeSngl extends BaseFunction {
continue;
}
- if (valueMap[+value]) {
- valueMap[+value].count++;
+ if (valueCountMap[+value]) {
+ valueCountMap[+value].count++;
- if (valueMap[+value].count > maxCount) {
- maxCount = valueMap[+value].count;
+ if (valueCountMap[+value].count > valueMaxCount) {
+ valueMaxCount = valueCountMap[+value].count;
}
} else {
- valueMap[+value] = { count: 1, order: order++ };
+ valueCountMap[+value] = { count: 1, order: order++ };
}
}
}
}
- if (order === 0 || maxCount === 1) {
+ if (order === 0 || valueMaxCount === 1) {
return new ErrorValueObject(ErrorType.NA);
}
- return this._getResult(valueMap, maxCount);
- }
-
- private _getResult(valueMap: valueMapType, maxCount: number): BaseValueObject {
- const result = Object.entries(valueMap)
- .filter(([_, { count }]) => count === maxCount)
- .sort((a, b) => a[1].order - b[1].order)
- .map(([value]) => +value);
-
- return NumberValueObject.create(result[0]);
+ return getModeSnglResult(valueCountMap, valueMaxCount);
}
}
diff --git a/packages/engine-formula/src/functions/statistical/percentile-exc/index.ts b/packages/engine-formula/src/functions/statistical/percentile-exc/index.ts
index 3d63bfdb7a..f6d5fc3fbd 100644
--- a/packages/engine-formula/src/functions/statistical/percentile-exc/index.ts
+++ b/packages/engine-formula/src/functions/statistical/percentile-exc/index.ts
@@ -15,12 +15,9 @@
*/
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
-import { ErrorType } from '../../../basics/error-type';
+import type { BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { getArrayValuesByAggregateIgnoreOptions, getPercentileExcResult } from '../../../basics/statistical';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
-import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
export class PercentileExc extends BaseFunction {
@@ -29,7 +26,7 @@ export class PercentileExc extends BaseFunction {
override maxParams = 2;
override calculate(array: BaseValueObject, k: BaseValueObject): BaseValueObject {
- const arrayValues = this._getValues(array);
+ const arrayValues = getArrayValuesByAggregateIgnoreOptions(array);
if (k.isArray()) {
const resultArray = (k as ArrayValueObject).mapValue((kObject) => this._handleSingleObject(arrayValues, kObject));
@@ -45,8 +42,8 @@ export class PercentileExc extends BaseFunction {
}
private _handleSingleObject(array: number[] | ErrorValueObject, k: BaseValueObject): BaseValueObject {
- if (array instanceof ErrorValueObject) {
- return array;
+ if (!Array.isArray(array)) {
+ return array as ErrorValueObject;
}
const { isError, errorObject, variants } = checkVariantsErrorIsStringToNumber(k);
@@ -59,57 +56,6 @@ export class PercentileExc extends BaseFunction {
const kValue = +kObject.getValue();
- const n = array.length;
-
- if (kValue < 1 / (n + 1) || kValue > 1 - 1 / (n + 1)) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- const kValueIndex = kValue * (n + 1) - 1;
- const integerPart = Math.floor(kValueIndex);
- const fractionPart = kValueIndex - integerPart;
-
- if (fractionPart === 0) {
- return NumberValueObject.create(array[integerPart]);
- }
-
- const result = array[integerPart] + fractionPart * (array[integerPart + 1] - array[integerPart]);
-
- return NumberValueObject.create(result);
- }
-
- private _getValues(array: BaseValueObject): number[] | ErrorValueObject {
- const rowCount = array.isArray() ? (array as ArrayValueObject).getRowCount() : 1;
- const columnCount = array.isArray() ? (array as ArrayValueObject).getColumnCount() : 1;
-
- const values: number[] = [];
-
- for (let r = 0; r < rowCount; r++) {
- for (let c = 0; c < columnCount; c++) {
- const valueObject = array.isArray() ? (array as ArrayValueObject).get(r, c) as BaseValueObject : array;
-
- if (valueObject.isError()) {
- return valueObject as ErrorValueObject;
- }
-
- if (valueObject.isNull() || valueObject.isBoolean() || valueObject.isString()) {
- continue;
- }
-
- const value = valueObject.getValue();
-
- if (!isRealNum(value)) {
- continue;
- }
-
- values.push(+value);
- }
- }
-
- if (values.length === 0) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- return values.sort((a, b) => a - b);
+ return getPercentileExcResult(array, kValue);
}
}
diff --git a/packages/engine-formula/src/functions/statistical/percentile-inc/index.ts b/packages/engine-formula/src/functions/statistical/percentile-inc/index.ts
index bbf18bb756..2b3daba572 100644
--- a/packages/engine-formula/src/functions/statistical/percentile-inc/index.ts
+++ b/packages/engine-formula/src/functions/statistical/percentile-inc/index.ts
@@ -15,12 +15,9 @@
*/
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
-import { ErrorType } from '../../../basics/error-type';
+import type { BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { getArrayValuesByAggregateIgnoreOptions, getPercentileIncResult } from '../../../basics/statistical';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
-import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
export class PercentileInc extends BaseFunction {
@@ -29,7 +26,7 @@ export class PercentileInc extends BaseFunction {
override maxParams = 2;
override calculate(array: BaseValueObject, k: BaseValueObject): BaseValueObject {
- const arrayValues = this._getValues(array);
+ const arrayValues = getArrayValuesByAggregateIgnoreOptions(array);
if (k.isArray()) {
const resultArray = (k as ArrayValueObject).mapValue((kObject) => this._handleSingleObject(arrayValues, kObject));
@@ -45,8 +42,8 @@ export class PercentileInc extends BaseFunction {
}
private _handleSingleObject(array: number[] | ErrorValueObject, k: BaseValueObject): BaseValueObject {
- if (array instanceof ErrorValueObject) {
- return array;
+ if (!Array.isArray(array)) {
+ return array as ErrorValueObject;
}
const { isError, errorObject, variants } = checkVariantsErrorIsStringToNumber(k);
@@ -56,60 +53,8 @@ export class PercentileInc extends BaseFunction {
}
const [kObject] = variants as BaseValueObject[];
-
const kValue = +kObject.getValue();
- const n = array.length;
-
- if (kValue < 0 || kValue > 1) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- const kValueIndex = kValue * (n - 1);
- const integerPart = Math.floor(kValueIndex);
- const fractionPart = kValueIndex - integerPart;
-
- if (fractionPart === 0) {
- return NumberValueObject.create(array[integerPart]);
- }
-
- const result = array[integerPart] + fractionPart * (array[integerPart + 1] - array[integerPart]);
-
- return NumberValueObject.create(result);
- }
-
- private _getValues(array: BaseValueObject): number[] | ErrorValueObject {
- const rowCount = array.isArray() ? (array as ArrayValueObject).getRowCount() : 1;
- const columnCount = array.isArray() ? (array as ArrayValueObject).getColumnCount() : 1;
-
- const values: number[] = [];
-
- for (let r = 0; r < rowCount; r++) {
- for (let c = 0; c < columnCount; c++) {
- const valueObject = array.isArray() ? (array as ArrayValueObject).get(r, c) as BaseValueObject : array;
-
- if (valueObject.isError()) {
- return valueObject as ErrorValueObject;
- }
-
- if (valueObject.isNull() || valueObject.isBoolean() || valueObject.isString()) {
- continue;
- }
-
- const value = valueObject.getValue();
-
- if (!isRealNum(value)) {
- continue;
- }
-
- values.push(+value);
- }
- }
-
- if (values.length === 0) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- return values.sort((a, b) => a - b);
+ return getPercentileIncResult(array, kValue);
}
}
diff --git a/packages/engine-formula/src/functions/statistical/quartile-exc/index.ts b/packages/engine-formula/src/functions/statistical/quartile-exc/index.ts
index fc2880f697..3552c87284 100644
--- a/packages/engine-formula/src/functions/statistical/quartile-exc/index.ts
+++ b/packages/engine-formula/src/functions/statistical/quartile-exc/index.ts
@@ -15,12 +15,9 @@
*/
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
-import { ErrorType } from '../../../basics/error-type';
+import type { BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { getArrayValuesByAggregateIgnoreOptions, getQuartileExcResult } from '../../../basics/statistical';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
-import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
export class QuartileExc extends BaseFunction {
@@ -29,7 +26,7 @@ export class QuartileExc extends BaseFunction {
override maxParams = 2;
override calculate(array: BaseValueObject, quart: BaseValueObject): BaseValueObject {
- const arrayValues = this._getValues(array);
+ const arrayValues = getArrayValuesByAggregateIgnoreOptions(array);
if (quart.isArray()) {
const resultArray = (quart as ArrayValueObject).mapValue((quartObject) => this._handleSingleObject(arrayValues, quartObject));
@@ -45,8 +42,8 @@ export class QuartileExc extends BaseFunction {
}
private _handleSingleObject(array: number[] | ErrorValueObject, quart: BaseValueObject): BaseValueObject {
- if (array instanceof ErrorValueObject) {
- return array;
+ if (!Array.isArray(array)) {
+ return array as ErrorValueObject;
}
const { isError, errorObject, variants } = checkVariantsErrorIsStringToNumber(quart);
@@ -56,65 +53,8 @@ export class QuartileExc extends BaseFunction {
}
const [quartObject] = variants as BaseValueObject[];
-
const quartValue = Math.floor(+quartObject.getValue());
- if (quartValue <= 0 || quartValue >= 4) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- const k = quartValue / 4;
- const n = array.length;
-
- if (k < 1 / (n + 1) || k > 1 - 1 / (n + 1)) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- const kIndex = k * (n + 1) - 1;
- const integerPart = Math.floor(kIndex);
- const fractionPart = kIndex - integerPart;
-
- if (fractionPart === 0) {
- return NumberValueObject.create(array[integerPart]);
- }
-
- const result = array[integerPart] + fractionPart * (array[integerPart + 1] - array[integerPart]);
-
- return NumberValueObject.create(result);
- }
-
- private _getValues(array: BaseValueObject): number[] | ErrorValueObject {
- const rowCount = array.isArray() ? (array as ArrayValueObject).getRowCount() : 1;
- const columnCount = array.isArray() ? (array as ArrayValueObject).getColumnCount() : 1;
-
- const values: number[] = [];
-
- for (let r = 0; r < rowCount; r++) {
- for (let c = 0; c < columnCount; c++) {
- const valueObject = array.isArray() ? (array as ArrayValueObject).get(r, c) as BaseValueObject : array;
-
- if (valueObject.isError()) {
- return valueObject as ErrorValueObject;
- }
-
- if (valueObject.isNull() || valueObject.isBoolean()) {
- continue;
- }
-
- const value = valueObject.getValue();
-
- if (!isRealNum(value)) {
- continue;
- }
-
- values.push(+value);
- }
- }
-
- if (values.length === 0) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- return values.sort((a, b) => a - b);
+ return getQuartileExcResult(array, quartValue);
}
}
diff --git a/packages/engine-formula/src/functions/statistical/quartile-inc/index.ts b/packages/engine-formula/src/functions/statistical/quartile-inc/index.ts
index 7ba6a7027f..bece9f33bb 100644
--- a/packages/engine-formula/src/functions/statistical/quartile-inc/index.ts
+++ b/packages/engine-formula/src/functions/statistical/quartile-inc/index.ts
@@ -15,12 +15,9 @@
*/
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
-import { ErrorType } from '../../../basics/error-type';
+import type { BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { getArrayValuesByAggregateIgnoreOptions, getQuartileIncResult } from '../../../basics/statistical';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
-import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
export class QuartileInc extends BaseFunction {
@@ -29,7 +26,7 @@ export class QuartileInc extends BaseFunction {
override maxParams = 2;
override calculate(array: BaseValueObject, quart: BaseValueObject): BaseValueObject {
- const arrayValues = this._getValues(array);
+ const arrayValues = getArrayValuesByAggregateIgnoreOptions(array);
if (quart.isArray()) {
const resultArray = (quart as ArrayValueObject).mapValue((quartObject) => this._handleSingleObject(arrayValues, quartObject));
@@ -45,8 +42,8 @@ export class QuartileInc extends BaseFunction {
}
private _handleSingleObject(array: number[] | ErrorValueObject, quart: BaseValueObject): BaseValueObject {
- if (array instanceof ErrorValueObject) {
- return array;
+ if (!Array.isArray(array)) {
+ return array as ErrorValueObject;
}
const { isError, errorObject, variants } = checkVariantsErrorIsStringToNumber(quart);
@@ -56,61 +53,8 @@ export class QuartileInc extends BaseFunction {
}
const [quartObject] = variants as BaseValueObject[];
-
const quartValue = Math.floor(+quartObject.getValue());
- if (quartValue < 0 || quartValue > 4) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- const k = quartValue / 4;
- const n = array.length;
-
- const kIndex = k * (n - 1);
- const integerPart = Math.floor(kIndex);
- const fractionPart = kIndex - integerPart;
-
- if (fractionPart === 0) {
- return NumberValueObject.create(array[integerPart]);
- }
-
- const result = array[integerPart] + fractionPart * (array[integerPart + 1] - array[integerPart]);
-
- return NumberValueObject.create(result);
- }
-
- private _getValues(array: BaseValueObject): number[] | ErrorValueObject {
- const rowCount = array.isArray() ? (array as ArrayValueObject).getRowCount() : 1;
- const columnCount = array.isArray() ? (array as ArrayValueObject).getColumnCount() : 1;
-
- const values: number[] = [];
-
- for (let r = 0; r < rowCount; r++) {
- for (let c = 0; c < columnCount; c++) {
- const valueObject = array.isArray() ? (array as ArrayValueObject).get(r, c) as BaseValueObject : array;
-
- if (valueObject.isError()) {
- return valueObject as ErrorValueObject;
- }
-
- if (valueObject.isNull() || valueObject.isBoolean()) {
- continue;
- }
-
- const value = valueObject.getValue();
-
- if (!isRealNum(value)) {
- continue;
- }
-
- values.push(+value);
- }
- }
-
- if (values.length === 0) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- return values.sort((a, b) => a - b);
+ return getQuartileIncResult(array, quartValue);
}
}
diff --git a/packages/engine-formula/src/functions/statistical/small/index.ts b/packages/engine-formula/src/functions/statistical/small/index.ts
index 82feec77d8..bb704e65b6 100644
--- a/packages/engine-formula/src/functions/statistical/small/index.ts
+++ b/packages/engine-formula/src/functions/statistical/small/index.ts
@@ -15,12 +15,9 @@
*/
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
-import { isRealNum } from '@univerjs/core';
-import { ErrorType } from '../../../basics/error-type';
+import type { BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
+import { getArrayValuesByAggregateIgnoreOptions, getSmallResult } from '../../../basics/statistical';
import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';
-import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
-import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
export class Small extends BaseFunction {
@@ -29,7 +26,7 @@ export class Small extends BaseFunction {
override maxParams = 2;
override calculate(array: BaseValueObject, k: BaseValueObject): BaseValueObject {
- const arrayValues = this._getValues(array);
+ const arrayValues = getArrayValuesByAggregateIgnoreOptions(array);
if (k.isArray()) {
const resultArray = (k as ArrayValueObject).mapValue((kObject) => this._handleSingleObject(arrayValues, kObject));
@@ -45,8 +42,8 @@ export class Small extends BaseFunction {
}
private _handleSingleObject(array: number[] | ErrorValueObject, k: BaseValueObject): BaseValueObject {
- if (array instanceof ErrorValueObject) {
- return array;
+ if (!Array.isArray(array)) {
+ return array as ErrorValueObject;
}
const { isError, errorObject, variants } = checkVariantsErrorIsStringToNumber(k);
@@ -56,50 +53,8 @@ export class Small extends BaseFunction {
}
const [kObject] = variants as BaseValueObject[];
+ const kValue = +kObject.getValue();
- let kValue = +kObject.getValue();
-
- if (kValue < 1 || kValue > array.length) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- kValue = Math.floor(kValue);
-
- return NumberValueObject.create(array[kValue - 1]);
- }
-
- private _getValues(array: BaseValueObject): number[] | ErrorValueObject {
- const rowCount = array.isArray() ? (array as ArrayValueObject).getRowCount() : 1;
- const columnCount = array.isArray() ? (array as ArrayValueObject).getColumnCount() : 1;
-
- const values: number[] = [];
-
- for (let r = 0; r < rowCount; r++) {
- for (let c = 0; c < columnCount; c++) {
- const valueObject = array.isArray() ? (array as ArrayValueObject).get(r, c) as BaseValueObject : array;
-
- if (valueObject.isError()) {
- return valueObject as ErrorValueObject;
- }
-
- if (valueObject.isNull() || valueObject.isBoolean() || valueObject.isString()) {
- continue;
- }
-
- const value = valueObject.getValue();
-
- if (!isRealNum(value)) {
- continue;
- }
-
- values.push(+value);
- }
- }
-
- if (values.length === 0) {
- return ErrorValueObject.create(ErrorType.NUM);
- }
-
- return values.sort((a, b) => a - b);
+ return getSmallResult(array, kValue);
}
}
diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/ca-ES.ts b/packages/sheets-formula-ui/src/locale/function-list/math/ca-ES.ts
index fb017a70d4..5dcc68472e 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/math/ca-ES.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/math/ca-ES.ts
@@ -95,8 +95,10 @@ const locale: typeof enUS = {
},
],
functionParameter: {
- number1: { name: 'nombre1', detail: 'primer' },
- number2: { name: 'nombre2', detail: 'segon' },
+ functionNum: { name: 'núm_función', detail: 'Un número de 1 a 19 que especifica la función que se usará.' },
+ options: { name: 'opciones', detail: 'Un valor numérico que determina qué valores del rango de evaluación de la función se omitirán.' },
+ ref1: { name: 'ref1', detail: 'El primer argumento numérico para las funciones que tienen varios argumentos numéricos de los que desea obtener el valor agregado.' },
+ ref2: { name: 'ref2', detail: 'Argumentos numéricos 2 a 252 cuyo valor agregado desea obtener.' },
},
},
ARABIC: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/en-US.ts b/packages/sheets-formula-ui/src/locale/function-list/math/en-US.ts
index 0d38884427..8b7161a4c2 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/math/en-US.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/math/en-US.ts
@@ -93,8 +93,10 @@ const locale = {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ functionNum: { name: 'function_num', detail: 'A number 1 to 19 that specifies which function to use.' },
+ options: { name: 'options', detail: 'A numerical value that determines which values to ignore in the evaluation range for the function.' },
+ ref1: { name: 'ref1', detail: 'The first numeric argument for functions that take multiple numeric arguments for which you want the aggregate value.' },
+ ref2: { name: 'ref2', detail: 'Numeric arguments 2 to 252 for which you want the aggregate value.' },
},
},
ARABIC: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/es-ES.ts b/packages/sheets-formula-ui/src/locale/function-list/math/es-ES.ts
index a651f0497c..b2bad3cc67 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/math/es-ES.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/math/es-ES.ts
@@ -95,8 +95,10 @@ const locale: typeof enUS = {
},
],
functionParameter: {
- number1: { name: 'número1', detail: 'primero' },
- number2: { name: 'número2', detail: 'segundo' },
+ functionNum: { name: 'núm_función', detail: 'Un número de 1 a 19 que especifica la función que se usará.' },
+ options: { name: 'opciones', detail: 'Un valor numérico que determina qué valores del rango de evaluación de la función se omitirán.' },
+ ref1: { name: 'ref1', detail: 'El primer argumento numérico para las funciones que tienen varios argumentos numéricos de los que desea obtener el valor agregado.' },
+ ref2: { name: 'ref2', detail: 'Argumentos numéricos 2 a 252 cuyo valor agregado desea obtener.' },
},
},
ARABIC: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/ja-JP.ts b/packages/sheets-formula-ui/src/locale/function-list/math/ja-JP.ts
index d952541cdb..a79f142b48 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/math/ja-JP.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/math/ja-JP.ts
@@ -95,8 +95,10 @@ const locale: typeof enUS = {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ functionNum: { name: '集計方法', detail: '使用する関数を指定する 1 ~ 19 の番号です。' },
+ options: { name: 'オプション', detail: '関数の検証範囲内の無視する値を指定する数値です。' },
+ ref1: { name: '範囲 1', detail: '集計値を求めるために複数の数値引数を受け取る関数の、最初の数値引数です。' },
+ ref2: { name: '範囲 2', detail: '集計値を求めるための数値引数 2 ~ 252 を指定します。' },
},
},
ARABIC: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/ko-KR.ts b/packages/sheets-formula-ui/src/locale/function-list/math/ko-KR.ts
index d44d83db0a..da38cad705 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/math/ko-KR.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/math/ko-KR.ts
@@ -95,8 +95,10 @@ const locale: typeof enUS = {
},
],
functionParameter: {
- number1: { name: 'number1', detail: '첫 번째' },
- number2: { name: 'number2', detail: '두 번째' },
+ functionNum: { name: 'function_num', detail: '사용할 함수를 지정하는 1에서 19 사이의 숫자입니다.' },
+ options: { name: 'options', detail: '함수의 계산 범위에서 무시할 값을 결정하는 숫자 값입니다.' },
+ ref1: { name: 'ref1', detail: '집계 값을 구할 인수가 여러 개인 함수의 첫 번째 인수입니다.' },
+ ref2: { name: 'ref2', detail: '집계 값을 구할 인수로, 2개에서 252개까지 사용할 수 있습니다.' },
},
},
ARABIC: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/ru-RU.ts b/packages/sheets-formula-ui/src/locale/function-list/math/ru-RU.ts
index cf98d30074..a559ffc648 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/math/ru-RU.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/math/ru-RU.ts
@@ -95,8 +95,10 @@ const locale: typeof enUS = {
},
],
functionParameter: {
- number1: { name: 'число1', detail: 'первое' },
- number2: { name: 'числое2', detail: 'второе' },
+ functionNum: { name: 'номер_функции', detail: 'Число от 1 до 19, определяющее функцию, которую необходимо использовать.' },
+ options: { name: 'параметры', detail: 'Числовое значение, определяющее, какие значения при вычислении функции следует пропускать.' },
+ ref1: { name: 'ссылка1', detail: 'Первый числовой аргумент для функций, которые принимают несколько числовых аргументов, для которых требуется агрегированное значение.' },
+ ref2: { name: 'ссылка2', detail: 'Числовые аргументы от 2 до 252, для которых необходимо вычислить агрегатное значение.' },
},
},
ARABIC: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/vi-VN.ts b/packages/sheets-formula-ui/src/locale/function-list/math/vi-VN.ts
index 21da2993f5..1f0b4af847 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/math/vi-VN.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/math/vi-VN.ts
@@ -83,17 +83,19 @@ const locale: typeof enUS = {
},
},
AGGREGATE: {
- description: 'Returns an aggregate in a list or database',
- abstract: 'Returns an aggregate in a list or database',
+ description: 'Trả về tổng gộp trong một danh sách hoặc một cơ sở dữ liệu.',
+ abstract: 'Trả về tổng gộp trong một danh sách hoặc một cơ sở dữ liệu.',
links: [
{
- title: 'Instruction',
- url: 'https://support.microsoft.com/en-us/office/aggregate-function-43b9278e-6aa7-4f17-92b6-e19993fa26df',
+ title: 'Hướng dẫn',
+ url: 'https://support.microsoft.com/vi-vn/office/aggregate-function-43b9278e-6aa7-4f17-92b6-e19993fa26df',
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ functionNum: { name: 'function_num', detail: 'Một số từ 1 đến 19 chỉ rõ hàm cần dùng.' },
+ options: { name: 'options', detail: 'Một giá trị số xác định những giá trị nào cần bỏ qua trong phạm vi định trị của hàm.' },
+ ref1: { name: 'ref1', detail: 'Đối số dạng số thứ nhất cho những hàm có nhiều đối số dạng số mà bạn muốn có giá trị tổng gộp.' },
+ ref2: { name: 'ref2', detail: 'Đối số dạng số 2 đến 252 mà bạn muốn có giá trị tổng gộp.' },
},
},
ARABIC: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/zh-CN.ts b/packages/sheets-formula-ui/src/locale/function-list/math/zh-CN.ts
index 1c88b5ff7d..7f4034bd29 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/math/zh-CN.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/math/zh-CN.ts
@@ -94,8 +94,10 @@ const locale: typeof enUS = {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ functionNum: { name: '函数编号', detail: '一个介于 1 到 19 之间的数字,指定要使用的函数。' },
+ options: { name: '选项', detail: '一个数值,决定在函数的计算区域内要忽略哪些值。' },
+ ref1: { name: '引用1', detail: '函数的第一个数值参数,这些函数具有要计算聚合值的多个数值参数。' },
+ ref2: { name: '引用2', detail: '要计算聚合值的 2 至 252 个数值参数。' },
},
},
ARABIC: {
diff --git a/packages/sheets-formula-ui/src/locale/function-list/math/zh-TW.ts b/packages/sheets-formula-ui/src/locale/function-list/math/zh-TW.ts
index 88fa0195bf..7df0e11a30 100644
--- a/packages/sheets-formula-ui/src/locale/function-list/math/zh-TW.ts
+++ b/packages/sheets-formula-ui/src/locale/function-list/math/zh-TW.ts
@@ -94,8 +94,10 @@ const locale: typeof enUS = {
},
],
functionParameter: {
- number1: { name: 'number1', detail: 'first' },
- number2: { name: 'number2', detail: 'second' },
+ functionNum: { name: 'function_num', detail: '數字 1 到 19,指定要使用哪一個函數。' },
+ options: { name: 'options', detail: '一個數值,決定函數的評估範圍中要忽略哪些值。' },
+ ref1: { name: 'ref1', detail: '接受多個數值引數之函數的第一個數值引數,這是您要計算彙總值的引數。' },
+ ref2: { name: 'ref2', detail: '這是您要計算彙總值的第 2 個到第 253 個數值引數。' },
},
},
ARABIC: {
diff --git a/packages/sheets-formula/src/services/function-list/math.ts b/packages/sheets-formula/src/services/function-list/math.ts
index 25b7f38a7d..8e940d07f4 100644
--- a/packages/sheets-formula/src/services/function-list/math.ts
+++ b/packages/sheets-formula/src/services/function-list/math.ts
@@ -100,19 +100,33 @@ export const FUNCTION_LIST_MATH: IFunctionInfo[] = [
abstract: 'formula.functionList.AGGREGATE.abstract',
functionParameter: [
{
- name: 'formula.functionList.AGGREGATE.functionParameter.number1.name',
- detail: 'formula.functionList.AGGREGATE.functionParameter.number1.detail',
- example: 'A1:A20',
+ name: 'formula.functionList.AGGREGATE.functionParameter.functionNum.name',
+ detail: 'formula.functionList.AGGREGATE.functionParameter.functionNum.detail',
+ example: '1',
+ require: 1,
+ repeat: 0,
+ },
+ {
+ name: 'formula.functionList.AGGREGATE.functionParameter.options.name',
+ detail: 'formula.functionList.AGGREGATE.functionParameter.options.detail',
+ example: '6',
require: 1,
repeat: 0,
},
{
- name: 'formula.functionList.AGGREGATE.functionParameter.number2.name',
- detail: 'formula.functionList.AGGREGATE.functionParameter.number2.detail',
+ name: 'formula.functionList.AGGREGATE.functionParameter.ref1.name',
+ detail: 'formula.functionList.AGGREGATE.functionParameter.ref1.detail',
example: 'A1:A20',
require: 1,
repeat: 0,
},
+ {
+ name: 'formula.functionList.AGGREGATE.functionParameter.ref2.name',
+ detail: 'formula.functionList.AGGREGATE.functionParameter.ref2.detail',
+ example: 'B1:B20',
+ require: 0,
+ repeat: 1,
+ },
],
},
{
diff --git a/packages/sheets/src/controllers/__tests__/formula/aggregate.spec.ts b/packages/sheets/src/controllers/__tests__/formula/aggregate.spec.ts
new file mode 100644
index 0000000000..a776848de9
--- /dev/null
+++ b/packages/sheets/src/controllers/__tests__/formula/aggregate.spec.ts
@@ -0,0 +1,475 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 { CellValue, Ctor, Injector, IWorkbookData, Nullable, Worksheet } from '@univerjs/core';
+import type { BaseFunction, IFunctionNames } from '@univerjs/engine-formula';
+import type { FFormula } from '@univerjs/engine-formula/facade';
+import { ICommandService, LocaleType } from '@univerjs/core';
+import { functionLogical, functionLookup, functionMath, functionMeta, functionStatistical, IFormulaCurrentConfigService, IFormulaRuntimeService, IFunctionService, SetArrayFormulaDataMutation, SetFormulaCalculationNotificationMutation, SetFormulaCalculationResultMutation, SetFormulaCalculationStartMutation, SetFormulaCalculationStopMutation } from '@univerjs/engine-formula';
+import { beforeEach, describe, expect, it } from 'vitest';
+import { SetRangeValuesMutation } from '../../../commands/mutations/set-range-values.mutation';
+import { createFunctionTestBed } from './create-function-test-bed';
+
+import '@univerjs/engine-formula/facade';
+
+const unitId = 'test';
+const subUnitId = 'sheet1';
+
+const getFunctionsTestWorkbookData = (): IWorkbookData => {
+ return {
+ id: unitId,
+ appVersion: '3.0.0-alpha',
+ styles: {},
+ sheets: {
+ sheet1: {
+ id: subUnitId,
+ cellData: {
+ 0: {
+ 0: {
+ v: '1st',
+ t: 1,
+ },
+ 1: {
+ v: '2nd',
+ t: 1,
+ },
+ 6: {
+ v: '1st',
+ t: 1,
+ },
+ 7: {
+ v: 'Occurances',
+ t: 1,
+ },
+ 8: {
+ v: '2nd ->',
+ t: 1,
+ },
+ },
+ 1: {
+ 0: {
+ v: '1',
+ t: 2,
+ },
+ 1: {
+ v: 'a',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ 6: {
+ v: 1,
+ t: 2,
+ },
+ 8: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G2,ROW($B$2:$B$13)),1)),"")',
+ },
+ 9: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G2,ROW($B$2:$B$13)),2)),"")',
+ },
+ 10: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G2,ROW($B$2:$B$13)),3)),"")',
+ },
+ 11: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G2,ROW($B$2:$B$13)),4)),"")',
+ },
+ 12: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G2,ROW($B$2:$B$13)),5)),"")',
+ },
+ 13: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G2,ROW($B$2:$B$13)),6)),"")',
+ },
+ 14: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G2,ROW($B$2:$B$13)),7)),"")',
+ },
+ },
+ 2: {
+ 0: {
+ v: '1',
+ t: 2,
+ },
+ 1: {
+ v: 'b',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ 6: {
+ v: 2,
+ t: 2,
+ },
+ 8: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G3,ROW($B$2:$B$13)),1)),"")',
+ },
+ 9: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G3,ROW($B$2:$B$13)),2)),"")',
+ },
+ 10: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G3,ROW($B$2:$B$13)),3)),"")',
+ },
+ 11: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G3,ROW($B$2:$B$13)),4)),"")',
+ },
+ 12: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G3,ROW($B$2:$B$13)),5)),"")',
+ },
+ 13: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G3,ROW($B$2:$B$13)),6)),"")',
+ },
+ 14: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G3,ROW($B$2:$B$13)),7)),"")',
+ },
+ },
+ 3: {
+ 0: {
+ v: '1',
+ t: 2,
+ },
+ 1: {
+ v: 'c',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ 6: {
+ v: 3,
+ t: 2,
+ },
+ 8: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G4,ROW($B$2:$B$13)),1)),"")',
+ },
+ 9: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G4,ROW($B$2:$B$13)),2)),"")',
+ },
+ 10: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G4,ROW($B$2:$B$13)),3)),"")',
+ },
+ 11: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G4,ROW($B$2:$B$13)),4)),"")',
+ },
+ 12: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G4,ROW($B$2:$B$13)),5)),"")',
+ },
+ 13: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G4,ROW($B$2:$B$13)),6)),"")',
+ },
+ 14: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G4,ROW($B$2:$B$13)),7)),"")',
+ },
+ },
+ 4: {
+ 0: {
+ v: '2',
+ t: 2,
+ },
+ 1: {
+ v: 'x',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ 8: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G5,ROW($B$2:$B$13)),1)),"")',
+ },
+ 9: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G5,ROW($B$2:$B$13)),2)),"")',
+ },
+ 10: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G5,ROW($B$2:$B$13)),3)),"")',
+ },
+ 11: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G5,ROW($B$2:$B$13)),4)),"")',
+ },
+ 12: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G5,ROW($B$2:$B$13)),5)),"")',
+ },
+ 13: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G5,ROW($B$2:$B$13)),6)),"")',
+ },
+ 14: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G5,ROW($B$2:$B$13)),7)),"")',
+ },
+ },
+ 5: {
+ 0: {
+ v: '2',
+ t: 2,
+ },
+ 1: {
+ v: 'w',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ },
+ 6: {
+ 0: {
+ v: '2',
+ t: 2,
+ },
+ 1: {
+ v: 'y',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ 8: {
+ f: '=INDEX($B$2:$B$13,1)',
+ },
+ },
+ 7: {
+ 0: {
+ v: '2',
+ t: 2,
+ },
+ 1: {
+ v: 'z',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ 8: {
+ f: '=INDEX($B:$B,2)',
+ },
+ },
+ 8: {
+ 0: {
+ v: '3',
+ t: 2,
+ },
+ 1: {
+ v: 'aa',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ },
+ 9: {
+ 0: {
+ v: '3',
+ t: 2,
+ },
+ 1: {
+ v: 'ab',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ 8: {
+ f: '=AGGREGATE(15,6,ROW($B$2:$B$13)/($A$2:$A$13=$G2),1)',
+ },
+ },
+ 10: {
+ 0: {
+ v: '3',
+ t: 2,
+ },
+ 1: {
+ v: 'ac',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ },
+ 11: {
+ 0: {
+ v: '3',
+ t: 2,
+ },
+ 1: {
+ v: 'ad',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ 8: {
+ f: '=IFERROR(INDEX($B:$B,SMALL(IF($A$2:$A$13=$G2,ROW($B$2:$B$13)),1)),"")',
+ },
+ },
+ 12: {
+ 0: {
+ v: '3',
+ t: 2,
+ },
+ 1: {
+ v: 'ae',
+ t: 1,
+ },
+ 2: {
+ v: '1',
+ t: 2,
+ },
+ },
+ },
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ };
+};
+
+describe('Test AGGREGATE formula', () => {
+ let get: Injector['get'];
+ let worksheet: Worksheet;
+ let formulaEngine: FFormula;
+ let commandService: ICommandService;
+ let getCellValue: (row: number, column: number) => Nullable<CellValue>;
+
+ beforeEach(async () => {
+ const testBed = createFunctionTestBed(getFunctionsTestWorkbookData());
+
+ get = testBed.get;
+ worksheet = testBed.sheet.getSheetBySheetId(subUnitId) as Worksheet;
+ formulaEngine = testBed.api.getFormula() as FFormula;
+
+ commandService = get(ICommandService);
+
+ commandService.registerCommand(SetFormulaCalculationStartMutation);
+ commandService.registerCommand(SetFormulaCalculationStopMutation);
+ commandService.registerCommand(SetFormulaCalculationResultMutation);
+ commandService.registerCommand(SetFormulaCalculationNotificationMutation);
+ commandService.registerCommand(SetArrayFormulaDataMutation);
+ commandService.registerCommand(SetRangeValuesMutation);
+
+ const functionService = get(IFunctionService);
+
+ const formulaCurrentConfigService = get(IFormulaCurrentConfigService);
+
+ const formulaRuntimeService = get(IFormulaRuntimeService);
+
+ formulaCurrentConfigService.load({
+ formulaData: {},
+ arrayFormulaCellData: {},
+ arrayFormulaRange: {},
+ forceCalculate: false,
+ dirtyRanges: [],
+ dirtyNameMap: {},
+ dirtyDefinedNameMap: {},
+ dirtyUnitFeatureMap: {},
+ dirtyUnitOtherFormulaMap: {},
+ excludedCell: {},
+ allUnitData: {
+ [testBed.unitId]: testBed.sheetData,
+ },
+ });
+
+ const sheetItem = testBed.sheetData[testBed.sheetId];
+
+ formulaRuntimeService.setCurrent(
+ 0,
+ 0,
+ sheetItem.rowCount,
+ sheetItem.columnCount,
+ testBed.sheetId,
+ testBed.unitId
+ );
+
+ const functions = [
+ ...functionMeta,
+ ...functionMath,
+ ...functionStatistical,
+ ...functionLookup,
+ ...functionLogical,
+ ]
+ .map((registerObject) => {
+ const Func = registerObject[0] as Ctor<BaseFunction>;
+ const name = registerObject[1] as IFunctionNames;
+
+ return new Func(name);
+ });
+
+ functionService.registerExecutors(
+ ...functions
+ );
+
+ formulaEngine.executeCalculation();
+ await formulaEngine.onCalculationEnd();
+
+ getCellValue = (row: number, column: number) => {
+ return worksheet.getCellRaw(row, column)?.v;
+ };
+ });
+
+ describe('Test formula', () => {
+ it('Test IFERROR nested INDEX,SMALL,IF,ROW', () => {
+ expect(getCellValue(1, 8)).toBe('a');
+ expect(getCellValue(1, 9)).toBe('b');
+ expect(getCellValue(1, 10)).toBe('c');
+ expect(getCellValue(1, 11)).toBe('');
+ expect(getCellValue(1, 12)).toBe('');
+ expect(getCellValue(1, 13)).toBe('');
+ expect(getCellValue(1, 14)).toBe('');
+
+ expect(getCellValue(2, 8)).toBe('x');
+ expect(getCellValue(2, 9)).toBe('w');
+ expect(getCellValue(2, 10)).toBe('y');
+ expect(getCellValue(2, 11)).toBe('z');
+ expect(getCellValue(2, 12)).toBe('');
+ expect(getCellValue(2, 13)).toBe('');
+ expect(getCellValue(2, 14)).toBe('');
+
+ expect(getCellValue(3, 8)).toBe('aa');
+ expect(getCellValue(3, 9)).toBe('ab');
+ expect(getCellValue(3, 10)).toBe('ac');
+ expect(getCellValue(3, 11)).toBe('ad');
+ expect(getCellValue(3, 12)).toBe('ae');
+ expect(getCellValue(3, 13)).toBe('');
+ expect(getCellValue(3, 14)).toBe('');
+
+ expect(getCellValue(4, 8)).toBe('');
+ expect(getCellValue(4, 9)).toBe('');
+ expect(getCellValue(4, 10)).toBe('');
+ expect(getCellValue(4, 11)).toBe('');
+ expect(getCellValue(4, 12)).toBe('');
+ expect(getCellValue(4, 13)).toBe('');
+ expect(getCellValue(4, 14)).toBe('');
+
+ expect(getCellValue(11, 8)).toBe('a');
+ });
+
+ it('Test INDEX', () => {
+ expect(getCellValue(6, 8)).toBe('a');
+ expect(getCellValue(7, 8)).toBe('a');
+ });
+
+ it('Test AGGREGATE nested ROW', () => {
+ expect(getCellValue(9, 8)).toBe(2);
+ });
+ });
+});
diff --git a/packages/sheets/src/controllers/__tests__/formula/aggregate2.spec.ts b/packages/sheets/src/controllers/__tests__/formula/aggregate2.spec.ts
new file mode 100644
index 0000000000..be164019e6
--- /dev/null
+++ b/packages/sheets/src/controllers/__tests__/formula/aggregate2.spec.ts
@@ -0,0 +1,3452 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 { CellValue, Ctor, Injector, IWorkbookData, Nullable, Worksheet } from '@univerjs/core';
+import type { BaseFunction, IFunctionNames } from '@univerjs/engine-formula';
+import type { FFormula } from '@univerjs/engine-formula/facade';
+import { ICommandService, LocaleType } from '@univerjs/core';
+import { functionLogical, functionLookup, functionMath, functionMeta, functionStatistical, IFormulaCurrentConfigService, IFormulaRuntimeService, IFunctionService, SetArrayFormulaDataMutation, SetFormulaCalculationNotificationMutation, SetFormulaCalculationResultMutation, SetFormulaCalculationStartMutation, SetFormulaCalculationStopMutation } from '@univerjs/engine-formula';
+import { beforeEach, describe, expect, it } from 'vitest';
+import { SetRangeValuesMutation } from '../../../commands/mutations/set-range-values.mutation';
+import { createFunctionTestBed } from './create-function-test-bed';
+
+import '@univerjs/engine-formula/facade';
+
+const unitId = 'test';
+const subUnitId = 'sheet1';
+
+const getFunctionsTestWorkbookData = (): IWorkbookData => {
+ return {
+ id: unitId,
+ appVersion: '3.0.0-alpha',
+ styles: {
+ R3: {
+ n: {
+ pattern: 'YYYY-M-D',
+ },
+ },
+ },
+ sheets: {
+ sheet1: {
+ id: subUnitId,
+ cellData: {
+ 0: {
+ 0: {
+ v: 'S/N',
+ t: 1,
+ },
+ 1: {
+ v: 'Start 1',
+ t: 1,
+ },
+ 2: {
+ v: 'Start 2',
+ t: 1,
+ },
+ 3: {
+ v: 'D/U',
+ t: 1,
+ },
+ 4: {
+ v: 'Outcome 1',
+ t: 1,
+ },
+ 5: {
+ v: 'Outcome 2',
+ t: 1,
+ },
+ },
+ 1: {
+ 0: {
+ v: '1',
+ t: 2,
+ },
+ 1: {
+ v: '43831',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43834',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E2)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F2)),"")',
+ },
+ },
+ 2: {
+ 0: {
+ v: '2',
+ t: 2,
+ },
+ 1: {
+ v: '43832',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43834',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E3)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F3)),"")',
+ },
+ },
+ 3: {
+ 0: {
+ v: '3',
+ t: 2,
+ },
+ 1: {
+ v: '43833',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43834',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E4)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F4)),"")',
+ },
+ },
+ 4: {
+ 0: {
+ v: '4',
+ t: 2,
+ },
+ 1: {
+ v: '43834',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43834',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E5)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F5)),"")',
+ },
+ },
+ 5: {
+ 0: {
+ v: '5',
+ t: 2,
+ },
+ 1: {
+ v: '43835',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E6)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F6)),"")',
+ },
+ },
+ 6: {
+ 0: {
+ v: '6',
+ t: 2,
+ },
+ 1: {
+ v: '43836',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E7)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F7)),"")',
+ },
+ },
+ 7: {
+ 0: {
+ v: '7',
+ t: 2,
+ },
+ 1: {
+ v: '43837',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E8)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F8)),"")',
+ },
+ },
+ 8: {
+ 0: {
+ v: '8',
+ t: 2,
+ },
+ 1: {
+ v: '43838',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E9)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F9)),"")',
+ },
+ },
+ 9: {
+ 0: {
+ v: '9',
+ t: 2,
+ },
+ 1: {
+ v: '43839',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E10)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F10)),"")',
+ },
+ },
+ 10: {
+ 0: {
+ v: '10',
+ t: 2,
+ },
+ 1: {
+ v: '43840',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E11)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F11)),"")',
+ },
+ },
+ 11: {
+ 0: {
+ v: '11',
+ t: 2,
+ },
+ 1: {
+ v: '43841',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E12)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F12)),"")',
+ },
+ },
+ 12: {
+ 0: {
+ v: '12',
+ t: 2,
+ },
+ 1: {
+ v: '43842',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E13)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F13)),"")',
+ },
+ },
+ 13: {
+ 0: {
+ v: '13',
+ t: 2,
+ },
+ 1: {
+ v: '43843',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E14)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F14)),"")',
+ },
+ },
+ 14: {
+ 0: {
+ v: '14',
+ t: 2,
+ },
+ 1: {
+ v: '43844',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(AGGREGATE(15,6,B$2:B$2193/($D$2:$D$2193="U"),ROWS(E$2:E15)),"")',
+ },
+ 5: {
+ f: '=IFERROR(AGGREGATE(15,6,C$2:C$2193/($D$2:$D$2193="U"),ROWS(F$2:F15)),"")',
+ },
+ },
+ 15: {
+ 0: {
+ v: '15',
+ t: 2,
+ },
+ 1: {
+ v: '43845',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 16: {
+ 0: {
+ v: '16',
+ t: 2,
+ },
+ 1: {
+ v: '43846',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 17: {
+ 0: {
+ v: '17',
+ t: 2,
+ },
+ 1: {
+ v: '43847',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 18: {
+ 0: {
+ v: '18',
+ t: 2,
+ },
+ 1: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43848',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 19: {
+ 0: {
+ v: '19',
+ t: 2,
+ },
+ 1: {
+ v: '43849',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 20: {
+ 0: {
+ v: '20',
+ t: 2,
+ },
+ 1: {
+ v: '43850',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 21: {
+ 0: {
+ v: '21',
+ t: 2,
+ },
+ 1: {
+ v: '43851',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 22: {
+ 0: {
+ v: '22',
+ t: 2,
+ },
+ 1: {
+ v: '43852',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 23: {
+ 0: {
+ v: '23',
+ t: 2,
+ },
+ 1: {
+ v: '43853',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 24: {
+ 0: {
+ v: '24',
+ t: 2,
+ },
+ 1: {
+ v: '43854',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 25: {
+ 0: {
+ v: '25',
+ t: 2,
+ },
+ 1: {
+ v: '43855',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 26: {
+ 0: {
+ v: '26',
+ t: 2,
+ },
+ 1: {
+ v: '43856',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 27: {
+ 0: {
+ v: '27',
+ t: 2,
+ },
+ 1: {
+ v: '43857',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 28: {
+ 0: {
+ v: '28',
+ t: 2,
+ },
+ 1: {
+ v: '43858',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 29: {
+ 0: {
+ v: '29',
+ t: 2,
+ },
+ 1: {
+ v: '43859',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 30: {
+ 0: {
+ v: '30',
+ t: 2,
+ },
+ 1: {
+ v: '43860',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 31: {
+ 0: {
+ v: '31',
+ t: 2,
+ },
+ 1: {
+ v: '43861',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 32: {
+ 0: {
+ v: '32',
+ t: 2,
+ },
+ 1: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43862',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 33: {
+ 0: {
+ v: '33',
+ t: 2,
+ },
+ 1: {
+ v: '43863',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 34: {
+ 0: {
+ v: '34',
+ t: 2,
+ },
+ 1: {
+ v: '43864',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 35: {
+ 0: {
+ v: '35',
+ t: 2,
+ },
+ 1: {
+ v: '43865',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 36: {
+ 0: {
+ v: '36',
+ t: 2,
+ },
+ 1: {
+ v: '43866',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 37: {
+ 0: {
+ v: '37',
+ t: 2,
+ },
+ 1: {
+ v: '43867',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 38: {
+ 0: {
+ v: '38',
+ t: 2,
+ },
+ 1: {
+ v: '43868',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 39: {
+ 0: {
+ v: '39',
+ t: 2,
+ },
+ 1: {
+ v: '43869',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 40: {
+ 0: {
+ v: '40',
+ t: 2,
+ },
+ 1: {
+ v: '43870',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 41: {
+ 0: {
+ v: '41',
+ t: 2,
+ },
+ 1: {
+ v: '43871',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 42: {
+ 0: {
+ v: '42',
+ t: 2,
+ },
+ 1: {
+ v: '43872',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 43: {
+ 0: {
+ v: '43',
+ t: 2,
+ },
+ 1: {
+ v: '43873',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 44: {
+ 0: {
+ v: '44',
+ t: 2,
+ },
+ 1: {
+ v: '43874',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 45: {
+ 0: {
+ v: '45',
+ t: 2,
+ },
+ 1: {
+ v: '43875',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 46: {
+ 0: {
+ v: '46',
+ t: 2,
+ },
+ 1: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43876',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 47: {
+ 0: {
+ v: '47',
+ t: 2,
+ },
+ 1: {
+ v: '43877',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 48: {
+ 0: {
+ v: '48',
+ t: 2,
+ },
+ 1: {
+ v: '43878',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 49: {
+ 0: {
+ v: '49',
+ t: 2,
+ },
+ 1: {
+ v: '43879',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 50: {
+ 0: {
+ v: '50',
+ t: 2,
+ },
+ 1: {
+ v: '43880',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 51: {
+ 0: {
+ v: '51',
+ t: 2,
+ },
+ 1: {
+ v: '43881',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 52: {
+ 0: {
+ v: '52',
+ t: 2,
+ },
+ 1: {
+ v: '43882',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 53: {
+ 0: {
+ v: '53',
+ t: 2,
+ },
+ 1: {
+ v: '43883',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 54: {
+ 0: {
+ v: '54',
+ t: 2,
+ },
+ 1: {
+ v: '43884',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 55: {
+ 0: {
+ v: '55',
+ t: 2,
+ },
+ 1: {
+ v: '43885',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 56: {
+ 0: {
+ v: '56',
+ t: 2,
+ },
+ 1: {
+ v: '43886',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 57: {
+ 0: {
+ v: '57',
+ t: 2,
+ },
+ 1: {
+ v: '43887',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 58: {
+ 0: {
+ v: '58',
+ t: 2,
+ },
+ 1: {
+ v: '43888',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 59: {
+ 0: {
+ v: '59',
+ t: 2,
+ },
+ 1: {
+ v: '43889',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 60: {
+ 0: {
+ v: '60',
+ t: 2,
+ },
+ 1: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43890',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 61: {
+ 0: {
+ v: '61',
+ t: 2,
+ },
+ 1: {
+ v: '43891',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 62: {
+ 0: {
+ v: '62',
+ t: 2,
+ },
+ 1: {
+ v: '43892',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 63: {
+ 0: {
+ v: '63',
+ t: 2,
+ },
+ 1: {
+ v: '43893',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 64: {
+ 0: {
+ v: '64',
+ t: 2,
+ },
+ 1: {
+ v: '43894',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 65: {
+ 0: {
+ v: '65',
+ t: 2,
+ },
+ 1: {
+ v: '43895',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 66: {
+ 0: {
+ v: '66',
+ t: 2,
+ },
+ 1: {
+ v: '43896',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 67: {
+ 0: {
+ v: '67',
+ t: 2,
+ },
+ 1: {
+ v: '43897',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 68: {
+ 0: {
+ v: '68',
+ t: 2,
+ },
+ 1: {
+ v: '43898',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 69: {
+ 0: {
+ v: '69',
+ t: 2,
+ },
+ 1: {
+ v: '43899',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 70: {
+ 0: {
+ v: '70',
+ t: 2,
+ },
+ 1: {
+ v: '43900',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 71: {
+ 0: {
+ v: '71',
+ t: 2,
+ },
+ 1: {
+ v: '43901',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 72: {
+ 0: {
+ v: '72',
+ t: 2,
+ },
+ 1: {
+ v: '43902',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 73: {
+ 0: {
+ v: '73',
+ t: 2,
+ },
+ 1: {
+ v: '43903',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 74: {
+ 0: {
+ v: '74',
+ t: 2,
+ },
+ 1: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43904',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 75: {
+ 0: {
+ v: '75',
+ t: 2,
+ },
+ 1: {
+ v: '43905',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 76: {
+ 0: {
+ v: '76',
+ t: 2,
+ },
+ 1: {
+ v: '43906',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 77: {
+ 0: {
+ v: '77',
+ t: 2,
+ },
+ 1: {
+ v: '43907',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 78: {
+ 0: {
+ v: '78',
+ t: 2,
+ },
+ 1: {
+ v: '43908',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 79: {
+ 0: {
+ v: '79',
+ t: 2,
+ },
+ 1: {
+ v: '43909',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 80: {
+ 0: {
+ v: '80',
+ t: 2,
+ },
+ 1: {
+ v: '43910',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 81: {
+ 0: {
+ v: '81',
+ t: 2,
+ },
+ 1: {
+ v: '43911',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 82: {
+ 0: {
+ v: '82',
+ t: 2,
+ },
+ 1: {
+ v: '43912',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 83: {
+ 0: {
+ v: '83',
+ t: 2,
+ },
+ 1: {
+ v: '43913',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 84: {
+ 0: {
+ v: '84',
+ t: 2,
+ },
+ 1: {
+ v: '43914',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 85: {
+ 0: {
+ v: '85',
+ t: 2,
+ },
+ 1: {
+ v: '43915',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 86: {
+ 0: {
+ v: '86',
+ t: 2,
+ },
+ 1: {
+ v: '43916',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 87: {
+ 0: {
+ v: '87',
+ t: 2,
+ },
+ 1: {
+ v: '43917',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 88: {
+ 0: {
+ v: '88',
+ t: 2,
+ },
+ 1: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43918',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 89: {
+ 0: {
+ v: '89',
+ t: 2,
+ },
+ 1: {
+ v: '43919',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 90: {
+ 0: {
+ v: '90',
+ t: 2,
+ },
+ 1: {
+ v: '43920',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 91: {
+ 0: {
+ v: '91',
+ t: 2,
+ },
+ 1: {
+ v: '43921',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 92: {
+ 0: {
+ v: '92',
+ t: 2,
+ },
+ 1: {
+ v: '43922',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 93: {
+ 0: {
+ v: '93',
+ t: 2,
+ },
+ 1: {
+ v: '43923',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 94: {
+ 0: {
+ v: '94',
+ t: 2,
+ },
+ 1: {
+ v: '43924',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 95: {
+ 0: {
+ v: '95',
+ t: 2,
+ },
+ 1: {
+ v: '43925',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 96: {
+ 0: {
+ v: '96',
+ t: 2,
+ },
+ 1: {
+ v: '43926',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 97: {
+ 0: {
+ v: '97',
+ t: 2,
+ },
+ 1: {
+ v: '43927',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 98: {
+ 0: {
+ v: '98',
+ t: 2,
+ },
+ 1: {
+ v: '43928',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 99: {
+ 0: {
+ v: '99',
+ t: 2,
+ },
+ 1: {
+ v: '43929',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 100: {
+ 0: {
+ v: '100',
+ t: 2,
+ },
+ 1: {
+ v: '43930',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 101: {
+ 0: {
+ v: '101',
+ t: 2,
+ },
+ 1: {
+ v: '43931',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 102: {
+ 0: {
+ v: '102',
+ t: 2,
+ },
+ 1: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43932',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 103: {
+ 0: {
+ v: '103',
+ t: 2,
+ },
+ 1: {
+ v: '43933',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 104: {
+ 0: {
+ v: '104',
+ t: 2,
+ },
+ 1: {
+ v: '43934',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 105: {
+ 0: {
+ v: '105',
+ t: 2,
+ },
+ 1: {
+ v: '43935',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 106: {
+ 0: {
+ v: '106',
+ t: 2,
+ },
+ 1: {
+ v: '43936',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 107: {
+ 0: {
+ v: '107',
+ t: 2,
+ },
+ 1: {
+ v: '43937',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 108: {
+ 0: {
+ v: '108',
+ t: 2,
+ },
+ 1: {
+ v: '43938',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 109: {
+ 0: {
+ v: '109',
+ t: 2,
+ },
+ 1: {
+ v: '43939',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 110: {
+ 0: {
+ v: '110',
+ t: 2,
+ },
+ 1: {
+ v: '43940',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 111: {
+ 0: {
+ v: '111',
+ t: 2,
+ },
+ 1: {
+ v: '43941',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 112: {
+ 0: {
+ v: '112',
+ t: 2,
+ },
+ 1: {
+ v: '43942',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 113: {
+ 0: {
+ v: '113',
+ t: 2,
+ },
+ 1: {
+ v: '43943',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 114: {
+ 0: {
+ v: '114',
+ t: 2,
+ },
+ 1: {
+ v: '43944',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 115: {
+ 0: {
+ v: '115',
+ t: 2,
+ },
+ 1: {
+ v: '43945',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 116: {
+ 0: {
+ v: '116',
+ t: 2,
+ },
+ 1: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43946',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 117: {
+ 0: {
+ v: '117',
+ t: 2,
+ },
+ 1: {
+ v: '43947',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 118: {
+ 0: {
+ v: '118',
+ t: 2,
+ },
+ 1: {
+ v: '43948',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 119: {
+ 0: {
+ v: '119',
+ t: 2,
+ },
+ 1: {
+ v: '43949',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 120: {
+ 0: {
+ v: '120',
+ t: 2,
+ },
+ 1: {
+ v: '43950',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 121: {
+ 0: {
+ v: '121',
+ t: 2,
+ },
+ 1: {
+ v: '43951',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 122: {
+ 0: {
+ v: '122',
+ t: 2,
+ },
+ 1: {
+ v: '43952',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 123: {
+ 0: {
+ v: '123',
+ t: 2,
+ },
+ 1: {
+ v: '43953',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 124: {
+ 0: {
+ v: '124',
+ t: 2,
+ },
+ 1: {
+ v: '43954',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 125: {
+ 0: {
+ v: '125',
+ t: 2,
+ },
+ 1: {
+ v: '43955',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 126: {
+ 0: {
+ v: '126',
+ t: 2,
+ },
+ 1: {
+ v: '43956',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 127: {
+ 0: {
+ v: '127',
+ t: 2,
+ },
+ 1: {
+ v: '43957',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 128: {
+ 0: {
+ v: '128',
+ t: 2,
+ },
+ 1: {
+ v: '43958',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 129: {
+ 0: {
+ v: '129',
+ t: 2,
+ },
+ 1: {
+ v: '43959',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 130: {
+ 0: {
+ v: '130',
+ t: 2,
+ },
+ 1: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43960',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 131: {
+ 0: {
+ v: '131',
+ t: 2,
+ },
+ 1: {
+ v: '43961',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 132: {
+ 0: {
+ v: '132',
+ t: 2,
+ },
+ 1: {
+ v: '43962',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 133: {
+ 0: {
+ v: '133',
+ t: 2,
+ },
+ 1: {
+ v: '43963',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 134: {
+ 0: {
+ v: '134',
+ t: 2,
+ },
+ 1: {
+ v: '43964',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 135: {
+ 0: {
+ v: '135',
+ t: 2,
+ },
+ 1: {
+ v: '43965',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 136: {
+ 0: {
+ v: '136',
+ t: 2,
+ },
+ 1: {
+ v: '43966',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 137: {
+ 0: {
+ v: '137',
+ t: 2,
+ },
+ 1: {
+ v: '43967',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 138: {
+ 0: {
+ v: '138',
+ t: 2,
+ },
+ 1: {
+ v: '43968',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 139: {
+ 0: {
+ v: '139',
+ t: 2,
+ },
+ 1: {
+ v: '43969',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 140: {
+ 0: {
+ v: '140',
+ t: 2,
+ },
+ 1: {
+ v: '43970',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 141: {
+ 0: {
+ v: '141',
+ t: 2,
+ },
+ 1: {
+ v: '43971',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 142: {
+ 0: {
+ v: '142',
+ t: 2,
+ },
+ 1: {
+ v: '43972',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 143: {
+ 0: {
+ v: '143',
+ t: 2,
+ },
+ 1: {
+ v: '43973',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 144: {
+ 0: {
+ v: '144',
+ t: 2,
+ },
+ 1: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43974',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 145: {
+ 0: {
+ v: '145',
+ t: 2,
+ },
+ 1: {
+ v: '43975',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'U',
+ t: 1,
+ },
+ },
+ 146: {
+ 0: {
+ v: '146',
+ t: 2,
+ },
+ 1: {
+ v: '43976',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 147: {
+ 0: {
+ v: '147',
+ t: 2,
+ },
+ 1: {
+ v: '43977',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 148: {
+ 0: {
+ v: '148',
+ t: 2,
+ },
+ 1: {
+ v: '43978',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 149: {
+ 0: {
+ v: '149',
+ t: 2,
+ },
+ 1: {
+ v: '43979',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 150: {
+ 0: {
+ v: '150',
+ t: 2,
+ },
+ 1: {
+ v: '43980',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 151: {
+ 0: {
+ v: '151',
+ t: 2,
+ },
+ 1: {
+ v: '43981',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 152: {
+ 0: {
+ v: '152',
+ t: 2,
+ },
+ 1: {
+ v: '43982',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 153: {
+ 0: {
+ v: '153',
+ t: 2,
+ },
+ 1: {
+ v: '43983',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 154: {
+ 0: {
+ v: '154',
+ t: 2,
+ },
+ 1: {
+ v: '43984',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 155: {
+ 0: {
+ v: '155',
+ t: 2,
+ },
+ 1: {
+ v: '43985',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 156: {
+ 0: {
+ v: '156',
+ t: 2,
+ },
+ 1: {
+ v: '43986',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 157: {
+ 0: {
+ v: '157',
+ t: 2,
+ },
+ 1: {
+ v: '43987',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ 158: {
+ 0: {
+ v: '158',
+ t: 2,
+ },
+ 1: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 2: {
+ v: '43988',
+ t: 2,
+ s: 'R3',
+ },
+ 3: {
+ v: 'D',
+ t: 1,
+ },
+ },
+ },
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ };
+};
+
+describe('Test AGGREGATE formula 2', () => {
+ let get: Injector['get'];
+ let worksheet: Worksheet;
+ let formulaEngine: FFormula;
+ let commandService: ICommandService;
+ let getCellValue: (row: number, column: number) => Nullable<CellValue>;
+
+ beforeEach(async () => {
+ const testBed = createFunctionTestBed(getFunctionsTestWorkbookData());
+
+ get = testBed.get;
+ worksheet = testBed.sheet.getSheetBySheetId(subUnitId) as Worksheet;
+ formulaEngine = testBed.api.getFormula() as FFormula;
+
+ commandService = get(ICommandService);
+
+ commandService.registerCommand(SetFormulaCalculationStartMutation);
+ commandService.registerCommand(SetFormulaCalculationStopMutation);
+ commandService.registerCommand(SetFormulaCalculationResultMutation);
+ commandService.registerCommand(SetFormulaCalculationNotificationMutation);
+ commandService.registerCommand(SetArrayFormulaDataMutation);
+ commandService.registerCommand(SetRangeValuesMutation);
+
+ const functionService = get(IFunctionService);
+
+ const formulaCurrentConfigService = get(IFormulaCurrentConfigService);
+
+ const formulaRuntimeService = get(IFormulaRuntimeService);
+
+ formulaCurrentConfigService.load({
+ formulaData: {},
+ arrayFormulaCellData: {},
+ arrayFormulaRange: {},
+ forceCalculate: false,
+ dirtyRanges: [],
+ dirtyNameMap: {},
+ dirtyDefinedNameMap: {},
+ dirtyUnitFeatureMap: {},
+ dirtyUnitOtherFormulaMap: {},
+ excludedCell: {},
+ allUnitData: {
+ [testBed.unitId]: testBed.sheetData,
+ },
+ });
+
+ const sheetItem = testBed.sheetData[testBed.sheetId];
+
+ formulaRuntimeService.setCurrent(
+ 0,
+ 0,
+ sheetItem.rowCount,
+ sheetItem.columnCount,
+ testBed.sheetId,
+ testBed.unitId
+ );
+
+ const functions = [
+ ...functionMeta,
+ ...functionMath,
+ ...functionStatistical,
+ ...functionLookup,
+ ...functionLogical,
+ ]
+ .map((registerObject) => {
+ const Func = registerObject[0] as Ctor<BaseFunction>;
+ const name = registerObject[1] as IFunctionNames;
+
+ return new Func(name);
+ });
+
+ functionService.registerExecutors(
+ ...functions
+ );
+
+ formulaEngine.executeCalculation();
+ await formulaEngine.onCalculationEnd();
+
+ getCellValue = (row: number, column: number) => {
+ return worksheet.getCellRaw(row, column)?.v;
+ };
+ });
+
+ describe('Test formula', () => {
+ it('Test IFERROR nested AGGREGATE,ROWS', () => {
+ expect(getCellValue(1, 4)).toBe(43831);
+ expect(getCellValue(1, 5)).toBe(43834);
+
+ expect(getCellValue(2, 4)).toBe(43835);
+ expect(getCellValue(2, 5)).toBe(43848);
+
+ expect(getCellValue(3, 4)).toBe(43849);
+ expect(getCellValue(3, 5)).toBe(43862);
+
+ expect(getCellValue(4, 4)).toBe(43863);
+ expect(getCellValue(4, 5)).toBe(43876);
+
+ expect(getCellValue(5, 4)).toBe(43877);
+ expect(getCellValue(5, 5)).toBe(43890);
+
+ expect(getCellValue(6, 4)).toBe(43891);
+ expect(getCellValue(6, 5)).toBe(43904);
+
+ expect(getCellValue(7, 4)).toBe(43905);
+ expect(getCellValue(7, 5)).toBe(43918);
+
+ expect(getCellValue(8, 4)).toBe(43919);
+ expect(getCellValue(8, 5)).toBe(43932);
+
+ expect(getCellValue(9, 4)).toBe(43933);
+ expect(getCellValue(9, 5)).toBe(43946);
+
+ expect(getCellValue(10, 4)).toBe(43947);
+ expect(getCellValue(10, 5)).toBe(43960);
+
+ expect(getCellValue(11, 4)).toBe(43961);
+ expect(getCellValue(11, 5)).toBe(43974);
+
+ expect(getCellValue(12, 4)).toBe(43975);
+ expect(getCellValue(12, 5)).toBe(43988);
+
+ expect(getCellValue(13, 4)).toBe('');
+ expect(getCellValue(13, 5)).toBe('');
+
+ expect(getCellValue(14, 4)).toBe('');
+ expect(getCellValue(14, 5)).toBe('');
+ });
+ });
+});
diff --git a/packages/sheets/src/controllers/__tests__/formula/aggregate3.spec.ts b/packages/sheets/src/controllers/__tests__/formula/aggregate3.spec.ts
new file mode 100644
index 0000000000..d30e963f20
--- /dev/null
+++ b/packages/sheets/src/controllers/__tests__/formula/aggregate3.spec.ts
@@ -0,0 +1,279 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 { CellValue, Ctor, Injector, IWorkbookData, Nullable, Worksheet } from '@univerjs/core';
+import type { BaseFunction, IFunctionNames } from '@univerjs/engine-formula';
+import type { FFormula } from '@univerjs/engine-formula/facade';
+import { ICommandService, LocaleType } from '@univerjs/core';
+import { functionLogical, functionLookup, functionMath, functionMeta, functionStatistical, IFormulaCurrentConfigService, IFormulaRuntimeService, IFunctionService, SetArrayFormulaDataMutation, SetFormulaCalculationNotificationMutation, SetFormulaCalculationResultMutation, SetFormulaCalculationStartMutation, SetFormulaCalculationStopMutation } from '@univerjs/engine-formula';
+import { beforeEach, describe, expect, it } from 'vitest';
+import { SetRangeValuesMutation } from '../../../commands/mutations/set-range-values.mutation';
+import { createFunctionTestBed } from './create-function-test-bed';
+
+import '@univerjs/engine-formula/facade';
+
+const unitId = 'test';
+const subUnitId = 'sheet1';
+
+const getFunctionsTestWorkbookData = (): IWorkbookData => {
+ return {
+ id: unitId,
+ appVersion: '3.0.0-alpha',
+ styles: {
+ R3: {
+ n: {
+ pattern: 'YYYY-M-D',
+ },
+ },
+ },
+ sheets: {
+ sheet1: {
+ id: subUnitId,
+ cellData: {
+ 0: {
+ 0: {
+ v: 'Products',
+ t: 1,
+ },
+ 1: {
+ v: 'Type',
+ t: 1,
+ },
+ 3: {
+ v: 'Products',
+ t: 1,
+ },
+ 4: {
+ v: 'Type',
+ t: 1,
+ },
+ },
+ 1: {
+ 0: {
+ v: 'Pencil',
+ t: 1,
+ },
+ 1: {
+ v: 'Yellow',
+ t: 1,
+ },
+ 3: {
+ v: 'Pencil',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(INDEX(B:B,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=D2),COUNTIF(D$2:D2,D2))),"")',
+ },
+ },
+ 2: {
+ 0: {
+ v: 'Stapler',
+ t: 1,
+ },
+ 1: {
+ v: 'Black',
+ t: 1,
+ },
+ 3: {
+ v: 'Stapler',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(INDEX(B:B,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=D3),COUNTIF(D$2:D3,D3))),"")',
+ },
+ },
+ 3: {
+ 0: {
+ v: 'Ruler',
+ t: 1,
+ },
+ 1: {
+ v: 'Yellow1',
+ t: 1,
+ },
+ 3: {
+ v: 'Ruler',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(INDEX(B:B,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=D4),COUNTIF(D$2:D4,D4))),"")',
+ },
+ },
+ 4: {
+ 0: {
+ v: 'Paper',
+ t: 1,
+ },
+ 1: {
+ v: 'Green',
+ t: 1,
+ },
+ 3: {
+ v: 'Paper',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(INDEX(B:B,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=D5),COUNTIF(D$2:D5,D5))),"")',
+ },
+ },
+ 5: {
+ 0: {
+ v: 'Pencil',
+ t: 1,
+ },
+ 1: {
+ v: 'Red',
+ t: 1,
+ },
+ 3: {
+ v: 'Pencil',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(INDEX(B:B,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=D6),COUNTIF(D$2:D6,D6))),"")',
+ },
+ },
+ 6: {
+ 0: {
+ v: 'Paper',
+ t: 1,
+ },
+ 1: {
+ v: 'Blue',
+ t: 1,
+ },
+ 3: {
+ v: 'Paper',
+ t: 1,
+ },
+ 4: {
+ f: '=IFERROR(INDEX(B:B,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=D7),COUNTIF(D$2:D7,D7))),"")',
+ },
+ },
+ 7: {
+ 4: {
+ f: '=IFERROR(INDEX(B:B,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=D8),COUNTIF(D$2:D8,D8))),"")',
+ },
+ },
+ 8: {
+ 4: {
+ f: '=IFERROR(INDEX(B:B,AGGREGATE(15,6,ROW($A$2:$A$20)/($A$2:$A$20=D9),COUNTIF(D$2:D9,D9))),"")',
+ },
+ },
+ },
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ };
+};
+
+describe('Test AGGREGATE formula 3', () => {
+ let get: Injector['get'];
+ let worksheet: Worksheet;
+ let formulaEngine: FFormula;
+ let commandService: ICommandService;
+ let getCellValue: (row: number, column: number) => Nullable<CellValue>;
+
+ beforeEach(async () => {
+ const testBed = createFunctionTestBed(getFunctionsTestWorkbookData());
+
+ get = testBed.get;
+ worksheet = testBed.sheet.getSheetBySheetId(subUnitId) as Worksheet;
+ formulaEngine = testBed.api.getFormula() as FFormula;
+
+ commandService = get(ICommandService);
+
+ commandService.registerCommand(SetFormulaCalculationStartMutation);
+ commandService.registerCommand(SetFormulaCalculationStopMutation);
+ commandService.registerCommand(SetFormulaCalculationResultMutation);
+ commandService.registerCommand(SetFormulaCalculationNotificationMutation);
+ commandService.registerCommand(SetArrayFormulaDataMutation);
+ commandService.registerCommand(SetRangeValuesMutation);
+
+ const functionService = get(IFunctionService);
+
+ const formulaCurrentConfigService = get(IFormulaCurrentConfigService);
+
+ const formulaRuntimeService = get(IFormulaRuntimeService);
+
+ formulaCurrentConfigService.load({
+ formulaData: {},
+ arrayFormulaCellData: {},
+ arrayFormulaRange: {},
+ forceCalculate: false,
+ dirtyRanges: [],
+ dirtyNameMap: {},
+ dirtyDefinedNameMap: {},
+ dirtyUnitFeatureMap: {},
+ dirtyUnitOtherFormulaMap: {},
+ excludedCell: {},
+ allUnitData: {
+ [testBed.unitId]: testBed.sheetData,
+ },
+ });
+
+ const sheetItem = testBed.sheetData[testBed.sheetId];
+
+ formulaRuntimeService.setCurrent(
+ 0,
+ 0,
+ sheetItem.rowCount,
+ sheetItem.columnCount,
+ testBed.sheetId,
+ testBed.unitId
+ );
+
+ const functions = [
+ ...functionMeta,
+ ...functionMath,
+ ...functionStatistical,
+ ...functionLookup,
+ ...functionLogical,
+ ]
+ .map((registerObject) => {
+ const Func = registerObject[0] as Ctor<BaseFunction>;
+ const name = registerObject[1] as IFunctionNames;
+
+ return new Func(name);
+ });
+
+ functionService.registerExecutors(
+ ...functions
+ );
+
+ formulaEngine.executeCalculation();
+ await formulaEngine.onCalculationEnd();
+
+ getCellValue = (row: number, column: number) => {
+ return worksheet.getCellRaw(row, column)?.v;
+ };
+ });
+
+ describe('Test formula', () => {
+ it('Test IFERROR nested INDEX,AGGREGATE,ROW,COUNTIF', () => {
+ expect(getCellValue(1, 4)).toBe('Yellow');
+ expect(getCellValue(2, 4)).toBe('Black');
+ expect(getCellValue(3, 4)).toBe('Yellow1');
+ expect(getCellValue(4, 4)).toBe('Green');
+ expect(getCellValue(5, 4)).toBe('Red');
+ expect(getCellValue(6, 4)).toBe('Blue');
+ expect(getCellValue(7, 4)).toBe('');
+ expect(getCellValue(8, 4)).toBe('');
+ });
+ });
+});
diff --git a/packages/sheets/src/controllers/__tests__/formula/subtotal-offset.spec.ts b/packages/sheets/src/controllers/__tests__/formula/subtotal-offset.spec.ts
new file mode 100644
index 0000000000..e126c7ac64
--- /dev/null
+++ b/packages/sheets/src/controllers/__tests__/formula/subtotal-offset.spec.ts
@@ -0,0 +1,1187 @@
+/**
+ * Copyright 2023-present DreamNum Co., Ltd.
+ *
+ * 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 { Ctor, Injector, IWorkbookData } from '@univerjs/core';
+import type { BaseAstNode, BaseFunction, IFunctionNames, LexerNode } from '@univerjs/engine-formula';
+import { ICommandService, LocaleType } from '@univerjs/core';
+import { AstTreeBuilder, ErrorType, functionLookup, functionMath, functionMeta, functionStatistical, generateExecuteAstNodeData, getObjectValue, IFormulaCurrentConfigService, IFormulaRuntimeService, IFunctionService, Interpreter, Lexer, SetArrayFormulaDataMutation, SetFormulaCalculationNotificationMutation, SetFormulaCalculationResultMutation, SetFormulaCalculationStartMutation, SetFormulaCalculationStopMutation } from '@univerjs/engine-formula';
+import { beforeEach, describe, expect, it } from 'vitest';
+import { SetRangeValuesMutation } from '../../../commands/mutations/set-range-values.mutation';
+import { createFunctionTestBed } from './create-function-test-bed';
+
+import '@univerjs/engine-formula/facade';
+
+const unitId = 'test';
+const subUnitId = 'sheet1';
+
+const getFunctionsTestWorkbookData = (): IWorkbookData => {
+ return {
+ id: unitId,
+ appVersion: '3.0.0-alpha',
+ styles: {},
+ sheets: {
+ sheet1: {
+ id: subUnitId,
+ cellData: {
+ 0: {
+ 0: {
+ v: 1,
+ t: 2,
+ },
+ 1: {
+ v: 2,
+ t: 2,
+ },
+ 2: {
+ v: '#NAME?',
+ t: 1,
+ },
+ },
+ 1: {
+ 0: {
+ v: 3,
+ t: 2,
+ },
+ 1: {
+ v: 4,
+ t: 2,
+ },
+ 2: {
+ v: 'B2',
+ t: 1,
+ },
+ 3: {
+ v: 'R2C2',
+ t: 1,
+ },
+ },
+ 2: {
+ 0: {
+ v: 1,
+ t: 2,
+ },
+ 2: {
+ v: 1.23,
+ t: 2,
+ },
+ 3: {
+ v: 1,
+ t: 3,
+ },
+ 4: {
+ v: 0,
+ t: 3,
+ },
+ 9: {
+ v: 8,
+ t: 2,
+ },
+ 10: {
+ v: 9,
+ t: 2,
+ },
+ 11: {
+ v: 10,
+ t: 2,
+ },
+ 12: {
+ v: 11,
+ t: 2,
+ },
+ },
+ 3: {
+ 0: {
+ v: 0,
+ t: 2,
+ },
+ 1: {
+ v: 100,
+ t: 2,
+ },
+ 2: {
+ v: 2.34,
+ t: 2,
+ },
+ 3: {
+ v: 'test',
+ t: 1,
+ },
+ 4: {
+ v: -3,
+ t: 2,
+ },
+ 9: {
+ v: 9,
+ t: 2,
+ },
+ 10: {
+ v: 10,
+ t: 2,
+ },
+ 11: {
+ v: 11,
+ t: 2,
+ },
+ 12: {
+ v: 12,
+ t: 2,
+ },
+ },
+ 4: {
+ 2: {
+ v: 5,
+ t: 2,
+ },
+ 9: {
+ v: 10,
+ t: 2,
+ },
+ 10: {
+ v: 11,
+ t: 2,
+ },
+ 11: {
+ v: 12,
+ t: 2,
+ },
+ 12: {
+ v: 13,
+ t: 2,
+ },
+ },
+ 5: {
+ 9: {
+ v: 11,
+ t: 2,
+ },
+ 10: {
+ v: 12,
+ t: 2,
+ },
+ 11: {
+ v: 13,
+ t: 2,
+ },
+ 12: {
+ v: 14,
+ t: 2,
+ },
+ },
+ 6: {
+ 9: {
+ v: 12,
+ t: 2,
+ },
+ 10: {
+ v: 13,
+ t: 2,
+ },
+ 11: {
+ v: 14,
+ t: 2,
+ },
+ 12: {
+ v: 15,
+ t: 2,
+ },
+ },
+ 7: {
+ 9: {
+ v: 13,
+ t: 2,
+ },
+ 10: {
+ v: 14,
+ t: 2,
+ },
+ 11: {
+ v: 15,
+ t: 2,
+ },
+ 12: {
+ v: 16,
+ t: 2,
+ },
+ },
+ 8: {
+ 1: {
+ v: 4,
+ t: 2,
+ },
+ 9: {
+ v: 14,
+ t: 2,
+ },
+ 10: {
+ v: 15,
+ t: 2,
+ },
+ 11: {
+ v: 16,
+ t: 2,
+ },
+ 12: {
+ v: 17,
+ t: 2,
+ },
+ },
+ 9: {
+ 9: {
+ v: 15,
+ t: 2,
+ },
+ 10: {
+ v: 16,
+ t: 2,
+ },
+ 11: {
+ v: 17,
+ t: 2,
+ },
+ 12: {
+ v: 18,
+ t: 2,
+ },
+ },
+ 10: {
+ 9: {
+ v: 16,
+ t: 2,
+ },
+ 10: {
+ v: 17,
+ t: 2,
+ },
+ 11: {
+ v: 18,
+ t: 2,
+ },
+ 12: {
+ v: 19,
+ t: 2,
+ },
+ },
+ 11: {
+ 9: {
+ v: 17,
+ t: 2,
+ },
+ 10: {
+ v: 18,
+ t: 2,
+ },
+ 11: {
+ v: 19,
+ t: 2,
+ },
+ 12: {
+ v: 20,
+ t: 2,
+ },
+ },
+ 12: {
+ 2: {
+ v: 11,
+ t: 2,
+ },
+ 3: {
+ v: 12,
+ t: 2,
+ },
+ 4: {
+ v: 13,
+ t: 2,
+ },
+ 5: {
+ v: 14,
+ t: 2,
+ },
+ 6: {
+ v: 15,
+ t: 2,
+ },
+ 7: {
+ v: 16,
+ t: 2,
+ },
+ 8: {
+ v: 17,
+ t: 2,
+ },
+ 9: {
+ v: 18,
+ t: 2,
+ },
+ 10: {
+ v: 19,
+ t: 2,
+ },
+ 11: {
+ v: 20,
+ t: 2,
+ },
+ 12: {
+ v: 21,
+ t: 2,
+ },
+ },
+ 13: {
+ 2: {
+ v: 12,
+ t: 2,
+ },
+ 3: {
+ v: 13,
+ t: 2,
+ },
+ 4: {
+ v: 14,
+ t: 2,
+ },
+ 5: {
+ v: 15,
+ t: 2,
+ },
+ 6: {
+ v: 16,
+ t: 2,
+ },
+ 7: {
+ v: 17,
+ t: 2,
+ },
+ 8: {
+ v: 18,
+ t: 2,
+ },
+ 9: {
+ v: 19,
+ t: 2,
+ },
+ 10: {
+ v: 20,
+ t: 2,
+ },
+ 11: {
+ v: 21,
+ t: 2,
+ },
+ 12: {
+ v: 22,
+ t: 2,
+ },
+ },
+ 14: {
+ 2: {
+ v: 13,
+ t: 2,
+ },
+ 3: {
+ v: 14,
+ t: 2,
+ },
+ 4: {
+ v: 15,
+ t: 2,
+ },
+ 5: {
+ v: 16,
+ t: 2,
+ },
+ 6: {
+ v: 17,
+ t: 2,
+ },
+ 7: {
+ v: 18,
+ t: 2,
+ },
+ 8: {
+ v: 19,
+ t: 2,
+ },
+ 9: {
+ v: 20,
+ t: 2,
+ },
+ 10: {
+ v: 21,
+ t: 2,
+ },
+ 11: {
+ v: 22,
+ t: 2,
+ },
+ 12: {
+ v: 23,
+ t: 2,
+ },
+ },
+ 15: {
+ 2: {
+ v: 14,
+ t: 2,
+ },
+ 3: {
+ v: 15,
+ t: 2,
+ },
+ 4: {
+ v: 16,
+ t: 2,
+ },
+ 5: {
+ v: 17,
+ t: 2,
+ },
+ 6: {
+ v: 18,
+ t: 2,
+ },
+ 7: {
+ v: 19,
+ t: 2,
+ },
+ 8: {
+ v: 20,
+ t: 2,
+ },
+ 9: {
+ v: 21,
+ t: 2,
+ },
+ 10: {
+ v: 22,
+ t: 2,
+ },
+ 11: {
+ v: 23,
+ t: 2,
+ },
+ 12: {
+ v: 24,
+ t: 2,
+ },
+ },
+ 16: {
+ 2: {
+ v: 15,
+ t: 2,
+ },
+ 3: {
+ v: 16,
+ t: 2,
+ },
+ 4: {
+ v: 17,
+ t: 2,
+ },
+ 5: {
+ v: 18,
+ t: 2,
+ },
+ 6: {
+ v: 19,
+ t: 2,
+ },
+ 7: {
+ v: 20,
+ t: 2,
+ },
+ 8: {
+ v: 21,
+ t: 2,
+ },
+ 9: {
+ v: 22,
+ t: 2,
+ },
+ 10: {
+ v: 23,
+ t: 2,
+ },
+ 11: {
+ v: 24,
+ t: 2,
+ },
+ 12: {
+ v: 25,
+ t: 2,
+ },
+ },
+ 17: {
+ 2: {
+ v: 16,
+ t: 2,
+ },
+ 3: {
+ v: 17,
+ t: 2,
+ },
+ 4: {
+ v: 18,
+ t: 2,
+ },
+ 5: {
+ v: 19,
+ t: 2,
+ },
+ 6: {
+ v: 20,
+ t: 2,
+ },
+ 7: {
+ v: 21,
+ t: 2,
+ },
+ 8: {
+ v: 22,
+ t: 2,
+ },
+ 9: {
+ v: 23,
+ t: 2,
+ },
+ 10: {
+ v: 24,
+ t: 2,
+ },
+ 11: {
+ v: 25,
+ t: 2,
+ },
+ 12: {
+ v: 26,
+ t: 2,
+ },
+ },
+ 18: {
+ 2: {
+ v: 17,
+ t: 2,
+ },
+ 3: {
+ v: 18,
+ t: 2,
+ },
+ 4: {
+ v: 19,
+ t: 2,
+ },
+ 5: {
+ v: 20,
+ t: 2,
+ },
+ 6: {
+ v: 21,
+ t: 2,
+ },
+ 7: {
+ v: 22,
+ t: 2,
+ },
+ 8: {
+ v: 23,
+ t: 2,
+ },
+ 9: {
+ v: 24,
+ t: 2,
+ },
+ 10: {
+ v: 25,
+ t: 2,
+ },
+ 11: {
+ v: 26,
+ t: 2,
+ },
+ 12: {
+ v: 27,
+ t: 2,
+ },
+ },
+ 19: {
+ 2: {
+ v: 18,
+ t: 2,
+ },
+ 3: {
+ v: 19,
+ t: 2,
+ },
+ 4: {
+ v: 20,
+ t: 2,
+ },
+ 5: {
+ v: 21,
+ t: 2,
+ },
+ 6: {
+ v: 22,
+ t: 2,
+ },
+ 7: {
+ v: 23,
+ t: 2,
+ },
+ 8: {
+ v: 24,
+ t: 2,
+ },
+ 9: {
+ v: 25,
+ t: 2,
+ },
+ 10: {
+ v: 26,
+ t: 2,
+ },
+ 11: {
+ v: 27,
+ t: 2,
+ },
+ 12: {
+ v: 28,
+ t: 2,
+ },
+ },
+ 20: {
+ 2: {
+ v: 19,
+ t: 2,
+ },
+ 3: {
+ v: 20,
+ t: 2,
+ },
+ 4: {
+ v: 21,
+ t: 2,
+ },
+ 5: {
+ v: 22,
+ t: 2,
+ },
+ 6: {
+ v: 23,
+ t: 2,
+ },
+ 7: {
+ v: 24,
+ t: 2,
+ },
+ 8: {
+ v: 25,
+ t: 2,
+ },
+ 9: {
+ v: 26,
+ t: 2,
+ },
+ 10: {
+ v: 27,
+ t: 2,
+ },
+ 11: {
+ v: 28,
+ t: 2,
+ },
+ 12: {
+ v: 29,
+ t: 2,
+ },
+ },
+ 21: {
+ 2: {
+ v: 20,
+ t: 2,
+ },
+ 3: {
+ v: 21,
+ t: 2,
+ },
+ 4: {
+ v: 22,
+ t: 2,
+ },
+ 5: {
+ v: 23,
+ t: 2,
+ },
+ 6: {
+ v: 24,
+ t: 2,
+ },
+ 7: {
+ v: 25,
+ t: 2,
+ },
+ 8: {
+ v: 26,
+ t: 2,
+ },
+ 9: {
+ v: 27,
+ t: 2,
+ },
+ 10: {
+ v: 28,
+ t: 2,
+ },
+ 11: {
+ v: 29,
+ t: 2,
+ },
+ 12: {
+ v: 30,
+ t: 2,
+ },
+ },
+ 22: {
+ 2: {
+ v: 21,
+ t: 2,
+ },
+ 3: {
+ v: 22,
+ t: 2,
+ },
+ 4: {
+ v: 23,
+ t: 2,
+ },
+ 5: {
+ v: 24,
+ t: 2,
+ },
+ 6: {
+ v: 25,
+ t: 2,
+ },
+ 7: {
+ v: 26,
+ t: 2,
+ },
+ 8: {
+ v: 27,
+ t: 2,
+ },
+ 9: {
+ v: 28,
+ t: 2,
+ },
+ 10: {
+ v: 29,
+ t: 2,
+ },
+ 11: {
+ v: 30,
+ t: 2,
+ },
+ 12: {
+ v: 31,
+ t: 2,
+ },
+ },
+ 23: {
+ 2: {
+ v: 22,
+ t: 2,
+ },
+ 3: {
+ v: 23,
+ t: 2,
+ },
+ 4: {
+ v: 24,
+ t: 2,
+ },
+ 5: {
+ v: 25,
+ t: 2,
+ },
+ 6: {
+ v: 26,
+ t: 2,
+ },
+ 7: {
+ v: 27,
+ t: 2,
+ },
+ 8: {
+ v: 28,
+ t: 2,
+ },
+ 9: {
+ v: 29,
+ t: 2,
+ },
+ 10: {
+ v: 30,
+ t: 2,
+ },
+ 11: {
+ v: 31,
+ t: 2,
+ },
+ 12: {
+ v: 32,
+ t: 2,
+ },
+ },
+ 24: {
+ 2: {
+ v: 23,
+ t: 2,
+ },
+ 3: {
+ v: 24,
+ t: 2,
+ },
+ 4: {
+ v: 25,
+ t: 2,
+ },
+ 5: {
+ v: 26,
+ t: 2,
+ },
+ 6: {
+ v: 27,
+ t: 2,
+ },
+ 7: {
+ v: 28,
+ t: 2,
+ },
+ 8: {
+ v: 29,
+ t: 2,
+ },
+ 9: {
+ v: 30,
+ t: 2,
+ },
+ 10: {
+ v: 31,
+ t: 2,
+ },
+ 11: {
+ v: 32,
+ t: 2,
+ },
+ 12: {
+ v: 33,
+ t: 2,
+ },
+ },
+ 25: {
+ 2: {
+ v: 24,
+ t: 2,
+ },
+ 3: {
+ v: 25,
+ t: 2,
+ },
+ 4: {
+ v: 26,
+ t: 2,
+ },
+ 5: {
+ v: 27,
+ t: 2,
+ },
+ 6: {
+ v: 28,
+ t: 2,
+ },
+ 7: {
+ v: 29,
+ t: 2,
+ },
+ 8: {
+ v: 30,
+ t: 2,
+ },
+ 9: {
+ v: 31,
+ t: 2,
+ },
+ 10: {
+ v: 32,
+ t: 2,
+ },
+ 11: {
+ v: 33,
+ t: 2,
+ },
+ 12: {
+ v: 34,
+ t: 2,
+ },
+ },
+ 26: {
+ 2: {
+ v: 25,
+ t: 2,
+ },
+ 3: {
+ v: 26,
+ t: 2,
+ },
+ 4: {
+ v: 27,
+ t: 2,
+ },
+ 5: {
+ v: 28,
+ t: 2,
+ },
+ 6: {
+ v: 29,
+ t: 2,
+ },
+ 7: {
+ v: 30,
+ t: 2,
+ },
+ 8: {
+ v: 31,
+ t: 2,
+ },
+ 9: {
+ v: 32,
+ t: 2,
+ },
+ 10: {
+ v: 33,
+ t: 2,
+ },
+ 11: {
+ v: 34,
+ t: 2,
+ },
+ 12: {
+ v: 35,
+ t: 2,
+ },
+ },
+ },
+ rowData: {
+ 3: {
+ hd: 1,
+ },
+ },
+ },
+ },
+ locale: LocaleType.ZH_CN,
+ name: '',
+ sheetOrder: [],
+ };
+};
+
+describe('Test SUBTOTAL formula', () => {
+ let get: Injector['get'];
+ let lexer: Lexer;
+ let astTreeBuilder: AstTreeBuilder;
+ let interpreter: Interpreter;
+ let commandService: ICommandService;
+ let calculate: (formula: string) => (string | number | boolean | null)[][] | string | number | boolean;
+
+ beforeEach(async () => {
+ const testBed = createFunctionTestBed(getFunctionsTestWorkbookData());
+
+ get = testBed.get;
+
+ lexer = get(Lexer);
+ astTreeBuilder = get(AstTreeBuilder);
+ interpreter = get(Interpreter);
+ commandService = get(ICommandService);
+
+ commandService.registerCommand(SetFormulaCalculationStartMutation);
+ commandService.registerCommand(SetFormulaCalculationStopMutation);
+ commandService.registerCommand(SetFormulaCalculationResultMutation);
+ commandService.registerCommand(SetFormulaCalculationNotificationMutation);
+ commandService.registerCommand(SetArrayFormulaDataMutation);
+ commandService.registerCommand(SetRangeValuesMutation);
+
+ const functionService = get(IFunctionService);
+
+ const formulaCurrentConfigService = get(IFormulaCurrentConfigService);
+
+ const formulaRuntimeService = get(IFormulaRuntimeService);
+
+ formulaCurrentConfigService.load({
+ formulaData: {},
+ arrayFormulaCellData: {},
+ arrayFormulaRange: {},
+ forceCalculate: false,
+ dirtyRanges: [],
+ dirtyNameMap: {},
+ dirtyDefinedNameMap: {},
+ dirtyUnitFeatureMap: {},
+ dirtyUnitOtherFormulaMap: {},
+ excludedCell: {},
+ allUnitData: {
+ [testBed.unitId]: testBed.sheetData,
+ },
+ });
+
+ const sheetItem = testBed.sheetData[testBed.sheetId];
+
+ formulaRuntimeService.setCurrent(
+ 0,
+ 0,
+ sheetItem.rowCount,
+ sheetItem.columnCount,
+ testBed.sheetId,
+ testBed.unitId
+ );
+
+ const functions = [
+ ...functionMeta,
+ ...functionMath,
+ ...functionStatistical,
+ ...functionLookup,
+ ]
+ .map((registerObject) => {
+ const Func = registerObject[0] as Ctor<BaseFunction>;
+ const name = registerObject[1] as IFunctionNames;
+
+ return new Func(name);
+ });
+
+ functionService.registerExecutors(
+ ...functions
+ );
+
+ calculate = (formula: string) => {
+ const lexerNode = lexer.treeBuilder(formula);
+
+ const astNode = astTreeBuilder.parse(lexerNode as LexerNode);
+
+ const result = interpreter.execute(generateExecuteAstNodeData(astNode as BaseAstNode));
+
+ return getObjectValue(result, true);
+ };
+ });
+
+ describe('Test formula', () => {
+ it('Test SUBTOTAL nested OFFSET', () => {
+ let result = calculate('=SUBTOTAL(A1:B2,C13)');
+ expect(result).toStrictEqual([
+ [11, 1],
+ [1, 11],
+ ]);
+
+ result = calculate('=SUBTOTAL(A1:B2,OFFSET(J$16,ROW(J$16:J$37)-MIN(ROW($J$16:$J$37)),0))');
+ expect(result).toStrictEqual([
+ [21, 1],
+ [1, 22],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ [ErrorType.NA, ErrorType.NA],
+ ]);
+
+ result = calculate('=SUBTOTAL(A1:B1,OFFSET(J$16,ROW(J$16:J$37)-MIN(ROW($J$16:$J$37)),0))');
+ expect(result).toStrictEqual([
+ [21, 1],
+ [22, 1],
+ [23, 1],
+ [24, 1],
+ [25, 1],
+ [26, 1],
+ [27, 1],
+ [28, 1],
+ [29, 1],
+ [30, 1],
+ [31, 1],
+ [32, 1],
+ [ErrorType.DIV_BY_ZERO, 0],
+ [ErrorType.DIV_BY_ZERO, 0],
+ [ErrorType.DIV_BY_ZERO, 0],
+ [ErrorType.DIV_BY_ZERO, 0],
+ [ErrorType.DIV_BY_ZERO, 0],
+ [ErrorType.DIV_BY_ZERO, 0],
+ [ErrorType.DIV_BY_ZERO, 0],
+ [ErrorType.DIV_BY_ZERO, 0],
+ [ErrorType.DIV_BY_ZERO, 0],
+ [ErrorType.DIV_BY_ZERO, 0],
+ ]);
+
+ result = calculate('=SUBTOTAL(C3:C5,OFFSET(J$16,ROW(J$16:J$37)-MIN(ROW($J$16:$J$37)),0))');
+ expect(result).toStrictEqual([
+ [21],
+ [1],
+ [23],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ [ErrorType.NA],
+ ]);
+ });
+
+ it('Test SUBTOTAL', () => {
+ let result = calculate('=SUBTOTAL(107,H27:I27)');
+ expect(result).toBe(0.707106781187);
+
+ result = calculate('=SUBTOTAL(C1,C3:M27)');
+ expect(result).toBe(ErrorType.NAME);
+
+ result = calculate('=SUBTOTAL(C1:C3,C3:M27)');
+ expect(result).toStrictEqual([
+ [ErrorType.NAME],
+ [ErrorType.VALUE],
+ [20.8639712919],
+ ]);
+ });
+ });
+});