Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.
TypeScript boardcollaborationdocdocxexceloffce365officepdfpptpptxpresentationsdksheetslidesspreadsheetuniverwordwordprocessorworkspacexlsx
feat(formula): implement formula lower (#1970)
Hantondb3fa45f0b60
diff --git a/packages/engine-formula/src/functions/text/function-map.ts b/packages/engine-formula/src/functions/text/function-map.tsindex b11c41c206..63c8d4b09a 100644--- a/packages/engine-formula/src/functions/text/function-map.ts+++ b/packages/engine-formula/src/functions/text/function-map.ts@@ -19,10 +19,12 @@ import { FUNCTION_NAMES_TEXT } from './function-names';import { Len } from './len';import { Lenb } from './lenb';import { Text } from './text';+import { Lower } from './lower';export const functionText = [[Concatenate, FUNCTION_NAMES_TEXT.CONCATENATE],[Len, FUNCTION_NAMES_TEXT.LEN],[Lenb, FUNCTION_NAMES_TEXT.LENB],[Text, FUNCTION_NAMES_TEXT.TEXT],+ [Lower, FUNCTION_NAMES_TEXT.LOWER],];diff --git a/packages/engine-formula/src/functions/text/lower/__test__/index.spec.ts b/packages/engine-formula/src/functions/text/lower/__test__/index.spec.tsnew file mode 100644index 0000000000..c51b4e32f3--- /dev/null+++ b/packages/engine-formula/src/functions/text/lower/__test__/index.spec.ts@@ -0,0 +1,67 @@+/**+ * 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 { Lower } from '../index';+import { BooleanValueObject, NumberValueObject, StringValueObject } from '../../../../engine/value-object/primitive-object';+import { ArrayValueObject, transformToValue, transformToValueObject } from '../../../../engine/value-object/array-value-object';+import { ErrorType } from '../../../../basics/error-type';++describe('Test lower function', () => {+ const textFunction = new Lower(FUNCTION_NAMES_TEXT.LOWER);++ describe('Lower', () => {+ it('Value is normal', () => {+ const value = StringValueObject.create('Apt. 2B');+ const result = textFunction.calculate(value);+ expect(result.getValue()).toBe('apt. 2b');+ });++ it('Value is number', () => {+ const value = NumberValueObject.create(1);+ const result = textFunction.calculate(value);+ expect(result.getValue()).toBe('1');+ });++ it('Value is boolean', () => {+ const value = BooleanValueObject.create(true);+ const result = textFunction.calculate(value);+ expect(result.getValue()).toBe('true');+ });++ it('Value is array', () => {+ const text = new ArrayValueObject({+ calculateValueList: transformToValueObject([+ [1, ' ', 1.23, true, false, null, 'Univer表格シート繁體한국인'],+ [0, '100', '2.34', 'TEST', -3, ErrorType.VALUE, null],+ ]),+ rowCount: 2,+ columnCount: 7,+ unitId: '',+ sheetId: '',+ row: 0,+ column: 0,+ });+ const result = textFunction.calculate(text);+ expect(transformToValue(result.getArrayValue())).toStrictEqual([+ ['1', ' ', '1.23', 'true', 'false', '', 'univer表格シート繁體한국인'],+ ['0', '100', '2.34', 'test', '-3', '#VALUE!', ''],+ ]);+ });+ });+});diff --git a/packages/engine-formula/src/functions/text/lower/index.ts b/packages/engine-formula/src/functions/text/lower/index.tsnew file mode 100644index 0000000000..f9b8b63db1--- /dev/null+++ b/packages/engine-formula/src/functions/text/lower/index.ts@@ -0,0 +1,57 @@+/**+ * 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 BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';+import { StringValueObject } from '../../../engine/value-object/primitive-object';+import { BaseFunction } from '../../base-function';++export class Lower extends BaseFunction {+ override calculate(text: BaseValueObject) {+ if (text == null) {+ return ErrorValueObject.create(ErrorType.NA);+ }++ if (text.isError()) {+ return text;+ }++ if (text.isArray()) {+ return text.mapValue((textValue: BaseValueObject) => {+ return this._handleSingleText(textValue);+ });+ }++ return this._handleSingleText(text);+ }++ private _handleSingleText(text: BaseValueObject) {+ if (text.isError()) {+ return text;+ }++ if (text.isNull()) {+ return StringValueObject.create('');+ }++ if (text.isString() || text.isBoolean() || text.isNumber()) {+ const textValue = text.getValue().toString().toLowerCase();+ return StringValueObject.create(textValue);+ }++ return ErrorValueObject.create(ErrorType.VALUE);+ }+}diff --git a/packages/sheets-formula/src/locale/function-list/text/en-US.ts b/packages/sheets-formula/src/locale/function-list/text/en-US.tsindex 10d1513da7..9ca7184d0c 100644--- a/packages/sheets-formula/src/locale/function-list/text/en-US.ts+++ b/packages/sheets-formula/src/locale/function-list/text/en-US.ts@@ -266,7 +266,7 @@ export default {},},LOWER: {- description: 'Converts text to lowercase',+ description: 'Converts text to lowercase.',abstract: 'Converts text to lowercase',links: [{@@ -275,8 +275,10 @@ export default {},],functionParameter: {- number1: { name: 'number1', detail: 'first' },- number2: { name: 'number2', detail: 'second' },+ text: {+ name: 'text',+ detail: 'The text you want to convert to lowercase. LOWER does not change characters in text that are not letters.',+ },},},MID: {diff --git a/packages/sheets-formula/src/locale/function-list/text/ja-JP.ts b/packages/sheets-formula/src/locale/function-list/text/ja-JP.tsindex 9e51d68679..161b9637d0 100644--- a/packages/sheets-formula/src/locale/function-list/text/ja-JP.ts+++ b/packages/sheets-formula/src/locale/function-list/text/ja-JP.ts@@ -268,7 +268,7 @@ export default {},LOWER: {description: '文字列に含まれる英字をすべて小文字に変換します。',- abstract: '文字列に含まれる英字をすべて小文字に変換します。',+ abstract: '文字列に含まれる英字をすべて小文字に変換します',links: [{title: '指導',@@ -276,8 +276,10 @@ export default {},],functionParameter: {- number1: { name: 'number1', detail: 'first' },- number2: { name: 'number2', detail: 'second' },+ text: {+ name: '文字列',+ detail: '小文字に変換する文字列を指定します。 それ以外の文字は変換されません。',+ },},},MID: {diff --git a/packages/sheets-formula/src/locale/function-list/text/zh-CN.ts b/packages/sheets-formula/src/locale/function-list/text/zh-CN.tsindex 6f427e3f22..37131e0f68 100644--- a/packages/sheets-formula/src/locale/function-list/text/zh-CN.ts+++ b/packages/sheets-formula/src/locale/function-list/text/zh-CN.ts@@ -266,7 +266,7 @@ export default {},},LOWER: {- description: '将文本转换为小写',+ description: '将文本转换为小写。',abstract: '将文本转换为小写',links: [{@@ -275,8 +275,10 @@ export default {},],functionParameter: {- number1: { name: 'number1', detail: 'first' },- number2: { name: 'number2', detail: 'second' },+ text: {+ name: '文本',+ detail: '要转换为小写字母的文本。 LOWER 不改变文本中的非字母字符。',+ },},},MID: {diff --git a/packages/sheets-formula/src/services/function-list/text.ts b/packages/sheets-formula/src/services/function-list/text.tsindex 09ec334ac7..7cd5d634e6 100644--- a/packages/sheets-formula/src/services/function-list/text.ts+++ b/packages/sheets-formula/src/services/function-list/text.ts@@ -406,16 +406,9 @@ export const FUNCTION_LIST_TEXT: IFunctionInfo[] = [abstract: 'formula.functionList.LOWER.abstract',functionParameter: [{- name: 'formula.functionList.LOWER.functionParameter.number1.name',- detail: 'formula.functionList.LOWER.functionParameter.number1.detail',- example: 'A1:A20',- require: 1,- repeat: 0,- },- {- name: 'formula.functionList.LOWER.functionParameter.number2.name',- detail: 'formula.functionList.LOWER.functionParameter.number2.detail',- example: 'A1:A20',+ name: 'formula.functionList.LOWER.functionParameter.text.name',+ detail: 'formula.functionList.LOWER.functionParameter.text.detail',+ example: '"Univer"',require: 1,repeat: 0,},