Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(formula): today function, set numfmt data #1295

Merged
merged 24 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
3fdffa5
feat(formula): today function, set numfmt data
Dushusir Jan 29, 2024
8b1fd3d
feat(formula): supports numfmt
Dushusir Jan 30, 2024
149152b
test(formula): set numfmt data
Dushusir Jan 30, 2024
26a9f6e
fix(formula): sum get numfmt from link cell
Dushusir Jan 30, 2024
616d6e7
fix(sheet): recover status bar
Dushusir Jan 30, 2024
8d2c5ea
fix(formula): set numfmt on formula cell
Dushusir Jan 30, 2024
8ce958c
feat(formula): date function single number
Dushusir Jan 30, 2024
9b3a463
feat(formula): date function with array
Dushusir Jan 31, 2024
4ecdf50
fix(formula): date function test, link to formatted cell supports format
Dushusir Jan 31, 2024
de9cc3e
fix(formula): date serial calculation
Dushusir Feb 3, 2024
814be41
fix(formula): function calculate null params, abs gets 0 when linked …
Dushusir Feb 3, 2024
b6cb67b
fix(formula): concatenate calculate zero value
Dushusir Feb 3, 2024
5cc3063
fix(formula): sum,max,min .etc add blank params check
Dushusir Feb 3, 2024
8aa1fb7
test(formula): new test template
Dushusir Feb 3, 2024
bb769ba
fix(formula): date function gets error when date before 1900.1.1
Dushusir Feb 3, 2024
dc4c6bd
feat(formula): add edate function
Dushusir Feb 6, 2024
df2b6ca
test(formula): test edate function
Dushusir Feb 6, 2024
7e49aea
feat(formula): year,month,day functions
Dushusir Feb 6, 2024
9598eb5
feat(formula): match, xmatch functions
Dushusir Feb 6, 2024
ba2c693
test(formula): xmatch function test case
Dushusir Feb 6, 2024
ddebc78
test(formula): nested functions test
Dushusir Feb 6, 2024
644fbb4
fix(formula): formula cell shows format, cell calculation sets percen…
Dushusir Feb 6, 2024
28e2f49
fix(formula): edate,month,year functions support blank cell
Dushusir Feb 6, 2024
38d9310
fix(formula): xmatch description
Dushusir Feb 6, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(formula): date function with array
  • Loading branch information
Dushusir committed Feb 19, 2024
commit 9b3a46338268a6863fd4754209e92d4465d1ea45
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,99 @@ import { describe, expect, it } from 'vitest';
import { FUNCTION_NAMES_DATE } from '../../function-names';
import { DateFunction } from '..';
import { NumberValueObject } from '../../../../engine/value-object/primitive-object';
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';

