-
-
Notifications
You must be signed in to change notification settings - Fork 460
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
267 additions
and
19 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
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
141 changes: 141 additions & 0 deletions
141
packages/engine-formula/src/functions/text/text/__test__/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,141 @@ | ||
/** | ||
* 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 { FUNCTION_NAMES_TEXT } from '../../function-names'; | ||
import { Text } from '..'; | ||
import { NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object'; | ||
import type { ArrayValueObject } from '../../../../engine/value-object/array-value-object'; | ||
import { transformToValue } from '../../../../engine/value-object/array-value-object'; | ||
|
||
describe('Test text function', () => { | ||
const textFunction = new Text(FUNCTION_NAMES_TEXT.TEXT); | ||
|
||
// function getArrayValueObjectPattern(arrayValue: ArrayValueObject) { | ||
// return arrayValue.mapValue((value) => { | ||
// return value.getPattern(); | ||
// }) | ||
// } | ||
|
||
describe('Text', () => { | ||
it('Text is single cell, format text is single cell', () => { | ||
const text1 = NumberValueObject.create(111); | ||
const formatText = StringValueObject.create('$#,##0.00'); | ||
const result = textFunction.calculate(text1, formatText); | ||
const resultArray = result.getArrayValue(); | ||
expect(transformToValue(resultArray)).toStrictEqual([[111]]); | ||
expect((result as ArrayValueObject).getFirstCell().getPattern()).toStrictEqual('$#,##0.00'); | ||
}); | ||
|
||
// it('Text1 is single cell, text2 is array', () => { | ||
// const text1 = StringValueObject.create('a'); | ||
// const text2 = ArrayValueObject.create({ | ||
// calculateValueList: transformToValueObject([ | ||
// [1, 2, 3], | ||
// [2, 3, 4], | ||
// [3, 4, 5], | ||
// ]), | ||
// rowCount: 3, | ||
// columnCount: 3, | ||
// unitId: '', | ||
// sheetId: '', | ||
// row: 0, | ||
// column: 0, | ||
// }); | ||
// const result = textFunction.calculate(text1, text2); | ||
// expect(transformToValue(result.getArrayValue())).toStrictEqual([['a1', 'a2', 'a3'], ['a2', 'a3', 'a4'], ['a3', 'a4', 'a5']]); | ||
// }); | ||
|
||
// it('Text1 is array, text2 is single cell', () => { | ||
// const text1 = ArrayValueObject.create({ | ||
// calculateValueList: transformToValueObject([ | ||
// [1, 2, 3], | ||
// [2, 3, 4], | ||
// [3, 4, 5], | ||
// ]), | ||
// rowCount: 3, | ||
// columnCount: 3, | ||
// unitId: '', | ||
// sheetId: '', | ||
// row: 0, | ||
// column: 0, | ||
// }); | ||
// const text2 = StringValueObject.create('a'); | ||
// const result = textFunction.calculate(text1, text2); | ||
// expect(transformToValue(result.getArrayValue())).toStrictEqual([['1a', '2a', '3a'], ['2a', '3a', '4a'], ['3a', '4a', '5a']]); | ||
// }); | ||
|
||
// it('Text1 is 3*1 array, text2 is 1*3 array', () => { | ||
// const text1 = ArrayValueObject.create({ | ||
// calculateValueList: transformToValueObject([ | ||
// ['a'], | ||
// ['b'], | ||
// ['c'], | ||
// ]), | ||
// rowCount: 3, | ||
// columnCount: 1, | ||
// unitId: '', | ||
// sheetId: '', | ||
// row: 0, | ||
// column: 0, | ||
// }); | ||
// const text2 = ArrayValueObject.create({ | ||
// calculateValueList: transformToValueObject([ | ||
// [1, 2, 3], | ||
// ]), | ||
// rowCount: 1, | ||
// columnCount: 3, | ||
// unitId: '', | ||
// sheetId: '', | ||
// row: 0, | ||
// column: 0, | ||
// }); | ||
// const result = textFunction.calculate(text1, text2); | ||
// expect(transformToValue(result.getArrayValue())).toStrictEqual([['a1', 'a2', 'a3'], ['b1', 'b2', 'b3'], ['c1', 'c2', 'c3']]); | ||
// }); | ||
|
||
// it('Text1 is 2*2 array, text2 is 3*3 array', () => { | ||
// const text1 = ArrayValueObject.create({ | ||
// calculateValueList: transformToValueObject([ | ||
// ['a', 'd'], | ||
// [0, null], | ||
// ]), | ||
// rowCount: 2, | ||
// columnCount: 2, | ||
// unitId: '', | ||
// sheetId: '', | ||
// row: 0, | ||
// column: 0, | ||
// }); | ||
// const text2 = ArrayValueObject.create({ | ||
// calculateValueList: transformToValueObject([ | ||
// [1, 2, 3], | ||
// [0, null, 4], | ||
// [3, 4, 5], | ||
// ]), | ||
// rowCount: 3, | ||
// columnCount: 3, | ||
// unitId: '', | ||
// sheetId: '', | ||
// row: 0, | ||
// column: 0, | ||
// }); | ||
// const result = textFunction.calculate(text1, text2); | ||
// expect(transformToValue(result.getArrayValue())).toStrictEqual([['a1', 'd2', '#N/A'], ['00', '', '#N/A'], ['#N/A', '#N/A', '#N/A']]); | ||
// }); | ||
}); | ||
}); |
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,83 @@ | ||
/** | ||
* 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 { expandArrayValueObject } from '../../../engine/utils/array-object'; | ||
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object'; | ||
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object'; | ||
import { NumberValueObject, StringValueObject } from '../../../engine/value-object/primitive-object'; | ||
import { BaseFunction } from '../../base-function'; | ||
|
||
export class Text extends BaseFunction { | ||
override calculate(text: BaseValueObject, formatText: BaseValueObject) { | ||
if (text == null || formatText == null) { | ||
return ErrorValueObject.create(ErrorType.NA); | ||
} | ||
|
||
if (text.isError()) { | ||
return text; | ||
} | ||
|
||
if (formatText.isError()) { | ||
return formatText; | ||
} | ||
|
||
// get max row length | ||
const maxRowLength = Math.max( | ||
text.isArray() ? (text as ArrayValueObject).getRowCount() : 1, | ||
formatText.isArray() ? (formatText as ArrayValueObject).getRowCount() : 1 | ||
); | ||
|
||
// get max column length | ||
const maxColumnLength = Math.max( | ||
text.isArray() ? (text as ArrayValueObject).getColumnCount() : 1, | ||
formatText.isArray() ? (formatText as ArrayValueObject).getColumnCount() : 1 | ||
); | ||
|
||
const textArray = expandArrayValueObject(maxRowLength, maxColumnLength, text); | ||
const formatTextArray = expandArrayValueObject(maxRowLength, maxColumnLength, formatText); | ||
|
||
return textArray.map((textValue, rowIndex, columnIndex) => { | ||
if (textValue.isError() || textValue.isString() || textValue.isBoolean()) { | ||
return textValue; | ||
} | ||
|
||
let formatTextValue = formatTextArray.get(rowIndex, columnIndex); | ||
|
||
if (formatTextValue.isError()) { | ||
return formatTextValue; | ||
} | ||
|
||
if (formatTextValue.isBoolean()) { | ||
return ErrorValueObject.create(ErrorType.VALUE); | ||
} | ||
|
||
if (formatTextValue.isNull()) { | ||
formatTextValue = StringValueObject.create(' '); | ||
} | ||
|
||
const formatTextValueString = `${formatTextValue.getValue()}`; | ||
|
||
if (textValue.isNull()) { | ||
textValue = NumberValueObject.create(0); | ||
} | ||
|
||
const textValueNumber = textValue.getValue() as number; | ||
|
||
return NumberValueObject.create(textValueNumber, formatTextValueString); | ||
}); | ||
} | ||
} |
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