Skip to content

Commit

Permalink
fix(formula): fix date type
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored and Gggpound committed Aug 27, 2024
1 parent 9ae0bb8 commit 9e755b4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
6 changes: 5 additions & 1 deletion packages/engine-formula/src/basics/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,15 @@ export function getDateSerialNumberByObject(serialNumberObject: BaseValueObject)
return ErrorValueObject.create(ErrorType.VALUE);
}

if (dateSerial instanceof Date) {
dateSerial = excelDateTimeSerial(dateSerial);
}

if (+dateSerial < 0 || +dateSerial > 2958465) { // 2958465 = 9999/12/31
return ErrorValueObject.create(ErrorType.NUM);
}

return dateSerial;
return +dateSerial;
} else {
const dateSerial = +serialNumberObject.getValue();

Expand Down
13 changes: 9 additions & 4 deletions packages/engine-formula/src/functions/date/datevalue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { isDate, parseFormattedValue } from '../../../basics/date';
import { excelDateTimeSerial, isDate, parseFormattedValue } from '../../../basics/date';
import { ErrorType } from '../../../basics/error-type';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
Expand Down Expand Up @@ -43,9 +43,14 @@ export class Datevalue extends BaseFunction {
const value = `${dateTextObject.getValue()}`;
const parsedDate = parseFormattedValue(value);
if (parsedDate) {
const { v, z } = parsedDate;
if (isDate(z)) {
return NumberValueObject.create(Math.trunc(v));
let { v, z } = parsedDate;

if (z && isDate(z)) {
if (v instanceof Date) {
v = excelDateTimeSerial(v);
}

return NumberValueObject.create(Math.trunc(+v));
}
}
}
Expand Down
12 changes: 8 additions & 4 deletions packages/engine-formula/src/functions/date/timevalue/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

import { isDate, parseFormattedValue } from '../../../basics/date';
import { excelDateTimeSerial, isDate, parseFormattedValue } from '../../../basics/date';
import { ErrorType } from '../../../basics/error-type';
import { getFractionalPart } from '../../../engine/utils/math-kit';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
Expand Down Expand Up @@ -44,10 +44,14 @@ export class Timevalue extends BaseFunction {
const value = `${timeTextObject.getValue()}`;
const parsedTime = parseFormattedValue(value);
if (parsedTime) {
const { v, z } = parsedTime;
let { v, z } = parsedTime;

if (isDate(z)) {
return NumberValueObject.create(getFractionalPart(v));
if (z && isDate(z)) {
if (v instanceof Date) {
v = excelDateTimeSerial(v);
}

return NumberValueObject.create(getFractionalPart(+v));
}
}
}
Expand Down

0 comments on commit 9e755b4

Please sign in to comment.