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

fix(formula): subtotal count #2452

Merged
merged 11 commits into from
Jun 21, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import { Minus } from '../../../functions/meta/minus';
import { createCommandTestBed } from './create-command-test-bed';

describe('Test indirect', () => {
// const textFunction = new Makearray(FUNCTION_NAMES_LOGICAL.MAKEARRAY);
let get: Injector['get'];
let lexer: Lexer;
let astTreeBuilder: AstTreeBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,8 @@ export class FormulaDependencyGenerator extends Disposable {
}

private _generateAstNode(unitId: string, formulaString: string, refOffsetX: number = 0, refOffsetY: number = 0) {
let astNode: Nullable<AstRootNode> = this._formulaASTCache.get(`${unitId}${formulaString}##${refOffsetX}${refOffsetY}`);
// refOffsetX and refOffsetY are separated by -, otherwise x:1 y:10 will be repeated with x:11 y:0
let astNode: Nullable<AstRootNode> = this._formulaASTCache.get(`${unitId}${formulaString}##${refOffsetX}-${refOffsetY}`);

if (astNode && !this._isDirtyDefinedForNode(astNode)) {
return astNode;
Expand All @@ -377,7 +378,7 @@ export class FormulaDependencyGenerator extends Disposable {
throw new Error('astNode is null');
}

this._formulaASTCache.set(`${unitId}${formulaString}##${refOffsetX}${refOffsetY}`, astNode);
this._formulaASTCache.set(`${unitId}${formulaString}##${refOffsetX}-${refOffsetY}`, astNode);

return astNode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,21 +313,21 @@ describe('Test nested functions', () => {
});

describe('Normal', () => {
it('Nested functions IFERROR,XLOOKUP,MAX,SUMIFS,EDATE,TODAY,DAY,PLUS,Minus,CONCATENATE', async () => {
it('Nested functions IFERROR,XLOOKUP,MAX,SUMIFS,EDATE,TODAY,DAY,PLUS,Minus,CONCATENATE', () => {
const lexerNode = lexer.treeBuilder('=IFERROR(XLOOKUP(MAX(SUMIFS(C2:C10, A2:A10, ">="&EDATE(TODAY(),-1)+1-DAY(TODAY()), A2:A10, "<"&TODAY()-DAY(TODAY())+1)), SUMIFS(C2:C10, A2:A10, ">="&EDATE(TODAY(),-1)+1-DAY(TODAY()), A2:A10, "<"&TODAY()-DAY(TODAY())+1), B2:B10, "No Data"), "No Data")');

const astNode = astTreeBuilder.parse(lexerNode as LexerNode);

const result = await interpreter.executeAsync(astNode as BaseAstNode);
const result = interpreter.execute(astNode as BaseAstNode);

expect((result as ArrayValueObject).toValue()).toStrictEqual([[101], [102], [103], [104], [105], [101], [102], [103], [104]]);
});
it('Nested functions ADDRESS,XMATCH,MIN,SUMIFS,EDATE,TODAY,DAY', async () => {
it('Nested functions ADDRESS,XMATCH,MIN,SUMIFS,EDATE,TODAY,DAY', () => {
const lexerNode = lexer.treeBuilder('=ADDRESS(XMATCH(MIN(SUMIFS(C2:C10, A2:A10, ">=" & EDATE(TODAY(), -1) + 1 - DAY(TODAY()), A2:A10, "<" & TODAY() - DAY(TODAY()) + 1)), SUMIFS(C2:C10, A2:A10, ">=" & EDATE(TODAY(), -1) + 1 - DAY(TODAY()), A2:A10, "<" & TODAY() - DAY(TODAY()) + 1), 0) + 1, 2)');

const astNode = astTreeBuilder.parse(lexerNode as LexerNode);

const result = await interpreter.executeAsync(astNode as BaseAstNode);
const result = interpreter.execute(astNode as BaseAstNode);

expect(getObjectValue(result)).toStrictEqual([['$B$2']]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,35 +23,35 @@ import { ArrayValueObject, transformToValue, transformToValueObject } from '../.
import { ErrorType } from '../../../../basics/error-type';

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

describe('Date', () => {
it('All value is normal', () => {
const year = NumberValueObject.create(2024);
const month = NumberValueObject.create(1);
const day = NumberValueObject.create(1);
const result = textFunction.calculate(year, month, day);
const result = testFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[45292]]);
});
it('Edge case, 1900.1.1', () => {
const year = NumberValueObject.create(1900);
const month = NumberValueObject.create(1);
const day = NumberValueObject.create(1);
const result = textFunction.calculate(year, month, day);
const result = testFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[1]]);
});
it('Edge case, 1900.1.0', () => {
const year = NumberValueObject.create(1900);
const month = NumberValueObject.create(1);
const day = NumberValueObject.create(0);
const result = textFunction.calculate(year, month, day);
const result = testFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[0]]);
});
it('Edge case, 1900.1.-1', () => {
const year = NumberValueObject.create(1900);
const month = NumberValueObject.create(1);
const day = NumberValueObject.create(-1);
const result = textFunction.calculate(year, month, day);
const result = testFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[ErrorType.NUM]]);
});

Expand All @@ -75,7 +75,7 @@ describe('Test date function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(year, month, day);
const result = testFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[45294, 45295, 45296], [45325, 45326, 45327]]);
});

Expand All @@ -94,7 +94,7 @@ describe('Test date function', () => {
});
const month = NumberValueObject.create(1);
const day = NumberValueObject.create(1);
const result = textFunction.calculate(year, month, day);
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]]);
});

