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): support cubeValueObject (#1536)
Univer1c77658431c2
diff --git a/packages/engine-formula/src/basics/token-type.ts b/packages/engine-formula/src/basics/token-type.tsindex 2a1c326e20..f31ecd585b 100644--- a/packages/engine-formula/src/basics/token-type.ts+++ b/packages/engine-formula/src/basics/token-type.ts@@ -27,3 +27,5 @@ export const DEFAULT_TOKEN_TYPE_LAMBDA_OMIT_PARAMETER = 'LO_1';export const DEFAULT_TOKEN_LET_FUNCTION_NAME = 'LET';export const DEFAULT_TOKEN_LAMBDA_FUNCTION_NAME = 'LAMBDA';++export const DEFAULT_TOKEN_CUBE_FUNCTION_NAME = 'CUBE';diff --git a/packages/engine-formula/src/engine/analysis/__tests__/lexer-tree-builder.spec.ts b/packages/engine-formula/src/engine/analysis/__tests__/lexer-tree-builder.spec.tsindex 3f4ec6795a..665c063eb1 100644--- a/packages/engine-formula/src/engine/analysis/__tests__/lexer-tree-builder.spec.ts+++ b/packages/engine-formula/src/engine/analysis/__tests__/lexer-tree-builder.spec.ts@@ -179,6 +179,16 @@ describe('lexer nodeMaker test', () => {const node = lexerTreeBuilder.treeBuilder('=-(2)+*9') as LexerNode;expect(JSON.stringify(node.serialize())).toStrictEqual('{"token":"R_1","st":-1,"ed":-1,"children":["0","2","-","9","+"]}');});++ it('ref:ref parser', () => {+ const node = lexerTreeBuilder.treeBuilder('=SUM(A6:B6:C6:D7,1,1,2)') as LexerNode;+ expect(JSON.stringify(node.serialize())).toStrictEqual('{"token":"R_1","st":-1,"ed":-1,"children":[{"token":"SUM","st":0,"ed":2,"children":[{"token":"P_1","st":0,"ed":2,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"A6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"B6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"C6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"D7","st":-1,"ed":-1,"children":[]}]}]}]}]}]}]}]},{"token":"P_1","st":12,"ed":14,"children":["1"]},{"token":"P_1","st":14,"ed":16,"children":["1"]},{"token":"P_1","st":16,"ed":18,"children":["2"]}]}]}');+ });++ it('cube parser', () => {+ const node = lexerTreeBuilder.treeBuilder('=INDEX((A6:B6,C6:D7),1,1,2)') as LexerNode;+ expect(JSON.stringify(node.serialize())).toStrictEqual('{"token":"R_1","st":-1,"ed":-1,"children":[{"token":"INDEX","st":0,"ed":4,"children":[{"token":"P_1","st":2,"ed":4,"children":[{"token":"CUBE","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"A6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"B6","st":-1,"ed":-1,"children":[]}]}]}]},{"token":"P_1","st":9,"ed":11,"children":[{"token":":","st":-1,"ed":-1,"children":[{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"C6","st":-1,"ed":-1,"children":[]}]},{"token":"P_1","st":-1,"ed":-1,"children":[{"token":"D7","st":-1,"ed":-1,"children":[]}]}]}]}]}]},{"token":"P_1","st":16,"ed":18,"children":["1"]},{"token":"P_1","st":18,"ed":20,"children":["1"]},{"token":"P_1","st":20,"ed":22,"children":["2"]}]}]}');+ });});describe('checkIfAddBracket', () => {@@ -232,5 +242,54 @@ describe('lexer nodeMaker test', () => {},]);});++ it('cube', () => {+ expect(lexerTreeBuilder.sequenceNodesBuilder('=INDEX((A6:B6,C6:D7),1,1,2)')).toStrictEqual([+ {+ endIndex: 4,+ nodeType: 3,+ startIndex: 0,+ token: 'INDEX',+ },+ '(',+ '(',+ {+ endIndex: 11,+ nodeType: 4,+ startIndex: 7,+ token: 'A6:B6',+ },+ ',',+ {+ endIndex: 17,+ nodeType: 4,+ startIndex: 13,+ token: 'C6:D7',+ },+ ')',+ ',',+ {+ endIndex: 20,+ nodeType: 1,+ startIndex: 20,+ token: '1',+ },+ ',',+ {+ endIndex: 22,+ nodeType: 1,+ startIndex: 22,+ token: '1',+ },+ ',',+ {+ endIndex: 24,+ nodeType: 1,+ startIndex: 24,+ token: '2',+ },+ ')',+ ]);+ });});});diff --git a/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts b/packages/engine-formula/src/engine/analysis/lexer-tree-builder.tsindex ba3706fd6d..3e466ca1f6 100644--- a/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts+++ b/packages/engine-formula/src/engine/analysis/lexer-tree-builder.ts@@ -32,6 +32,7 @@ import {suffixToken,} from '../../basics/token';import {+ DEFAULT_TOKEN_CUBE_FUNCTION_NAME,DEFAULT_TOKEN_LAMBDA_FUNCTION_NAME,DEFAULT_TOKEN_TYPE_LAMBDA_PARAMETER,DEFAULT_TOKEN_TYPE_PARAMETER,@@ -701,6 +702,11 @@ export class LexerTreeBuilder extends Disposable {return bracketState[bracketState.length - 1];}+ private _changeCurrentBracket(type: bracketType) {+ const bracketState = this._bracketState;+ bracketState[bracketState.length - 1] = type;+ }+private _openBraces() {this._bracesState += 1;}@@ -1138,7 +1144,35 @@ export class LexerTreeBuilder extends Disposable {// this.setCurrentLexerNode(subLexerNode);this._newAndPushCurrentLexerNode(DEFAULT_TOKEN_TYPE_PARAMETER, cur);} else {- return ErrorType.VALUE;+ /**+ * support cubeValueObject for =INDEX((A6:B6,C6:D7),1,1,2)+ */+ const cubeNode = new LexerNode();+ cubeNode.setToken(DEFAULT_TOKEN_CUBE_FUNCTION_NAME);+ const cubeParamNode = new LexerNode();+ cubeParamNode.setToken(DEFAULT_TOKEN_TYPE_PARAMETER);+ cubeParamNode.changeToParent(cubeNode);++ const colonNode = this._currentLexerNode.getParent();++ if (colonNode && colonNode.getToken() === matchToken.COLON) {+ const colonNodeParent = colonNode.getParent();+ if (!colonNodeParent) {+ return ErrorType.VALUE;+ }++ colonNode.changeToParent(cubeParamNode);+ colonNodeParent.setChildren([]);+ cubeNode.changeToParent(colonNodeParent);+ } else {+ return ErrorType.VALUE;+ }++ this._changeCurrentBracket(bracketType.FUNCTION);+ this._pushNodeToChildren(this._segment);+ this._resetSegment();+ this._currentLexerNode = cubeNode;+ this._newAndPushCurrentLexerNode(DEFAULT_TOKEN_TYPE_PARAMETER, cur);}} else if (currentString === matchToken.COLON &&diff --git a/packages/engine-formula/src/engine/value-object/base-value-object.ts b/packages/engine-formula/src/engine/value-object/base-value-object.tsindex b99f173f1c..2c6a522fd6 100644--- a/packages/engine-formula/src/engine/value-object/base-value-object.ts+++ b/packages/engine-formula/src/engine/value-object/base-value-object.ts@@ -71,6 +71,10 @@ export class BaseValueObject extends ObjectClassType {/** abstract */}+ isCube() {+ return false;+ }+isArray() {return false;}diff --git a/packages/engine-formula/src/engine/value-object/cube-value-object.ts b/packages/engine-formula/src/engine/value-object/cube-value-object.tsnew file mode 100644index 0000000000..875fd96b60--- /dev/null+++ b/packages/engine-formula/src/engine/value-object/cube-value-object.ts@@ -0,0 +1,86 @@+/**+ * 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 type { ArrayValueObject } from './array-value-object';+import { BaseValueObject } from './base-value-object';+import { NumberValueObject } from './primitive-object';++export class CubeValueObject extends BaseValueObject {+ override isCube() {+ return true;+ }++ private _values: ArrayValueObject[] = [];++ constructor(values: ArrayValueObject[]) {+ super('');+ this._values = values;+ }++ override sum() {+ const result = new NumberValueObject(0);+ this._values.forEach((arr) => {+ result.plus(arr.sum());+ });+ return result;+ }++ override max() {+ let result = new NumberValueObject(Number.NEGATIVE_INFINITY);+ this._values.forEach((arr) => {+ const compare = arr.max() as NumberValueObject;+ if (result.isLessThan(compare)) {+ result = compare;+ }+ });+ return result;+ }++ override min() {+ let result = new NumberValueObject(Number.POSITIVE_INFINITY);+ this._values.forEach((arr) => {+ const compare = arr.max() as NumberValueObject;+ if (result.isGreaterThan(compare)) {+ result = compare;+ }+ });+ return result;+ }++ override count() {+ const count = new NumberValueObject(0);+ this._values.forEach((arr) => {+ count.plus(arr.count());+ });+ return count;+ }++ override countA() {+ const count = new NumberValueObject(0);+ this._values.forEach((arr) => {+ count.plus(arr.countA());+ });+ return count;+ }++ override countBlank() {+ const count = new NumberValueObject(0);+ this._values.forEach((arr) => {+ count.plus(arr.countBlank());+ });+ return count;+ }+}diff --git a/packages/engine-formula/src/functions/meta/cube/index.ts b/packages/engine-formula/src/functions/meta/cube/index.tsnew file mode 100644index 0000000000..0642af977a--- /dev/null+++ b/packages/engine-formula/src/functions/meta/cube/index.ts@@ -0,0 +1,48 @@+/**+ * 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 } from '../../../engine/value-object/base-value-object';+import { ErrorValueObject } from '../../../engine/value-object/base-value-object';+import { BaseFunction } from '../../base-function';+import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';+import { CubeValueObject } from '../../../engine/value-object/cube-value-object';++export class Cube extends BaseFunction {+ override calculate(...variants: BaseValueObject[]) {+ if (variants.length === 0) {+ return new ErrorValueObject(ErrorType.VALUE);+ }++ const values: ArrayValueObject[] = [];++ for (let i = 0; i < variants.length; i++) {+ const variant = variants[i];++ if (variant.isError()) {+ return variant;+ }++ if (!variant.isArray()) {+ return new ErrorValueObject(ErrorType.VALUE);+ }++ values.push(variant as ArrayValueObject);+ }++ return new CubeValueObject(values);+ }+}diff --git a/packages/engine-formula/src/functions/meta/function-map.ts b/packages/engine-formula/src/functions/meta/function-map.tsindex a86bfee1c4..6608cf155b 100644--- a/packages/engine-formula/src/functions/meta/function-map.ts+++ b/packages/engine-formula/src/functions/meta/function-map.ts@@ -15,6 +15,7 @@*/import { Compare } from './compare';+import { Cube } from './cube';import { Divided } from './divided';import { FUNCTION_NAMES_META } from './function-names';import { Minus } from './minus';@@ -27,4 +28,5 @@ export const functionMeta = [[Minus, FUNCTION_NAMES_META.MINUS],[Multiply, FUNCTION_NAMES_META.MULTIPLY],[Plus, FUNCTION_NAMES_META.PLUS],+ [Cube, FUNCTION_NAMES_META.CUBE],];diff --git a/packages/engine-formula/src/functions/meta/function-names.ts b/packages/engine-formula/src/functions/meta/function-names.tsindex b77be2d6ce..1fb4700910 100644--- a/packages/engine-formula/src/functions/meta/function-names.ts+++ b/packages/engine-formula/src/functions/meta/function-names.ts@@ -21,4 +21,5 @@ export enum FUNCTION_NAMES_META {MULTIPLY = 'MULTIPLY',PLUS = 'PLUS',UNION = 'UNION',+ CUBE = 'CUBE',}