-
-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(formula): add choose function (#2613)
* feat(formula): add choose function * fix(formula): numfmt formula sum * fix(formula): choose function ref 0 * fix(formula): choose return reference object
- Loading branch information
Showing
11 changed files
with
317 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
packages/engine-formula/src/functions/lookup/choose/__tests__/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/** | ||
* 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 { ArrayValueObject, transformToValueObject } from '../../../../engine/value-object/array-value-object'; | ||
import { FUNCTION_NAMES_LOOKUP } from '../../function-names'; | ||
import { Choose } from '../index'; | ||
import { NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object'; | ||
import { ErrorType } from '../../../../basics/error-type'; | ||
import { getObjectValue } from '../../../__tests__/create-function-test-bed'; | ||
import { ErrorValueObject } from '../../../../engine/value-object/base-value-object'; | ||
|
||
describe('Test choose function', () => { | ||
const testFunction = new Choose(FUNCTION_NAMES_LOOKUP.CHOOSE); | ||
|
||
describe('Choose', () => { | ||
it('Index num and value', async () => { | ||
const indexNum = NumberValueObject.create(2); | ||
const value1 = NumberValueObject.create(11); | ||
const value2 = NumberValueObject.create(22); | ||
|
||
const resultObject = testFunction.calculate(indexNum, value1, value2); | ||
expect(getObjectValue(resultObject)).toStrictEqual(22); | ||
}); | ||
|
||
it('Index num error', async () => { | ||
const indexNum = ErrorValueObject.create(ErrorType.NAME); | ||
const value1 = NumberValueObject.create(11); | ||
|
||
const resultObject = testFunction.calculate(indexNum, value1); | ||
expect(getObjectValue(resultObject)).toStrictEqual(ErrorType.NAME); | ||
}); | ||
|
||
it('Index num and value, exceeding quantity', async () => { | ||
const indexNum = NumberValueObject.create(3); | ||
const value1 = NumberValueObject.create(11); | ||
const value2 = NumberValueObject.create(22); | ||
|
||
const resultObject = testFunction.calculate(indexNum, value1, value2); | ||
expect(getObjectValue(resultObject)).toStrictEqual(ErrorType.VALUE); | ||
}); | ||
|
||
it('Index num array', async () => { | ||
const indexNum = ArrayValueObject.create(/*ts*/ `{ | ||
1,2,3 | ||
}`); | ||
const value1 = NumberValueObject.create(11); | ||
const value2 = StringValueObject.create('second'); | ||
|
||
const resultObject = testFunction.calculate(indexNum, value1, value2); | ||
expect(getObjectValue(resultObject)).toStrictEqual([[11, 'second', ErrorType.VALUE]]); | ||
}); | ||
|
||
it('Index num number, value1 array', async () => { | ||
const indexNum = NumberValueObject.create(1.9); | ||
|
||
const value1 = ArrayValueObject.create(/*ts*/ `{ | ||
2; | ||
3; | ||
4 | ||
}`); | ||
const resultObject = testFunction.calculate(indexNum, value1); | ||
expect(getObjectValue(resultObject)).toStrictEqual([[2], [3], [4]]); | ||
}); | ||
|
||
it('Index num number negative', async () => { | ||
const indexNum = NumberValueObject.create(-2); | ||
|
||
const value1 = ArrayValueObject.create(/*ts*/ `{ | ||
2; | ||
3; | ||
4 | ||
}`); | ||
const resultObject = testFunction.calculate(indexNum, value1); | ||
expect(getObjectValue(resultObject)).toStrictEqual(ErrorType.VALUE); | ||
}); | ||
|
||
it('Index num number, value1 array with blank cell', async () => { | ||
const indexNum = NumberValueObject.create(1); | ||
|
||
const value1 = ArrayValueObject.create({ | ||
calculateValueList: transformToValueObject([ | ||
[null], | ||
]), | ||
rowCount: 1, | ||
columnCount: 1, | ||
unitId: '', | ||
sheetId: '', | ||
row: 0, | ||
column: 0, | ||
}); | ||
const resultObject = testFunction.calculate(indexNum, value1); | ||
expect(getObjectValue(resultObject)).toStrictEqual([[0]]); | ||
}); | ||
|
||
it('All params with array', async () => { | ||
const indexNum = ArrayValueObject.create(/*ts*/ `{ | ||
1; | ||
2; | ||
3 | ||
}`); | ||
|
||
const value1 = ArrayValueObject.create(/*ts*/ `{ | ||
11,22,33,44 | ||
}`); | ||
|
||
const value2 = ArrayValueObject.create(/*ts*/ `{ | ||
44,77; | ||
55,88; | ||
66,99 | ||
}`); | ||
const value3 = NumberValueObject.create(3); | ||
|
||
const resultObject = testFunction.calculate(indexNum, value1, value2, value3); | ||
expect(getObjectValue(resultObject)).toStrictEqual([[11, 22, 33, 44], [55, 88, ErrorType.NA, ErrorType.NA], [3, 3, 3, 3]]); | ||
}); | ||
}); | ||
}); |
105 changes: 105 additions & 0 deletions
105
packages/engine-formula/src/functions/lookup/choose/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* 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 { ErrorType } from '../../../basics/error-type'; | ||
import type { BaseReferenceObject, FunctionVariantType } from '../../../engine/reference-object/base-reference-object'; | ||
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 Choose extends BaseFunction { | ||
override minParams = 2; | ||
|
||
override maxParams = 255; | ||
|
||
override needsReferenceObject = true; | ||
|
||
override isAddress() { | ||
return true; | ||
} | ||
|
||
override calculate(indexNum: FunctionVariantType, ...variants: FunctionVariantType[]) { | ||
if (indexNum.isError()) { | ||
return indexNum; | ||
} | ||
|
||
if (indexNum.isReferenceObject()) { | ||
indexNum = (indexNum as BaseReferenceObject).toArrayValueObject(); | ||
} | ||
|
||
indexNum = indexNum as BaseValueObject; | ||
|
||
if (!indexNum.isArray()) { | ||
const index = indexNum.convertToNumberObjectValue(); | ||
|
||
if (index.isError()) { | ||
return index; | ||
} | ||
|
||
const variant = variants[Math.trunc(+index.getValue()) - 1] || ErrorValueObject.create(ErrorType.VALUE); | ||
// if (variant.isNull()) { | ||
// variant = NumberValueObject.create(0); | ||
// } | ||
return variant; | ||
} | ||
|
||
// The size of the extended range is determined by the maximum width and height of the criteria range. | ||
let maxRowLength = indexNum.isArray() ? (indexNum as ArrayValueObject).getRowCount() : 1; | ||
let maxColumnLength = indexNum.isArray() ? (indexNum as ArrayValueObject).getColumnCount() : 1; | ||
|
||
variants.forEach((variant, i) => { | ||
if (variant.isArray()) { | ||
const arrayValue = variant as ArrayValueObject; | ||
maxRowLength = Math.max(maxRowLength, arrayValue.getRowCount()); | ||
maxColumnLength = Math.max(maxColumnLength, arrayValue.getColumnCount()); | ||
} else { | ||
maxRowLength = Math.max(maxRowLength, 1); | ||
maxColumnLength = Math.max(maxColumnLength, 1); | ||
} | ||
}); | ||
|
||
const indexNumArray = expandArrayValueObject(maxRowLength, maxColumnLength, indexNum, ErrorValueObject.create(ErrorType.NA)); | ||
const arrayValueObjectList = variants.map((variant) => { | ||
if (variant.isReferenceObject()) { | ||
variant = (variant as BaseReferenceObject).toArrayValueObject(); | ||
} | ||
return expandArrayValueObject(maxRowLength, maxColumnLength, variant as BaseValueObject, ErrorValueObject.create(ErrorType.NA)); | ||
}); | ||
|
||
return indexNumArray.map((indexNumValue, row, column) => { | ||
if (indexNumValue.isError()) { | ||
return indexNumValue; | ||
} | ||
|
||
const index = indexNumValue.convertToNumberObjectValue(); | ||
|
||
if (index.isError()) { | ||
return index; | ||
} | ||
|
||
const arrayValueObject = arrayValueObjectList[Math.trunc(+index.getValue()) - 1]; | ||
|
||
let valueObject = arrayValueObject?.get(row, column) || ErrorValueObject.create(ErrorType.VALUE); | ||
if (valueObject?.isNull()) { | ||
valueObject = NumberValueObject.create(0); | ||
} | ||
return valueObject; | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.