From c533e6d97d7b653916ab9de52525b367894db828 Mon Sep 17 00:00:00 2001 From: Dushusir <1414556676@qq.com> Date: Wed, 31 Jan 2024 04:05:20 +0800 Subject: [PATCH] feat(formula): date function single number --- .../date/date/__tests__/index.spec.ts | 35 +++++++++++++ .../src/functions/date/date/index.ts | 52 +++++++++++++++++++ .../src/functions/date/function-map.ts | 2 + .../src/locale/function-list/date/en-US.ts | 5 +- .../src/locale/function-list/date/ja-JP.ts | 5 +- .../src/locale/function-list/date/zh-CN.ts | 7 +-- .../src/services/function-list/date.ts | 19 ++++--- 7 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 packages/engine-formula/src/functions/date/date/__tests__/index.spec.ts create mode 100644 packages/engine-formula/src/functions/date/date/index.ts 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.ts new file mode 100644 index 00000000000..5a227b91fa6 --- /dev/null +++ b/packages/engine-formula/src/functions/date/date/__tests__/index.spec.ts @@ -0,0 +1,35 @@ +/** + * Copyright 2023-present DreamNum Inc. + * + * 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 { describe, expect, it } from 'vitest'; + +import { FUNCTION_NAMES_DATE } from '../../function-names'; +import { DateFunction } from '..'; +import { NumberValueObject } from '../../../../engine/value-object/primitive-object'; + +describe('Test date function', () => { + const textFunction = new DateFunction(FUNCTION_NAMES_DATE.DATE); + + describe('Date', () => { + it('Value is normal', () => { + const year = new NumberValueObject(2024); + const month = new NumberValueObject(1); + const day = new NumberValueObject(1); + const result = textFunction.calculate(year, month, day); + expect(result.getValue()).toBe(45292); + }); + }); +}); diff --git a/packages/engine-formula/src/functions/date/date/index.ts b/packages/engine-formula/src/functions/date/date/index.ts new file mode 100644 index 00000000000..05d96675afa --- /dev/null +++ b/packages/engine-formula/src/functions/date/date/index.ts @@ -0,0 +1,52 @@ +/** + * Copyright 2023-present DreamNum Inc. + * + * 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 { BaseValueObject } from '../../..'; +import { ErrorType, ErrorValueObject, NumberValueObject } from '../../..'; +import { DEFFAULT_DATE_FORMAT, excelDateSerial } from '../../../basics/date'; +import { BaseFunction } from '../../base-function'; + +export class DateFunction extends BaseFunction { + override calculate(year: BaseValueObject, month: BaseValueObject, day: BaseValueObject) { + if (year.isError()) { + return year; + } + + if (month.isError()) { + return month; + } + + if (day.isError()) { + return day; + } + + // TODO@Dushusir: array + const yearValue = +year.getValue(); + const monthValue = +month.getValue(); + const dayValue = +day.getValue(); + + const date = new Date(yearValue, monthValue - 1, dayValue); + + if (date.getMonth() !== monthValue - 1 || date.getDate() !== dayValue) { + return new ErrorValueObject(ErrorType.VALUE); + } + + const currentSerial = excelDateSerial(date); + const valueObject = new NumberValueObject(currentSerial); + valueObject.setPattern(DEFFAULT_DATE_FORMAT); + return valueObject; + } +} diff --git a/packages/engine-formula/src/functions/date/function-map.ts b/packages/engine-formula/src/functions/date/function-map.ts index f839d6668ca..b693135fd1f 100644 --- a/packages/engine-formula/src/functions/date/function-map.ts +++ b/packages/engine-formula/src/functions/date/function-map.ts @@ -14,9 +14,11 @@ * limitations under the License. */ +import { DateFunction } from './date'; import { FUNCTION_NAMES_DATE } from './function-names'; import { Today } from './today'; export const functionDate = [ [Today, FUNCTION_NAMES_DATE.TODAY], + [DateFunction, FUNCTION_NAMES_DATE.DATE], ]; diff --git a/packages/sheets-formula/src/locale/function-list/date/en-US.ts b/packages/sheets-formula/src/locale/function-list/date/en-US.ts index 849c5b1f8d4..9fbd5aec972 100644 --- a/packages/sheets-formula/src/locale/function-list/date/en-US.ts +++ b/packages/sheets-formula/src/locale/function-list/date/en-US.ts @@ -25,8 +25,9 @@ export default { }, ], functionParameter: { - number1: { name: 'number1', detail: 'first' }, - number2: { name: 'number2', detail: 'second' }, + year: { name: 'year', detail: 'The value of the year argument can include one to four digits. Excel interprets the year argument according to the date system your computer is using. By default, Univer uses the 1900 date system, which means the first date is January 1, 1900.' }, + month: { name: 'month', detail: 'A positive or negative integer representing the month of the year from 1 to 12 (January to December).' }, + day: { name: 'day', detail: 'A positive or negative integer representing the day of the month from 1 to 31.' }, }, }, DATEDIF: { diff --git a/packages/sheets-formula/src/locale/function-list/date/ja-JP.ts b/packages/sheets-formula/src/locale/function-list/date/ja-JP.ts index b466f0afdff..84f11d88e40 100644 --- a/packages/sheets-formula/src/locale/function-list/date/ja-JP.ts +++ b/packages/sheets-formula/src/locale/function-list/date/ja-JP.ts @@ -25,8 +25,9 @@ export default { }, ], functionParameter: { - number1: { name: 'number1', detail: 'first' }, - number2: { name: 'number2', detail: 'second' }, + year: { name: '年', detail: 'year 引数の値 には 、1 ~ 4 桁の数字を指定できます。 Excel は、コンピューター が使用 している日付システムに応じて年の引数を解釈します。 既定では、Univer では 1900 年の日付システムが使用されます。つまり、最初の日付は 1900 年 1 月 1 日です。' }, + month: { name: '月', detail: '1 ~ 12 (1 月から 12 月) の月を表す正または負の整数です。' }, + day: { name: '日', detail: '1 ~ 31 の月の日を表す正または負の整数です。' }, }, }, DATEDIF: { diff --git a/packages/sheets-formula/src/locale/function-list/date/zh-CN.ts b/packages/sheets-formula/src/locale/function-list/date/zh-CN.ts index 58fc98484f8..69724365a32 100644 --- a/packages/sheets-formula/src/locale/function-list/date/zh-CN.ts +++ b/packages/sheets-formula/src/locale/function-list/date/zh-CN.ts @@ -16,7 +16,7 @@ export default { DATE: { - description: '返回特定日期的序列号', + description: '采用三个单独的值并将它们合并为一个日期。', abstract: '返回特定日期的序列号', links: [ { @@ -25,8 +25,9 @@ export default { }, ], functionParameter: { - number1: { name: 'number1', detail: 'first' }, - number2: { name: 'number2', detail: 'second' }, + year: { name: '年', detail: '可以包含 1 到 4 位数字。 Excel 根据计算机使用的日期系统解释 year 参数。 默认情况下,Univer 使用 1900 日期系统,这意味着第一个日期是 1900 年 1 月 1 日。' }, + month: { name: '月', detail: '一个正整数或负整数,表示一年中从 1 月至 12 月(一月到十二月)的各个月。' }, + day: { name: '日', detail: '一个正整数或负整数,表示一月中从 1 日到 31 日的各天。' }, }, }, DATEDIF: { diff --git a/packages/sheets-formula/src/services/function-list/date.ts b/packages/sheets-formula/src/services/function-list/date.ts index f48dcb40b1b..6a91c76fdf5 100644 --- a/packages/sheets-formula/src/services/function-list/date.ts +++ b/packages/sheets-formula/src/services/function-list/date.ts @@ -24,16 +24,23 @@ export const FUNCTION_LIST_DATE: IFunctionInfo[] = [ abstract: 'formula.functionList.DATE.abstract', functionParameter: [ { - name: 'formula.functionList.DATE.functionParameter.number1.name', - detail: 'formula.functionList.DATE.functionParameter.number1.detail', - example: 'A1:A20', + name: 'formula.functionList.DATE.functionParameter.year.name', + detail: 'formula.functionList.DATE.functionParameter.year.detail', + example: '2024', require: 1, repeat: 0, }, { - name: 'formula.functionList.DATE.functionParameter.number2.name', - detail: 'formula.functionList.DATE.functionParameter.number2.detail', - example: 'A1:A20', + name: 'formula.functionList.DATE.functionParameter.month.name', + detail: 'formula.functionList.DATE.functionParameter.month.detail', + example: '1', + require: 1, + repeat: 0, + }, + { + name: 'formula.functionList.DATE.functionParameter.day.name', + detail: 'formula.functionList.DATE.functionParameter.day.detail', + example: '1', require: 1, repeat: 0, },