Expand All @@ -113,7 +113,7 @@ describe('Test date function', () => {
column: 0,
});
const day = NumberValueObject.create(1);
const result = textFunction.calculate(year, month, day);
const result = testFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[45292, '#VALUE!', 45292, 45292, 45261, 45261], [45261, 48305, 45323, '#VALUE!', 45170, 45689]]);
});

Expand All @@ -132,7 +132,7 @@ describe('Test date function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(year, month, day);
const result = testFunction.calculate(year, month, day);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[45292, '#VALUE!', 45292, 45292, 45291, 45291], [45291, 45391, 45293, '#VALUE!', 45288, 45323]]);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import { NumberValueObject, StringValueObject } from '../../../../engine/value-o
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';

describe('Test day function', () => {
const textFunction = new Day(FUNCTION_NAMES_DATE.DAY);
const testFunction = new Day(FUNCTION_NAMES_DATE.DAY);

describe('Day', () => {
it('Serial number is normal', () => {
const serialNumber = NumberValueObject.create(43832);
const result = textFunction.calculate(serialNumber);
const result = testFunction.calculate(serialNumber);
expect(result.getValue()).toStrictEqual(2);
});

it('Serial number is date string', () => {
const serialNumber = StringValueObject.create('2020-01-02');
const result = textFunction.calculate(serialNumber);
const result = testFunction.calculate(serialNumber);
expect(result.getValue()).toStrictEqual(2);
});

Expand All @@ -48,7 +48,7 @@ describe('Test day function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(serialNumber);
const result = testFunction.calculate(serialNumber);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[1, '#VALUE!', 1, 1, 0, 0], [0, 9, 2, '#VALUE!', '#NUM!', 14]]);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ import { NumberValueObject } from '../../../../engine/value-object/primitive-obj
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';

describe('Test edate function', () => {
const textFunction = new Edate(FUNCTION_NAMES_DATE.EDATE);
const testFunction = new Edate(FUNCTION_NAMES_DATE.EDATE);

describe('Edate', () => {
it('All value is normal', () => {
const startDate = NumberValueObject.create(43831);
const months = NumberValueObject.create(1);
const result = textFunction.calculate(startDate, months);
const result = testFunction.calculate(startDate, months);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[43862]]);
});

Expand All @@ -43,7 +43,7 @@ describe('Test edate function', () => {
column: 0,
});
const months = NumberValueObject.create(1);
const result = textFunction.calculate(startDate, months);
const result = testFunction.calculate(startDate, months);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[43863], [43864]]);
});

Expand All @@ -61,7 +61,7 @@ describe('Test edate function', () => {
column: 0,
});
const months = NumberValueObject.create(1);
const result = textFunction.calculate(startDate, months);
const result = testFunction.calculate(startDate, months);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[32, '#VALUE!', 32, '#VALUE!', '#VALUE!', 31], [31, 130, 33, '#VALUE!', '#NUM!', 1931]]);
});

Expand All @@ -76,7 +76,7 @@ describe('Test edate function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(startDate, months);
const result = testFunction.calculate(startDate, months);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[43862], [43891]]);
});

Expand All @@ -92,7 +92,7 @@ describe('Test edate function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(startDate, months);
const result = testFunction.calculate(startDate, months);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[43862, '#VALUE!', 43862, '#VALUE!', '#VALUE!', 43831], [43831, 46874, 43891, '#VALUE!', 43739, 101660]]);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import { NumberValueObject, StringValueObject } from '../../../../engine/value-o
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';

