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.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
fix(formula): fix date formula calculation error (#6070)
wpxp123456a4a08cd0ad62
diff --git a/packages/engine-formula/src/functions/date/date/__tests__/index.spec.ts b/packages/engine-formula/src/functions/date/date/__tests__/index.spec.tsindex 4f434dfade..b6f77453cb 100644--- a/packages/engine-formula/src/functions/date/date/__tests__/index.spec.ts+++ b/packages/engine-formula/src/functions/date/date/__tests__/index.spec.ts@@ -16,8 +16,9 @@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 { NumberValueObject } from '../../../../engine/value-object/primitive-object';+import { getObjectValue } from '../../../util';import { FUNCTION_NAMES_DATE } from '../../function-names';import { DateFunction } from '../index';@@ -30,28 +31,31 @@ describe('Test date function', () => {const month = NumberValueObject.create(1);const day = NumberValueObject.create(1);const result = testFunction.calculate(year, month, day);- expect(transformToValue(result.getArrayValue())).toStrictEqual([[45292]]);+ expect(getObjectValue(result)).toBe(45292);});+it('Edge case, 1900.1.1', () => {const year = NumberValueObject.create(1900);const month = NumberValueObject.create(1);const day = NumberValueObject.create(1);const result = testFunction.calculate(year, month, day);- expect(transformToValue(result.getArrayValue())).toStrictEqual([[1]]);+ expect(getObjectValue(result)).toBe(1);});+it('Edge case, 1900.1.0', () => {const year = NumberValueObject.create(1900);const month = NumberValueObject.create(1);const day = NumberValueObject.create(0);const result = testFunction.calculate(year, month, day);- expect(transformToValue(result.getArrayValue())).toStrictEqual([[0]]);+ expect(getObjectValue(result)).toBe(0);});+it('Edge case, 1900.1.-1', () => {const year = NumberValueObject.create(1900);const month = NumberValueObject.create(1);const day = NumberValueObject.create(-1);const result = testFunction.calculate(year, month, day);- expect(transformToValue(result.getArrayValue())).toStrictEqual([[ErrorType.NUM]]);+ expect(getObjectValue(result)).toBe(ErrorType.NUM);});it('Year is single cell, month is one column, day is one row', () => {@@ -75,7 +79,10 @@ describe('Test date function', () => {column: 0,});const result = testFunction.calculate(year, month, day);- expect(transformToValue(result.getArrayValue())).toStrictEqual([[45294, 45295, 45296], [45325, 45326, 45327]]);+ expect(getObjectValue(result)).toStrictEqual([+ [45294, 45295, 45296],+ [45325, 45326, 45327],+ ]);});it('Year is array with multiple format values', () => {@@ -94,7 +101,10 @@ describe('Test date function', () => {const month = NumberValueObject.create(1);const day = NumberValueObject.create(1);const result = testFunction.calculate(year, month, day);- expect(transformToValue(result.getArrayValue())).toStrictEqual([[367, '#VALUE!', 367, 367, 1, 1, 6576], [1, 36526, 732, '#VALUE!', '#NUM!', 1, 39448]]);+ expect(getObjectValue(result)).toStrictEqual([+ [367, ErrorType.VALUE, 367, 367, 1, 1, 6576],+ [1, 36526, 732, ErrorType.VALUE, ErrorType.NUM, 1, 39448],+ ]);});it('Month is array with multiple format values', () => {@@ -113,7 +123,10 @@ describe('Test date function', () => {});const day = NumberValueObject.create(1);const result = testFunction.calculate(year, month, day);- expect(transformToValue(result.getArrayValue())).toStrictEqual([[45292, '#VALUE!', 45292, 45292, 45261, 45261], [45261, 48305, 45323, '#VALUE!', 45170, 45689]]);+ expect(getObjectValue(result)).toStrictEqual([+ [45292, ErrorType.VALUE, 45292, 45292, 45261, 45261],+ [45261, 48305, 45323, ErrorType.VALUE, 45170, 45689],+ ]);});it('Day is array with multiple format values', () => {@@ -132,7 +145,10 @@ describe('Test date function', () => {column: 0,});const result = testFunction.calculate(year, month, day);- expect(transformToValue(result.getArrayValue())).toStrictEqual([[45292, '#VALUE!', 45292, 45292, 45291, 45291], [45291, 45391, 45293, '#VALUE!', 45288, 45323]]);+ expect(getObjectValue(result)).toStrictEqual([+ [45292, ErrorType.VALUE, 45292, 45292, 45291, 45291],+ [45291, 45391, 45293, ErrorType.VALUE, 45288, 45323],+ ]);});});});diff --git a/packages/engine-formula/src/functions/date/date/index.ts b/packages/engine-formula/src/functions/date/date/index.tsindex 96c565a33d..1c69914e25 100644--- a/packages/engine-formula/src/functions/date/date/index.ts+++ b/packages/engine-formula/src/functions/date/date/index.ts@@ -14,13 +14,14 @@* limitations under the License.*/+import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';+import type { BaseValueObject } from '../../../engine/value-object/base-value-object';import { DEFAULT_DATE_FORMAT, excelDateSerial } from '../../../basics/date';import { ErrorType } from '../../../basics/error-type';import { expandArrayValueObject } from '../../../engine/utils/array-object';-import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';-import type { BaseValueObject } from '../../../engine/value-object/base-value-object';+import { checkVariantsErrorIsStringToNumber } from '../../../engine/utils/check-variant-error';import { ErrorValueObject } from '../../../engine/value-object/base-value-object';-import { NullValueObject, NumberValueObject } from '../../../engine/value-object/primitive-object';+import { NumberValueObject } from '../../../engine/value-object/primitive-object';import { BaseFunction } from '../../base-function';export class DateFunction extends BaseFunction {@@ -29,59 +30,48 @@ export class DateFunction extends BaseFunction {override maxParams = 3;override calculate(year: BaseValueObject, month: BaseValueObject, day: BaseValueObject) {- if (year.isError()) {- return year;- }-- if (month.isError()) {- return month;- }-- if (day.isError()) {- return day;- }-- // get max row lengthconst maxRowLength = Math.max(year.isArray() ? (year as ArrayValueObject).getRowCount() : 1,month.isArray() ? (month as ArrayValueObject).getRowCount() : 1,day.isArray() ? (day as ArrayValueObject).getRowCount() : 1);-- // get max column lengthconst maxColumnLength = Math.max(year.isArray() ? (year as ArrayValueObject).getColumnCount() : 1,month.isArray() ? (month as ArrayValueObject).getColumnCount() : 1,day.isArray() ? (day as ArrayValueObject).getColumnCount() : 1);- const yearArray = expandArrayValueObject(maxRowLength, maxColumnLength, year);- const monthArray = expandArrayValueObject(maxRowLength, maxColumnLength, month);- const dayArray = expandArrayValueObject(maxRowLength, maxColumnLength, day);+ const yearArray = expandArrayValueObject(maxRowLength, maxColumnLength, year, ErrorValueObject.create(ErrorType.NA));+ const monthArray = expandArrayValueObject(maxRowLength, maxColumnLength, month, ErrorValueObject.create(ErrorType.NA));+ const dayArray = expandArrayValueObject(maxRowLength, maxColumnLength, day, ErrorValueObject.create(ErrorType.NA));- return yearArray.map((yearValueObject, rowIndex, columnIndex) => {- const monthValueObject = monthArray.get(rowIndex, columnIndex) || NullValueObject.create();- const dayValueObject = dayArray.get(rowIndex, columnIndex) || NullValueObject.create();+ const resultArray = yearArray.map((yearObject, rowIndex, columnIndex) => {+ const monthObject = monthArray.get(rowIndex, columnIndex) as BaseValueObject;+ const dayObject = dayArray.get(rowIndex, columnIndex) as BaseValueObject;- if (yearValueObject.isError()) {- return yearValueObject;+ if (yearObject.isError()) {+ return yearObject;}- if (monthValueObject.isError()) {- return monthValueObject;+ if (monthObject.isError()) {+ return monthObject;}- if (dayValueObject.isError()) {- return dayValueObject;+ if (dayObject.isError()) {+ return dayObject;}- if (yearValueObject.isString() || monthValueObject.isString() || dayValueObject.isString()) {- return ErrorValueObject.create(ErrorType.VALUE);+ const { isError, errorObject, variants } = checkVariantsErrorIsStringToNumber(yearObject, monthObject, dayObject);++ if (isError) {+ return errorObject as ErrorValueObject;}- let yearValue = +yearValueObject.getValue();- const monthValue = Math.floor(+monthValueObject.getValue());- const dayValue = +dayValueObject.getValue();+ const [_yearObject, _monthObject, _dayObject] = variants as BaseValueObject[];++ let yearValue = Math.floor(+_yearObject.getValue());+ const monthValue = Math.floor(+_monthObject.getValue());+ const dayValue = Math.floor(+_dayObject.getValue());if (yearValue < 0 || yearValue > 9999) {return ErrorValueObject.create(ErrorType.NUM);@@ -103,5 +93,11 @@ export class DateFunction extends BaseFunction {return valueObject;});++ if (maxRowLength === 1 && maxColumnLength === 1) {+ return (resultArray as ArrayValueObject).get(0, 0) as BaseValueObject;+ }++ return resultArray;}}