describe('Test date function', () => {
const textFunction = new DateFunction(FUNCTION_NAMES_DATE.DATE);

describe('Date', () => {
it('Value is normal', () => {
it('All 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);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[45292]]);
});

it('Year is single cell, month is one column, day is one row', () => {
const year = new NumberValueObject(2024);
const month = new ArrayValueObject({
calculateValueList: transformToValueObject([[1], [2]]),
rowCount: 2,
columnCount: 1,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const day = new ArrayValueObject({
calculateValueList: transformToValueObject([[3, 4, 5]]),
rowCount: 1,
columnCount: 3,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const result = textFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[45294, 45295, 45296], [45325, 45326, 45327]]);
});

it('Year is array with multiple format values', () => {
const year = new ArrayValueObject({
calculateValueList: transformToValueObject([
[1, ' ', 1.23, true, false, null, 18],
[0, '100', '2.34', 'test', -3, 1900, 108],
]),
rowCount: 2,
columnCount: 7,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const month = new NumberValueObject(1);
const day = new NumberValueObject(1);
const result = textFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[367, '#VALUE!', 367, 367, 1, 1, 6576], [1, 36526, 732, '#VALUE!', '#NUM!', 1, 39448]]);
});

it('Month is array with multiple format values', () => {
const year = new NumberValueObject(2024);
const month = new ArrayValueObject({
calculateValueList: transformToValueObject([
[1, ' ', 1.23, true, false, null],
[0, '100', '2.34', 'test', -3, 14],
]),
rowCount: 2,
columnCount: 6,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const day = new NumberValueObject(1);
const result = textFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[45292, '#VALUE!', 45292, 45292, 45261, 45261], [45261, 48305, 45323, '#VALUE!', 45170, 45689]]);
});

it('Day is array with multiple format values', () => {
const year = new NumberValueObject(2024);
const month = new NumberValueObject(1);
const day = new ArrayValueObject({
calculateValueList: transformToValueObject([
[1, ' ', 1.23, true, false, null],
[0, '100', '2.34', 'test', -3, 32],
]),
rowCount: 2,
columnCount: 6,
unitId: '',
sheetId: '',
row: 0,
column: 0,
});
const result = textFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[45292, '#VALUE!', 45292, 45292, 45291, 45291], [45291, 45391, 45293, '#VALUE!', 45288, 45323]]);
});
});
});
75 changes: 61 additions & 14 deletions packages/engine-formula/src/functions/date/date/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
* limitations under the License.
*/

import type { BaseValueObject } from '../../..';
import { ErrorType, ErrorValueObject, NumberValueObject } from '../../..';
import { DEFFAULT_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 { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';

export class DateFunction extends BaseFunction {
Expand All @@ -33,20 +37,63 @@ export class DateFunction extends BaseFunction {
return day;
}

// TODO@Dushusir: array
const yearValue = +year.getValue();
const monthValue = +month.getValue();
const dayValue = +day.getValue();
// get max row length
const maxRowLength = Math.max(
year.isArray() ? (year as ArrayValueObject).getRowCount() : 1,
month.isArray() ? (month as ArrayValueObject).getRowCount() : 1,
day.isArray() ? (day as ArrayValueObject).getRowCount() : 1
);

const date = new Date(yearValue, monthValue - 1, dayValue);
// get max column length
const maxColumnLength = Math.max(
year.isArray() ? (year as ArrayValueObject).getColumnCount() : 1,
month.isArray() ? (month as ArrayValueObject).getColumnCount() : 1,
day.isArray() ? (day as ArrayValueObject).getColumnCount() : 1
);

if (date.getMonth() !== monthValue - 1 || date.getDate() !== dayValue) {
return new ErrorValueObject(ErrorType.VALUE);
}
const yearArray = expandArrayValueObject(maxRowLength, maxColumnLength, year);
const monthArray = expandArrayValueObject(maxRowLength, maxColumnLength, month);
const dayArray = expandArrayValueObject(maxRowLength, maxColumnLength, day);

return yearArray.map((yearValueObject, rowIndex, columnIndex) => {
const monthValueObject = monthArray.get(rowIndex, columnIndex);
const dayValueObject = dayArray.get(rowIndex, columnIndex);

if (yearValueObject.isError()) {
return yearValueObject;
}

if (monthValueObject.isError()) {
return monthValueObject;
}

if (dayValueObject.isError()) {
return dayValueObject;
}

if (yearValueObject.isString() || monthValueObject.isString() || dayValueObject.isString()) {
return new ErrorValueObject(ErrorType.VALUE);
}

let yearValue = +yearValueObject.getValue();
const monthValue = Math.floor(+monthValueObject.getValue());
const dayValue = +dayValueObject.getValue();

if (yearValue < 0 || yearValue > 9999) {
return new ErrorValueObject(ErrorType.NUM);
}

if (yearValue >= 0 && yearValue < 1899) {
yearValue += 1900;
}

const date = new Date(yearValue, monthValue - 1, dayValue);

const currentSerial = excelDateSerial(date);
const valueObject = new NumberValueObject(currentSerial);
valueObject.setPattern(DEFFAULT_DATE_FORMAT);

const currentSerial = excelDateSerial(date);
const valueObject = new NumberValueObject(currentSerial);
valueObject.setPattern(DEFFAULT_DATE_FORMAT);
return valueObject;
return valueObject;
});
}
}