Skip to content

Commit

Permalink
fix(formula): t formula should handle reference and array values (#4556)
Browse files Browse the repository at this point in the history
  • Loading branch information
wpxp123456 authored Jan 23, 2025
1 parent f8c7684 commit 5b7cc47
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import { Min } from '../statistical/min';
import { Concatenate } from '../text/concatenate';
import { FUNCTION_NAMES_TEXT } from '../text/function-names';
import { Len } from '../text/len';
import { T } from '../text/t';
import { Text } from '../text/text';
import { createFunctionTestBed, getObjectValue } from './create-function-test-bed';

Expand Down Expand Up @@ -395,6 +396,7 @@ describe('Test nested functions', () => {
new Divided(FUNCTION_NAMES_META.DIVIDED),
new Product(FUNCTION_NAMES_MATH.PRODUCT),
new Fact(FUNCTION_NAMES_MATH.FACT),
new T(FUNCTION_NAMES_TEXT.T),
new Text(FUNCTION_NAMES_TEXT.TEXT)
);

Expand Down Expand Up @@ -525,5 +527,10 @@ describe('Test nested functions', () => {
['001234'],
]);
});

it('T formula test', () => {
const result = calculate('=T(A1:E1)');
expect(result).toStrictEqual('A');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ describe('Test t function', () => {
column: 0,
});
const result = testFunction.calculate(value);
expect(getObjectValue(result)).toStrictEqual('');
expect(getObjectValue(result)).toStrictEqual([
['', ' ', '中文测试', '', '', ''],
['', '', '', '2-way street', '', ErrorType.NAME],
]);
});
});
});
27 changes: 20 additions & 7 deletions packages/engine-formula/src/functions/text/t/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

import type { BaseReferenceObject, FunctionVariantType } from '../../../engine/reference-object/base-reference-object';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { StringValueObject } from '../../../engine/value-object/primitive-object';
Expand All @@ -24,21 +25,33 @@ export class T extends BaseFunction {

override maxParams = 1;

override calculate(value: BaseValueObject): BaseValueObject {
override needsReferenceObject = true;

override calculate(value: FunctionVariantType): BaseValueObject {
let _value = value;

if (value.isArray()) {
_value = (value as ArrayValueObject).get(0, 0) as BaseValueObject;
// If the parameter is a reference object, get the first value from the reference object
if (value.isReferenceObject()) {
_value = (value as BaseReferenceObject).toArrayValueObject().get(0, 0) as BaseValueObject;
}

// If the parameter is an array, return an array result
if (_value.isArray()) {
return (_value as ArrayValueObject).mapValue((valueObject) => this._handleSingleObject(valueObject));
}

if (_value.isError()) {
return _value;
return this._handleSingleObject(_value as BaseValueObject);
}

private _handleSingleObject(value: BaseValueObject): BaseValueObject {
if (value.isError()) {
return value;
}

if (_value.isNull() || _value.isBoolean() || _value.isNumber()) {
if (value.isNull() || value.isBoolean() || value.isNumber()) {
return StringValueObject.create('');
}

return _value;
return value;
}
}

0 comments on commit 5b7cc47

Please sign in to comment.