-
-
Notifications
You must be signed in to change notification settings - Fork 504
/
index.ts
96 lines (76 loc) · 3.71 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* 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:https://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 { DEFAULT_DATE_FORMAT, excelDateSerial, excelSerialToDate } 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 { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NullValueObject, NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
/**
* TODO@Dushusir: support plaine text date: =EDATE("2020-1-1",1), =EDATE("2020/1/1",1) and other formats
*/
export class Edate extends BaseFunction {
override minParams = 2;
override maxParams = 2;
override calculate(startDate: BaseValueObject, months: BaseValueObject) {
if (startDate.isError()) {
return startDate;
}
if (months.isError()) {
return months;
}
// get max row length
const maxRowLength = Math.max(
startDate.isArray() ? (startDate as ArrayValueObject).getRowCount() : 1,
months.isArray() ? (months as ArrayValueObject).getRowCount() : 1
);
// get max column length
const maxColumnLength = Math.max(
startDate.isArray() ? (startDate as ArrayValueObject).getColumnCount() : 1,
months.isArray() ? (months as ArrayValueObject).getColumnCount() : 1
);
const startDateArray = expandArrayValueObject(maxRowLength, maxColumnLength, startDate);
const monthsArray = expandArrayValueObject(maxRowLength, maxColumnLength, months);
return startDateArray.map((startDateObject, rowIndex, columnIndex) => {
const monthsValueObject = monthsArray.get(rowIndex, columnIndex) || NullValueObject.create();
if (startDateObject.isError()) {
return startDateObject;
}
if (monthsValueObject.isError()) {
return monthsValueObject;
}
if (startDateObject.isString() || startDateObject.isBoolean() || monthsValueObject.isString() || monthsValueObject.isBoolean()) {
return ErrorValueObject.create(ErrorType.VALUE);
}
const startDateSerial = +startDateObject.getValue();
if (startDateSerial < 0) {
return ErrorValueObject.create(ErrorType.NUM);
}
const monthsValue = Math.floor(+monthsValueObject.getValue());
const startDate = excelSerialToDate(startDateSerial);
const year = startDate.getUTCFullYear();
const month = startDate.getUTCMonth() + monthsValue;
const day = startDate.getUTCDate();
const resultDate = new Date(Date.UTC(year, month, day));
const currentSerial = excelDateSerial(resultDate);
const valueObject = NumberValueObject.create(currentSerial);
valueObject.setPattern(DEFAULT_DATE_FORMAT);
return valueObject;
});
}
}