describe('Test month function', () => {
const textFunction = new Month(FUNCTION_NAMES_DATE.MONTH);
const testFunction = new Month(FUNCTION_NAMES_DATE.MONTH);

describe('Month', () => {
it('Serial number is normal', () => {
const serialNumber = NumberValueObject.create(43831);
const result = textFunction.calculate(serialNumber);
const result = testFunction.calculate(serialNumber);
expect(result.getValue()).toStrictEqual(1);
});

it('Serial number is date string', () => {
const serialNumber = StringValueObject.create('2020-01-02');
const result = textFunction.calculate(serialNumber);
const result = testFunction.calculate(serialNumber);
expect(result.getValue()).toStrictEqual(1);
});

Expand All @@ -48,7 +48,7 @@ describe('Test month function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(serialNumber);
const result = testFunction.calculate(serialNumber);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[1, '#VALUE!', 1, 1, 1, 1], [1, 4, 1, '#VALUE!', '#NUM!', 3]]);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ global.Date = vi.fn((...params) => {
global.Date.UTC = _Date.UTC;

describe('Test today function', () => {
const textFunction = new Today(FUNCTION_NAMES_DATE.TODAY);
const testFunction = new Today(FUNCTION_NAMES_DATE.TODAY);

describe('Today', () => {
it('Normal', () => {
const result = textFunction.calculate();
const result = testFunction.calculate();
expect(result.getValue()).toBe(43831);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ import { NumberValueObject, StringValueObject } from '../../../../engine/value-o
import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';

describe('Test year function', () => {
const textFunction = new Year(FUNCTION_NAMES_DATE.YEAR);
const testFunction = new Year(FUNCTION_NAMES_DATE.YEAR);

describe('Year', () => {
it('Serial number is normal', () => {
const serialNumber = NumberValueObject.create(43831);
const result = textFunction.calculate(serialNumber);
const result = testFunction.calculate(serialNumber);
expect(result.getValue()).toStrictEqual(2020);
});
it('Serial number is date string', () => {
const serialNumber = StringValueObject.create('2020-01-02');
const result = textFunction.calculate(serialNumber);
const result = testFunction.calculate(serialNumber);
expect(result.getValue()).toStrictEqual(2020);
});

Expand All @@ -47,7 +47,7 @@ describe('Test year function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(serialNumber);
const result = testFunction.calculate(serialNumber);
expect(transformToValue(result.getArrayValue())).toStrictEqual([[1900, '#VALUE!', 1900, 1900, 1900, 1900], [1900, 1900, 1900, '#VALUE!', '#NUM!', 1905]]);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,42 @@ import { ErrorType } from '../../../../basics/error-type';
import { Isblank } from '../index';

describe('Test isblank function', () => {
const textFunction = new Isblank(FUNCTION_NAMES_INFORMATION.ISBLANK);
const testFunction = new Isblank(FUNCTION_NAMES_INFORMATION.ISBLANK);

describe('Isblank', () => {
it('value null', () => {
const value = NullValueObject.create();
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(result.getValue()).toBe(true);
});

it('value error', () => {
const value = ErrorValueObject.create(ErrorType.NA);
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(result.getValue()).toBe(false);
});

it('value boolean', () => {
const value = BooleanValueObject.create(true);
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(result.getValue()).toBe(false);
});

it('value string', () => {
const value = StringValueObject.create('a1');
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(result.getValue()).toBe(false);
});

it('value number 1', () => {
const value = NumberValueObject.create(1);
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(result.getValue()).toBe(false);
});

it('value number 0', () => {
const value = NumberValueObject.create(0);
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(result.getValue()).toBe(false);
});

Expand All @@ -76,7 +76,7 @@ describe('Test isblank function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(transformToValue(result.getArrayValue())).toStrictEqual([
[false],
[true],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import { ErrorType } from '../../../../basics/error-type';
import { Iserr } from '../index';

describe('Test iserr function', () => {
const textFunction = new Iserr(FUNCTION_NAMES_INFORMATION.ISERR);
const testFunction = new Iserr(FUNCTION_NAMES_INFORMATION.ISERR);

describe('Iserr', () => {
it('value error', () => {
const value = ErrorValueObject.create(ErrorType.NA);
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(result.getValue()).toBe(false);
});

Expand All @@ -45,7 +45,7 @@ describe('Test iserr function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(transformToValue(result.getArrayValue())).toStrictEqual([
[false, false, false, false, false, false, false, true, true, true],
[false, false, false, false, false, true, true, true, true, false],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ import { ErrorType } from '../../../../basics/error-type';
import { Iserror } from '../index';

describe('Test iserror function', () => {
const textFunction = new Iserror(FUNCTION_NAMES_INFORMATION.ISERROR);
const testFunction = new Iserror(FUNCTION_NAMES_INFORMATION.ISERROR);

describe('Iserror', () => {
it('value error', () => {
const value = ErrorValueObject.create(ErrorType.NA);
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(result.getValue()).toBe(true);
});

Expand All @@ -45,7 +45,7 @@ describe('Test iserror function', () => {
row: 0,
column: 0,
});
const result = textFunction.calculate(value);
const result = testFunction.calculate(value);
expect(transformToValue(result.getArrayValue())).toStrictEqual([
[false, false, false, false, false, false, true, true, true, true],
[false, false, false, false, false, true, true, true, true, false],
Expand Down
Loading