Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

fix(formula): some formulas need to be tested in dev (#4116)

wpxp1234564ff30b1bb95f
diff --git a/packages/engine-formula/src/functions/date/epochtodate/__tests__/index.spec.ts b/packages/engine-formula/src/functions/date/epochtodate/__tests__/index.spec.ts
index dc7497c6cc..f159815f15 100644
--- a/packages/engine-formula/src/functions/date/epochtodate/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/date/epochtodate/__tests__/index.spec.ts
@@ -138,5 +138,12 @@ describe('Test epochtodate function', () => {
const result8 = testFunction.calculate(timestamp, unit8);
expect(getObjectValue(result8)).toBe(ErrorType.NUM);
});
+
+ it('More test', () => {
+ const timestamp = NumberValueObject.create(1656356678000410);
+ const unit = NumberValueObject.create(1);
+ const result = testFunction.calculate(timestamp, unit);
+ expect(getObjectValue(result)).toBe(19170820453.264008);
+ });
});
});
diff --git a/packages/engine-formula/src/functions/date/epochtodate/index.ts b/packages/engine-formula/src/functions/date/epochtodate/index.ts
index 1c1511e9a2..8616ef78a7 100644
--- a/packages/engine-formula/src/functions/date/epochtodate/index.ts
+++ b/packages/engine-formula/src/functions/date/epochtodate/index.ts
@@ -72,9 +72,16 @@ export class Epochtodate extends BaseFunction {
}
const date = new Date(Date.UTC(1970, 0, 1, 0, 0, 0, 0) + timestampValue);
- const dateSerialNumber = excelDateTimeSerial(date);
- return NumberValueObject.create(dateSerialNumber, 'yyyy-MM-dd hh:mm:ss AM/PM');
+ if (!Number.isNaN(date.getTime())) {
+ const dateSerialNumber = excelDateTimeSerial(date);
+
+ return NumberValueObject.create(dateSerialNumber, 'yyyy-MM-dd AM/PM hh:mm:ss');
+ } else {
+ const result = 25569 + timestampValue / 86400000;
+
+ return NumberValueObject.create(result);
+ }
}
private _checkVariants(timestamp: FunctionVariantType, unit: FunctionVariantType) {
diff --git a/packages/engine-formula/src/functions/information/isbetween/__tests__/index.spec.ts b/packages/engine-formula/src/functions/information/isbetween/__tests__/index.spec.ts
index f7e71f92b1..71ab90bbb3 100644
--- a/packages/engine-formula/src/functions/information/isbetween/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/information/isbetween/__tests__/index.spec.ts
@@ -20,6 +20,7 @@ import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
+import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_INFORMATION } from '../../function-names';
import { Isbetween } from '../index';
@@ -32,7 +33,7 @@ describe('Test isbetween function', () => {
const lowerValue = NumberValueObject.create(1.2);
const upperValue = NumberValueObject.create(12.45);
const result = testFunction.calculate(valueToCompare, lowerValue, upperValue);
- expect(result.getValue()).toBe(true);
+ expect(getObjectValue(result)).toBe(true);
});
it('LowerValue > upperValue', () => {
@@ -40,7 +41,7 @@ describe('Test isbetween function', () => {
const lowerValue = NumberValueObject.create(1.2);
const upperValue = NumberValueObject.create(-12.45);
const result = testFunction.calculate(valueToCompare, lowerValue, upperValue);
- expect(result.getValue()).toBe(ErrorType.NUM);
+ expect(getObjectValue(result)).toBe(ErrorType.NUM);
});
it('Value is number string', () => {
@@ -48,7 +49,7 @@ describe('Test isbetween function', () => {
const lowerValue = NumberValueObject.create(1.2);
const upperValue = NumberValueObject.create(12.45);
const result = testFunction.calculate(valueToCompare, lowerValue, upperValue);
- expect(result.getValue()).toBe(ErrorType.VALUE);
+ expect(getObjectValue(result)).toBe(false);
});
it('Value is normal string', () => {
@@ -56,7 +57,11 @@ describe('Test isbetween function', () => {
const lowerValue = NumberValueObject.create(1.2);
const upperValue = NumberValueObject.create(12.45);
const result = testFunction.calculate(valueToCompare, lowerValue, upperValue);
- expect(result.getValue()).toBe(ErrorType.VALUE);
+ expect(getObjectValue(result)).toBe(false);
+
+ const lowerValueIsInclusive = StringValueObject.create('test');
+ const result2 = testFunction.calculate(valueToCompare, lowerValue, upperValue, lowerValueIsInclusive);
+ expect(getObjectValue(result2)).toBe(ErrorType.VALUE);
});
it('Value is blank cell', () => {
@@ -64,7 +69,7 @@ describe('Test isbetween function', () => {
const lowerValue = NumberValueObject.create(1.2);
const upperValue = NumberValueObject.create(12.45);
const result = testFunction.calculate(valueToCompare, lowerValue, upperValue);
- expect(result.getValue()).toBe(false);
+ expect(getObjectValue(result)).toBe(false);
});
it('Value is boolean', () => {
@@ -72,7 +77,7 @@ describe('Test isbetween function', () => {
const lowerValue = NumberValueObject.create(1.2);
const upperValue = NumberValueObject.create(12.45);
const result = testFunction.calculate(valueToCompare, lowerValue, upperValue);
- expect(result.getValue()).toBe(false);
+ expect(getObjectValue(result)).toBe(false);
});
it('Value is error', () => {
@@ -80,7 +85,7 @@ describe('Test isbetween function', () => {
const lowerValue = NumberValueObject.create(1.2);
const upperValue = NumberValueObject.create(12.45);
const result = testFunction.calculate(valueToCompare, lowerValue, upperValue);
- expect(result.getValue()).toBe(ErrorType.NAME);
+ expect(getObjectValue(result)).toBe(ErrorType.NAME);
});
it('Value is array', () => {
@@ -98,7 +103,31 @@ describe('Test isbetween function', () => {
const lowerValue = NumberValueObject.create(1.2);
const upperValue = NumberValueObject.create(12.45);
const result = testFunction.calculate(valueToCompare, lowerValue, upperValue);
- expect(result.getValue()).toBe(ErrorType.VALUE);
+ expect(getObjectValue(result)).toBe(ErrorType.VALUE);
+ });
+
+ it('More test', () => {
+ const valueToCompare = NumberValueObject.create(0);
+ const lowerValue = StringValueObject.create('AA');
+ const upperValue = NumberValueObject.create(1);
+ const lowerValueIsInclusive = NumberValueObject.create(1);
+ const upperValueIsInclusive = BooleanValueObject.create(false);
+ const result = testFunction.calculate(valueToCompare, lowerValue, upperValue, lowerValueIsInclusive, upperValueIsInclusive);
+ expect(getObjectValue(result)).toBe(ErrorType.NUM);
+
+ const valueToCompare2 = NumberValueObject.create(0);
+ const lowerValue2 = StringValueObject.create('AA');
+ const upperValue2 = StringValueObject.create('BB');
+ const lowerValueIsInclusive2 = NumberValueObject.create(1);
+ const upperValueIsInclusive2 = BooleanValueObject.create(false);
+ const result2 = testFunction.calculate(valueToCompare2, lowerValue2, upperValue2, lowerValueIsInclusive2, upperValueIsInclusive2);
+ expect(getObjectValue(result2)).toBe(false);
+
+ const valueToCompare3 = NumberValueObject.create(0);
+ const lowerValue3 = NumberValueObject.create(1);
+ const upperValue3 = BooleanValueObject.create(false);
+ const result3 = testFunction.calculate(valueToCompare3, lowerValue3, upperValue3);
+ expect(getObjectValue(result3)).toBe(false);
});
});
});
diff --git a/packages/engine-formula/src/functions/information/isbetween/index.ts b/packages/engine-formula/src/functions/information/isbetween/index.ts
index 7e47ac9bdd..6131f94d20 100644
--- a/packages/engine-formula/src/functions/information/isbetween/index.ts
+++ b/packages/engine-formula/src/functions/information/isbetween/index.ts
@@ -14,6 +14,7 @@
* limitations under the License.
*/
+import type { compareToken } from '../../../basics/token';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorType } from '../../../basics/error-type';
import { checkVariantsErrorIsArray } from '../../../engine/utils/check-variant-error';
@@ -44,29 +45,34 @@ export class Isbetween extends BaseFunction {
const [valueToCompareObject, lowerValueObject, upperValueObject, lowerValueIsInclusiveObject, upperValueIsInclusiveObject] = variants as BaseValueObject[];
- if (
- valueToCompareObject.isString() ||
- lowerValueObject.isString() ||
- upperValueObject.isString() ||
- lowerValueIsInclusiveObject.isString() ||
- upperValueIsInclusiveObject.isString()
- ) {
+ if (lowerValueIsInclusiveObject.isString() || upperValueIsInclusiveObject.isString()) {
return ErrorValueObject.create(ErrorType.VALUE);
}
- const valueToCompareValue = +valueToCompareObject.getValue();
- const lowerValueValue = +lowerValueObject.getValue();
- const upperValueValue = +upperValueObject.getValue();
+ const lowGreaterThanUpper = lowerValueObject.compare(upperValueObject, '>' as compareToken);
+
+ if (lowGreaterThanUpper.getValue() === true) {
+ return ErrorValueObject.create(ErrorType.NUM);
+ }
+
const lowerValueIsInclusiveValue = +lowerValueIsInclusiveObject.getValue();
const upperValueIsInclusiveValue = +upperValueIsInclusiveObject.getValue();
- if (lowerValueValue > upperValueValue) {
- return ErrorValueObject.create(ErrorType.NUM);
+ const lowerComparisonOperator = lowerValueIsInclusiveValue ? '>=' : '>';
+ const upperComparisonOperator = upperValueIsInclusiveValue ? '<=' : '<';
+
+ const lowerComparison = valueToCompareObject.compare(lowerValueObject, lowerComparisonOperator as compareToken);
+
+ if (lowerComparison.getValue() === false) {
+ return BooleanValueObject.create(false);
}
- const lowerComparison = lowerValueIsInclusiveValue ? valueToCompareValue >= lowerValueValue : valueToCompareValue > lowerValueValue;
- const upperComparison = upperValueIsInclusiveValue ? valueToCompareValue <= upperValueValue : valueToCompareValue < upperValueValue;
+ const upperComparison = valueToCompareObject.compare(upperValueObject, upperComparisonOperator as compareToken);
+
+ if (upperComparison.getValue() === false) {
+ return BooleanValueObject.create(false);
+ }
- return BooleanValueObject.create(lowerComparison && upperComparison);
+ return BooleanValueObject.create(true);
}
}
diff --git a/packages/engine-formula/src/functions/math/roundbank/__tests__/index.spec.ts b/packages/engine-formula/src/functions/math/roundbank/__tests__/index.spec.ts
index fa831a5f58..0ab9e54de7 100644
--- a/packages/engine-formula/src/functions/math/roundbank/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/math/roundbank/__tests__/index.spec.ts
@@ -17,9 +17,10 @@
import { describe, expect, it } from 'vitest';
import { ErrorType } from '../../../../basics/error-type';
-import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';
+import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
import { BooleanValueObject, NullValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';
+import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_MATH } from '../../function-names';
import { Roundbank } from '../index';
@@ -31,91 +32,91 @@ describe('Test roundbank function', () => {
const number = NumberValueObject.create(2.344);
const numDigits = NumberValueObject.create(2);
const result = testFunction.calculate(number, numDigits);
- expect(result.getValue()).toBe(2.34);
+ expect(getObjectValue(result)).toBe(2.34);
const number2 = NumberValueObject.create(2.346);
const result2 = testFunction.calculate(number2, numDigits);
- expect(result2.getValue()).toBe(2.35);
+ expect(getObjectValue(result2)).toBe(2.35);
const number3 = NumberValueObject.create(2.345);
const result3 = testFunction.calculate(number3, numDigits);
- expect(result3.getValue()).toBe(2.34);
+ expect(getObjectValue(result3)).toBe(2.34);
const number4 = NumberValueObject.create(2.3451);
const result4 = testFunction.calculate(number4, numDigits);
- expect(result4.getValue()).toBe(2.35);
+ expect(getObjectValue(result4)).toBe(2.35);
});
it('Number value is negtive', () => {
const number = NumberValueObject.create(-2.344);
const numDigits = NumberValueObject.create(2);
const result = testFunction.calculate(number, numDigits);
- expect(result.getValue()).toBe(-2.34);
+ expect(getObjectValue(result)).toBe(-2.34);
const number2 = NumberValueObject.create(-2.346);
const result2 = testFunction.calculate(number2, numDigits);
- expect(result2.getValue()).toBe(-2.35);
+ expect(getObjectValue(result2)).toBe(-2.35);
const number3 = NumberValueObject.create(-2.345);
const result3 = testFunction.calculate(number3, numDigits);
- expect(result3.getValue()).toBe(-2.34);
+ expect(getObjectValue(result3)).toBe(-2.34);
const number4 = NumberValueObject.create(-2.3451);
const result4 = testFunction.calculate(number4, numDigits);
- expect(result4.getValue()).toBe(-2.35);
+ expect(getObjectValue(result4)).toBe(-2.35);
});
it('NumDigits value is negtive', () => {
const number = NumberValueObject.create(2.344);
const numDigits = NumberValueObject.create(-2);
const result = testFunction.calculate(number, numDigits);
- expect(result.getValue()).toBe(0);
+ expect(getObjectValue(result)).toBe(0);
const number2 = NumberValueObject.create(-222.346);
const result2 = testFunction.calculate(number2, numDigits);
- expect(result2.getValue()).toBe(-200);
+ expect(getObjectValue(result2)).toBe(-200);
const number3 = NumberValueObject.create(22.345);
const numDigits2 = NumberValueObject.create(-1);
const result3 = testFunction.calculate(number3, numDigits2);
- expect(result3.getValue()).toBe(20);
+ expect(getObjectValue(result3)).toBe(20);
const number4 = NumberValueObject.create(-222.3451);
const result4 = testFunction.calculate(number4, numDigits2);
- expect(result4.getValue()).toBe(-220);
+ expect(getObjectValue(result4)).toBe(-220);
});
it('Value is normal string', () => {
const number = StringValueObject.create('test');
const numDigits = NumberValueObject.create(1);
const result = testFunction.calculate(number, numDigits);
- expect(result.getValue()).toBe(ErrorType.VALUE);
+ expect(getObjectValue(result)).toBe(ErrorType.VALUE);
});
it('Value is boolean', () => {
const number = NumberValueObject.create(2.15);
const numDigits = BooleanValueObject.create(true);
const result = testFunction.calculate(number, numDigits);
- expect(result.getValue()).toBe(2.2);
+ expect(getObjectValue(result)).toBe(2.2);
});
it('Value is blank cell', () => {
const number = NumberValueObject.create(2.15);
const numDigits = NullValueObject.create();
const result = testFunction.calculate(number, numDigits);
- expect(result.getValue()).toBe(2);
+ expect(getObjectValue(result)).toBe(2);
});
it('Value is error', () => {
const number = ErrorValueObject.create(ErrorType.NAME);
const numDigits = NumberValueObject.create(1);
const result = testFunction.calculate(number, numDigits);
- expect(result.getValue()).toBe(ErrorType.NAME);
+ expect(getObjectValue(result)).toBe(ErrorType.NAME);
const number2 = NumberValueObject.create(2.15);
const numDigits2 = ErrorValueObject.create(ErrorType.NAME);
const result2 = testFunction.calculate(number2, numDigits2);
- expect(result2.getValue()).toBe(ErrorType.NAME);
+ expect(getObjectValue(result2)).toBe(ErrorType.NAME);
});
it('Value is array and array', () => {
@@ -142,9 +143,20 @@ describe('Test roundbank function', () => {
column: 0,
});
const result = testFunction.calculate(number, numDigits);
- expect(transformToValue(result.getArrayValue())).toStrictEqual([
+ expect(getObjectValue(result)).toStrictEqual([
[2.2, 2.1, -1.58, 22, 630, 0, -0, ErrorType.NA, ErrorType.NA],
]);
});
+
+ it('More test', () => {
+ const number = NumberValueObject.create(2.344);
+ const numDigits = NumberValueObject.create(2000);
+ const result = testFunction.calculate(number, numDigits);
+ expect(getObjectValue(result)).toBe(2.344);
+
+ const numDigits2 = NumberValueObject.create(-2000);
+ const result2 = testFunction.calculate(number, numDigits2);
+ expect(getObjectValue(result2)).toBe(0);
+ });
});
});
diff --git a/packages/engine-formula/src/functions/math/roundbank/index.ts b/packages/engine-formula/src/functions/math/roundbank/index.ts
index c10e7f156d..097469411b 100644
--- a/packages/engine-formula/src/functions/math/roundbank/index.ts
+++ b/packages/engine-formula/src/functions/math/roundbank/index.ts
@@ -75,6 +75,14 @@ export class Roundbank extends BaseFunction {
}
private _roundBank(number: number, numDigits: number): number {
+ if (numDigits > 16) {
+ return number;
+ }
+
+ if (numDigits < -16) {
+ return 0;
+ }
+
// logic is:
// If the digit to be rounded is 4 or less than, it is discarded.
// If the digit to be rounded is 6 or greater, it rounds up.
diff --git a/packages/engine-formula/src/functions/statistical/average-weighted/__tests__/index.spec.ts b/packages/engine-formula/src/functions/statistical/average-weighted/__tests__/index.spec.ts
index 55d9c5dec1..58e1253013 100644
--- a/packages/engine-formula/src/functions/statistical/average-weighted/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/statistical/average-weighted/__tests__/index.spec.ts
@@ -18,7 +18,7 @@ import { describe, expect, it } from 'vitest';
import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
-import { NumberValueObject } from '../../../../engine/value-object/primitive-object';
+import { NullValueObject, NumberValueObject } from '../../../../engine/value-object/primitive-object';
import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_STATISTICAL } from '../../function-names';
import { AverageWeighted } from '../index';
@@ -124,5 +124,22 @@ describe('Test averageWeighted function', () => {
const result = testFunction.calculate(value1, weight1);
expect(getObjectValue(result)).toBe(ErrorType.VALUE);
});
+
+ it('More test', () => {
+ const value1 = ArrayValueObject.create('{2,4}');
+ const weight1 = ArrayValueObject.create('{1,3}');
+ const value2 = NullValueObject.create();
+ const weight2 = NullValueObject.create();
+ const result = testFunction.calculate(value1, weight1, value2, weight2);
+ expect(getObjectValue(result)).toBe(3.5);
+
+ const value3 = NumberValueObject.create(90);
+ const weight3 = NumberValueObject.create(0);
+ const result2 = testFunction.calculate(value3, weight3);
+ expect(getObjectValue(result2)).toBe(ErrorType.DIV_BY_ZERO);
+
+ const result3 = testFunction.calculate(value2, weight2);
+ expect(getObjectValue(result3)).toBe(ErrorType.DIV_BY_ZERO);
+ });
});
});
diff --git a/packages/engine-formula/src/functions/statistical/average-weighted/index.ts b/packages/engine-formula/src/functions/statistical/average-weighted/index.ts
index c3a56853d4..9c9743190e 100644
--- a/packages/engine-formula/src/functions/statistical/average-weighted/index.ts
+++ b/packages/engine-formula/src/functions/statistical/average-weighted/index.ts
@@ -20,6 +20,8 @@ import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-ob
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
+type ValueType = number | string | boolean;
+
export class AverageWeighted extends BaseFunction {
override minParams = 2;
@@ -35,8 +37,8 @@ export class AverageWeighted extends BaseFunction {
otherErrorObject = ErrorValueObject.create(ErrorType.NA);
}
- const values: number[] = [];
- const weights: number[] = [];
+ const values: ValueType[] = [];
+ const weights: ValueType[] = [];
for (let i = 0; i < variants.length; i += 2) {
const valueObject = variants[i];
@@ -55,13 +57,9 @@ export class AverageWeighted extends BaseFunction {
continue;
}
- if (obj.isNull() || obj.isBoolean() || obj.isString()) {
- isOtherError = true;
- otherErrorObject = ErrorValueObject.create(ErrorType.VALUE);
- continue;
- }
+ const value = obj.isNull() ? '' : obj.getValue();
- values.push(+obj.getValue());
+ values.push(value);
}
}
@@ -90,19 +88,7 @@ export class AverageWeighted extends BaseFunction {
continue;
}
- if (obj.isNull() || obj.isBoolean() || obj.isString()) {
- isOtherError = true;
- otherErrorObject = ErrorValueObject.create(ErrorType.VALUE);
- continue;
- }
-
- const weight = +obj.getValue();
-
- if (weight < 0) {
- isOtherError = true;
- otherErrorObject = ErrorValueObject.create(ErrorType.VALUE);
- continue;
- }
+ const weight = obj.isNull() ? '' : obj.getValue();
weights.push(weight);
}
@@ -116,15 +102,32 @@ export class AverageWeighted extends BaseFunction {
return this._getResult(values, weights);
}
- private _getResult(values: number[], weights: number[]): BaseValueObject {
+ private _getResult(values: ValueType[], weights: ValueType[]): BaseValueObject {
const n = values.length;
let sum = 0;
let sumWeight = 0;
for (let i = 0; i < n; i++) {
- sum += values[i] * weights[i];
- sumWeight += weights[i];
+ const value = values[i];
+ const weight = weights[i];
+
+ // If value and weight are not numbers, skip
+ if (typeof value !== 'number' && typeof weight !== 'number') {
+ continue;
+ }
+
+ // If value and weight only one is number, return error
+ if (typeof value !== 'number' || typeof weight !== 'number' || weight < 0) {
+ return ErrorValueObject.create(ErrorType.VALUE);
+ }
+
+ sum += value * weight;
+ sumWeight += weight;
+ }
+
+ if (sumWeight === 0) {
+ return ErrorValueObject.create(ErrorType.DIV_BY_ZERO);
}
const result = sum / sumWeight;
diff --git a/packages/engine-formula/src/functions/statistical/linest/__tests__/index.spec.ts b/packages/engine-formula/src/functions/statistical/linest/__tests__/index.spec.ts
index 3ba932cd5b..b94d061409 100644
--- a/packages/engine-formula/src/functions/statistical/linest/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/statistical/linest/__tests__/index.spec.ts
@@ -417,5 +417,40 @@ describe('Test linest function', () => {
const result2 = testFunction.calculate(knownYs2, knownXs2);
expect(getObjectValue(result2)).toStrictEqual(ErrorType.VALUE);
});
+
+ it('More test1', () => {
+ const knownYs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [2, 2],
+ ]),
+ rowCount: 1,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const knownXs = ArrayValueObject.create({
+ calculateValueList: transformToValueObject([
+ [20, 14200],
+ ]),
+ rowCount: 1,
+ columnCount: 2,
+ unitId: '',
+ sheetId: '',
+ row: 0,
+ column: 0,
+ });
+ const constb = BooleanValueObject.create(true);
+ const stats = BooleanValueObject.create(true);
+ const result = testFunction.calculate(knownYs, knownXs, constb, stats);
+ expect(getObjectValue(result)).toStrictEqual([
+ [0, 2],
+ [0, 0],
+ [1, 0],
+ [ErrorType.NUM, 0],
+ [0, 0],
+ ]);
+ });
});
});
diff --git a/packages/engine-formula/src/functions/statistical/linest/index.ts b/packages/engine-formula/src/functions/statistical/linest/index.ts
index e76dcb9015..e2e06643df 100644
--- a/packages/engine-formula/src/functions/statistical/linest/index.ts
+++ b/packages/engine-formula/src/functions/statistical/linest/index.ts
@@ -186,7 +186,7 @@ export class Linest extends BaseFunction {
let result: Array<Array<number | string>> = [];
if (stats) {
- const n = knownYsValues.length;
+ const n = knownYsValuesFlat.length;
let meanY = 0;
let meanX = 0;
@@ -217,12 +217,11 @@ export class Linest extends BaseFunction {
}
const ssreg = sstotal - ssresid;
- const r2 = sstotal === 0 ? 0 : ssreg / sstotal;
+ const r2 = ssreg === sstotal ? 1 : ssreg / sstotal;
let se = 0;
let seb: number | ErrorType = 0;
let sey = 0;
- let F = 0;
if (df > 0) {
if (ssx > 0) {
@@ -231,9 +230,10 @@ export class Linest extends BaseFunction {
}
sey = Math.sqrt(ssresid / df);
- F = (ssreg / 1) / (ssresid / df);
}
+ const F = df > 0 ? (ssreg / 1) / (ssresid / df) : ErrorType.NUM;
+
// seb = #N/A when const is FALSE
if (!constb) {
seb = ErrorType.NA;
diff --git a/packages/engine-formula/src/functions/statistical/trend/__tests__/index.spec.ts b/packages/engine-formula/src/functions/statistical/trend/__tests__/index.spec.ts
index e0456b6fb1..199cb4eb75 100644
--- a/packages/engine-formula/src/functions/statistical/trend/__tests__/index.spec.ts
+++ b/packages/engine-formula/src/functions/statistical/trend/__tests__/index.spec.ts
@@ -19,7 +19,7 @@ import { describe, expect, it } from 'vitest';
import { ErrorType } from '../../../../basics/error-type';
import { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object';
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object';
-import { BooleanValueObject } from '../../../../engine/value-object/primitive-object';
+import { BooleanValueObject, NumberValueObject } from '../../../../engine/value-object/primitive-object';
import { getObjectValue } from '../../../__tests__/create-function-test-bed';
import { FUNCTION_NAMES_STATISTICAL } from '../../function-names';
import { Trend } from '../index';
@@ -624,6 +624,12 @@ describe('Test trend function', () => {
expect(getObjectValue(result5)).toStrictEqual([
[0.9999999999999902, 1.9999999999999938, 2.9999999999999973, 4.000000000000001, 5.000000000000004, 6.000000000000008, 7.0000000000000115],
]);
+
+ const knownYs4 = NumberValueObject.create(1);
+ const knownXs4 = NumberValueObject.create(1);
+ const newXs5 = NumberValueObject.create(2);
+ const result6 = testFunction.calculate(knownYs4, knownXs4, newXs5);
+ expect(getObjectValue(result6)).toBe(ErrorType.NA);
});
});
});
diff --git a/packages/engine-formula/src/functions/statistical/trend/index.ts b/packages/engine-formula/src/functions/statistical/trend/index.ts
index 2128b05a98..96ab86fb25 100644
--- a/packages/engine-formula/src/functions/statistical/trend/index.ts
+++ b/packages/engine-formula/src/functions/statistical/trend/index.ts
@@ -135,10 +135,14 @@ export class Trend extends BaseFunction {
const knownYsValuesFlat = knownYsValues.flat();
const knownXsValuesFlat = knownXsValues.flat();
- const { slope, intercept } = getSlopeAndIntercept(knownXsValuesFlat, knownYsValuesFlat, constb, false);
+ const { slope: m, intercept: b } = getSlopeAndIntercept(knownXsValuesFlat, knownYsValuesFlat, constb, false);
+
+ if (Number.isNaN(m)) {
+ return ErrorValueObject.create(ErrorType.NA);
+ }
const result = newXsValues.map((row) => {
- return row.map((value) => slope * value + intercept);
+ return row.map((value) => m * value + b);
});
return ArrayValueObject.createByArray(result);