Skip to content

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.

fix(formula): len counts displayed value (#2702)

Dushusirce887b058d8c
diff --git a/packages/engine-formula/src/functions/__tests__/nested-functions.spec.ts b/packages/engine-formula/src/functions/__tests__/nested-functions.spec.ts
index c779ec4b20..d9cb024d2c 100644
--- a/packages/engine-formula/src/functions/__tests__/nested-functions.spec.ts
+++ b/packages/engine-formula/src/functions/__tests__/nested-functions.spec.ts
@@ -50,6 +50,8 @@ import type { LexerNode } from '../../engine/analysis/lexer-node';
import { Choose } from '../lookup/choose';
import { Sum } from '../math/sum';
import { ErrorType } from '../../basics/error-type';
+import { Len } from '../text/len';
+import { Divided } from '../meta/divided';
import { createFunctionTestBed, getObjectValue } from './create-function-test-bed';
const getFunctionsTestWorkbookData = (): IWorkbookData => {
@@ -316,7 +318,9 @@ describe('Test nested functions', () => {
new Minus(FUNCTION_NAMES_META.MINUS),
new Concatenate(FUNCTION_NAMES_TEXT.CONCATENATE),
new Sum(FUNCTION_NAMES_MATH.SUM),
- new Choose(FUNCTION_NAMES_LOOKUP.CHOOSE)
+ new Choose(FUNCTION_NAMES_LOOKUP.CHOOSE),
+ new Len(FUNCTION_NAMES_TEXT.LEN),
+ new Divided(FUNCTION_NAMES_META.DIVIDED)
);
@@ -372,5 +376,13 @@ describe('Test nested functions', () => {
result = calculate('="test"');
expect(result).toStrictEqual('test');
});
+
+ it('Len gets length from formula result', () => {
+ let result = calculate('=LEN(1/3)');
+ expect(result).toStrictEqual(17);
+
+ result = calculate('=LEN(0.1+0.2)');
+ expect(result).toStrictEqual(3);
+ });
});
});
diff --git a/packages/engine-formula/src/functions/text/len/index.ts b/packages/engine-formula/src/functions/text/len/index.ts
index 57660d5d82..19c3e360b7 100644
--- a/packages/engine-formula/src/functions/text/len/index.ts
+++ b/packages/engine-formula/src/functions/text/len/index.ts
@@ -15,6 +15,7 @@
*/
import { ErrorType } from '../../../basics/error-type';
+import { stripErrorMargin } from '../../../engine/utils/math-kit';
import { type BaseValueObject, ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { NumberValueObject } from '../../../engine/value-object/primitive-object';
import { BaseFunction } from '../../base-function';
@@ -47,6 +48,12 @@ export class Len extends BaseFunction {
return NumberValueObject.create(0);
}
+ if (text.isNumber()) {
+ const numberValue = text.getValue() as number;
+ const numberValueString = stripErrorMargin(numberValue).toString();
+ return NumberValueObject.create(numberValueString.length);
+ }
+
if (text.isString() || text.isBoolean() || text.isNumber()) {
const textValue = text.getValue().toString();
return NumberValueObject.create(textValue.length);
diff --git a/packages/engine-formula/src/services/dependency-manager.service.ts b/packages/engine-formula/src/services/dependency-manager.service.ts
index 715c4d0c3d..8b0d6cd709 100644
--- a/packages/engine-formula/src/services/dependency-manager.service.ts
+++ b/packages/engine-formula/src/services/dependency-manager.service.ts
@@ -127,6 +127,7 @@ export class DependencyManagerService extends Disposable implements IDependencyM
buildDependencyTree(shouldBeBuildTrees: FormulaDependencyTree[] | FormulaDependencyTreeCache, dependencyTrees?: FormulaDependencyTree[]): FormulaDependencyTree[] {
const allTrees = this.getAllTree();
if (shouldBeBuildTrees.length === 0) {
+ this._buildReverseDependency(allTrees, dependencyTrees);
return allTrees;
}
if (shouldBeBuildTrees instanceof FormulaDependencyTreeCache) {
@@ -159,10 +160,17 @@ export class DependencyManagerService extends Disposable implements IDependencyM
});
}
});
+ this._buildReverseDependency(allTrees, dependencyTrees);
+ }
- // Build the reverse dependency relationship between the trees.
+ /**
+ * Build the reverse dependency relationship between the trees.
+ * @param allTrees
+ * @param dependencyTrees
+ */
+ private _buildReverseDependency(allTrees: FormulaDependencyTree[], dependencyTrees?: FormulaDependencyTree[]) {
allTrees.forEach((tree) => {
- dependencyTrees.forEach((dependencyTree) => {
+ dependencyTrees?.forEach((dependencyTree) => {
if (tree === dependencyTree || tree.children.includes(dependencyTree)) {
return true;
}
diff --git a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-transform-affected.controller.ts b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-transform-affected.controller.ts
index 942b5b7927..574fa0ab61 100644
--- a/packages/sheets-drawing-ui/src/controllers/sheet-drawing-transform-affected.controller.ts
+++ b/packages/sheets-drawing-ui/src/controllers/sheet-drawing-transform-affected.controller.ts
@@ -1078,7 +1078,7 @@ export class SheetDrawingTransformAffectedController extends Disposable {
}
requestIdleCallback(() => {
- const params = command.params as ISetRowVisibleMutationParams | ISetColHiddenMutationParams | ISetWorksheetRowHeightMutationParams | ISetWorksheetColWidthMutationParams | ISetWorksheetRowIsAutoHeightMutationParams | ISetRowHiddenMutationParams | ISetColVisibleMutationParams | ISetWorksheetColWidthMutationParams;
+ const params = command.params as ISetRowVisibleMutationParams | ISetColHiddenMutationParams | ISetWorksheetRowHeightMutationParams | ISetWorksheetColWidthMutationParams | ISetWorksheetRowIsAutoHeightMutationParams | ISetRowHiddenMutationParams | ISetColVisibleMutationParams;
const { unitId, subUnitId, ranges } = params;
this._refreshDrawingTransform(unitId, subUnitId, ranges);
});
diff --git a/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts b/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts
index 4f048e6fcc..02960148cc 100644
--- a/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts
+++ b/packages/sheets-formula/src/controllers/formula-editor-show.controller.ts
@@ -14,12 +14,13 @@
* limitations under the License.
*/
-import type { ICellDataForSheetInterceptor, ICommandInfo, IRange, Nullable, Workbook } from '@univerjs/core';
+import type { ICellDataForSheetInterceptor, ICommandInfo, IObjectMatrixPrimitiveType, IRange, IRowAutoHeightInfo, Nullable, Workbook, Worksheet } from '@univerjs/core';
import {
ColorKit,
Disposable,
ICommandService,
ObjectMatrix,
+ Rectangle,
ThemeService,
toDisposable,
} from '@univerjs/core';
@@ -31,6 +32,8 @@ import {
} from '@univerjs/engine-formula';
import type { IRenderContext, IRenderModule } from '@univerjs/engine-render';
import { IRenderManagerService } from '@univerjs/engine-render';
+import type { ISetColHiddenMutationParams, ISetColVisibleMutationParams, ISetRowHiddenMutationParams, ISetRowVisibleMutationParams, ISetWorksheetColWidthMutationParams, ISetWorksheetRowAutoHeightMutationParams, ISetWorksheetRowHeightMutationParams } from '@univerjs/sheets';
+import { SetColHiddenMutation, SetColVisibleMutation, SetRowHiddenMutation, SetRowVisibleMutation, SetWorksheetColWidthMutation, SetWorksheetRowAutoHeightMutation, SetWorksheetRowHeightMutation } from '@univerjs/sheets';
import {
IEditorBridgeService,
ISheetSelectionRenderService,
@@ -39,6 +42,15 @@ import {
} from '@univerjs/sheets-ui';
import { Inject } from '@wendellhu/redi';
+const REFRESH_ARRAY_SHAPE_MUTATIONS = [
+ SetWorksheetRowHeightMutation.id,
+ SetWorksheetColWidthMutation.id,
+ SetColHiddenMutation.id,
+ SetColVisibleMutation.id,
+ SetRowHiddenMutation.id,
+ SetRowVisibleMutation.id,
+];
+
export class FormulaEditorShowController extends Disposable implements IRenderModule {
private _previousShape: Nullable<SelectionShape>;
@@ -105,44 +117,7 @@ export class FormulaEditorShowController extends Disposable implements IRenderMo
*/
const matrixRange = arrayFormulaMatrixRange?.[unitId]?.[subUnitId];
if (matrixRange != null) {
- new ObjectMatrix(matrixRange).forValue((rowIndex, columnIndex, range) => {
- if (range == null) {
- return true;
- }
- const { startRow, startColumn, endRow, endColumn } = range;
- if (rowIndex === row && columnIndex === col) {
- this._createArrayFormulaRangeShape(range, unitId);
- return false;
- }
- if (row >= startRow && row <= endRow && col >= startColumn && col <= endColumn) {
- const mainCellValue = worksheet.getCell(startRow, startColumn);
-
- if (mainCellValue?.v === ErrorType.SPILL) {
- return;
- }
-
- const formulaDataItem = this._formulaDataModel.getFormulaDataItem(
- rowIndex,
- columnIndex,
- subUnitId,
- unitId
- );
-
- if (formulaDataItem == null || formulaDataItem.f == null) {
- return true;
- }
-
- if (cellInfo == null) {
- cellInfo = {
- f: formulaDataItem.f,
- isInArrayFormulaRange: true,
- };
- }
-
- this._createArrayFormulaRangeShape(range, unitId);
- return false;
- }
- });
+ this._displayArrayFormulaRangeShape(matrixRange, row, col, unitId, subUnitId, worksheet, cellInfo);
}
if (cellInfo) {
@@ -164,6 +139,67 @@ export class FormulaEditorShowController extends Disposable implements IRenderMo
this._removeArrayFormulaRangeShape();
}
}));
+
+ this.disposeWithMe(
+ this._commandService.beforeCommandExecuted((command: ICommandInfo) => {
+ if (SetWorksheetRowAutoHeightMutation.id === command.id) {
+ requestIdleCallback(() => {
+ const params = command.params as ISetWorksheetRowAutoHeightMutationParams;
+ const { unitId, subUnitId, rowsAutoHeightInfo } = params;
+ this._refreshArrayFormulaRangeShapeByRow(unitId, subUnitId, rowsAutoHeightInfo);
+ });
+ }
+
+ if (REFRESH_ARRAY_SHAPE_MUTATIONS.includes(command.id)) {
+ requestIdleCallback(() => {
+ const params = command.params as ISetRowVisibleMutationParams | ISetColHiddenMutationParams | ISetWorksheetRowHeightMutationParams | ISetWorksheetColWidthMutationParams | ISetRowHiddenMutationParams | ISetColVisibleMutationParams;
+ const { unitId, subUnitId, ranges } = params;
+ this._refreshArrayFormulaRangeShapeByRanges(unitId, subUnitId, ranges);
+ });
+ }
+ })
+ );
+ }
+
+ private _displayArrayFormulaRangeShape(matrixRange: IObjectMatrixPrimitiveType<IRange>, row: number, col: number, unitId: string, subUnitId: string, worksheet: Worksheet, cellInfo: Nullable<ICellDataForSheetInterceptor>) {
+ new ObjectMatrix(matrixRange).forValue((rowIndex, columnIndex, range) => {
+ if (range == null) {
+ return true;
+ }
+ const { startRow, startColumn, endRow, endColumn } = range;
+ if (rowIndex === row && columnIndex === col) {
+ this._createArrayFormulaRangeShape(range, unitId);
+ return false;
+ }
+ if (row >= startRow && row <= endRow && col >= startColumn && col <= endColumn) {
+ const mainCellValue = worksheet.getCell(startRow, startColumn);
+
+ if (mainCellValue?.v === ErrorType.SPILL) {
+ return;
+ }
+
+ const formulaDataItem = this._formulaDataModel.getFormulaDataItem(
+ rowIndex,
+ columnIndex,
+ subUnitId,
+ unitId
+ );
+
+ if (formulaDataItem == null || formulaDataItem.f == null) {
+ return true;
+ }
+
+ if (cellInfo == null) {
+ cellInfo = {
+ f: formulaDataItem.f,
+ isInArrayFormulaRange: true,
+ };
+ }
+
+ this._createArrayFormulaRangeShape(range, unitId);
+ return false;
+ }
+ });
}
private _createArrayFormulaRangeShape(arrayRange: IRange, unitId: string) {
@@ -191,7 +227,7 @@ export class FormulaEditorShowController extends Disposable implements IRenderMo
primary: null,
style,
});
- const skeleton = this._sheetSkeletonManagerService.getCurrent()?.skeleton;
+ const skeleton = this._sheetSkeletonManagerService.getCurrentSkeleton();
if (!scene || !skeleton) return;
const { rowHeaderWidth, columnHeaderHeight } = skeleton;
const control = new SelectionShape(scene, 100, false, this._themeService);
@@ -207,4 +243,79 @@ export class FormulaEditorShowController extends Disposable implements IRenderMo
this._previousShape.dispose();
this._previousShape = null;
}
+
+ private _refreshArrayFormulaRangeShape(unitId: string, range: IRange) {
+ if (this._previousShape) {
+ const { startRow, endRow, startColumn, endColumn } = this._previousShape.getRange();
+ const range = { startRow, endRow, startColumn, endColumn };
+ this._removeArrayFormulaRangeShape();
+ this._createArrayFormulaRangeShape(range, unitId);
+ }
+ }
+
+ private _checkCurrentSheet(unitId: string, subUnitId: string) {
+ const skeleton = this._sheetSkeletonManagerService.getCurrentSkeleton();
+ if (!skeleton) return false;
+
+ const worksheet = skeleton.worksheet;
+ if (!worksheet) return false;
+
+ if (worksheet.unitId === unitId && worksheet.getSheetId() === subUnitId) {
+ return true;
+ }
+
+ return false;
+ }
+
+ private _refreshArrayFormulaRangeShapeByRanges(unitId: string, subUnitId: string, ranges: IRange[]) {
+ if (!this._checkCurrentSheet(unitId, subUnitId)) return;
+
+ if (!this._previousShape) return;
+
+ const { startRow: shapeStartRow, endRow: shapeEndRow, startColumn: shapeStartColumn, endColumn: shapeEndColumn } = this._previousShape.getRange();
+
+ for (let i = 0; i < ranges.length; i++) {
+ const range = ranges[i];
+ const { startRow, endRow, startColumn, endColumn } = range;
+ if (Rectangle.intersects(
+ {
+ startRow, endRow, startColumn, endColumn,
+ },
+ {
+ startRow: shapeStartRow, endRow: shapeEndRow, startColumn: shapeStartColumn, endColumn: shapeEndColumn,
+ }
+ ) || shapeStartRow >= endRow || startColumn >= endColumn) {
+ const shapeRange = {
+ startRow: shapeStartRow,
+ endRow: shapeEndRow,
+ startColumn: shapeStartColumn,
+ endColumn: shapeEndColumn,
+ };
+ this._refreshArrayFormulaRangeShape(unitId, shapeRange);
+ break;
+ }
+ }
+ }
+
+ private _refreshArrayFormulaRangeShapeByRow(unitId: string, subUnitId: string, rowAutoHeightInfo: IRowAutoHeightInfo[]) {
+ if (!this._checkCurrentSheet(unitId, subUnitId)) return;
+
+ if (!this._previousShape) return;
+
+ const { startRow: shapeStartRow, endRow: shapeEndRow, startColumn: shapeStartColumn, endColumn: shapeEndColumn } = this._previousShape.getRange();
+
+ for (let i = 0; i < rowAutoHeightInfo.length; i++) {
+ const { row } = rowAutoHeightInfo[i];
+ if (shapeStartRow >= row) {
+ const shapeRange = {
+ startRow: shapeStartRow,
+ endRow: shapeEndRow,
+ startColumn: shapeStartColumn,
+ endColumn: shapeEndColumn,
+ };
+ this._refreshArrayFormulaRangeShape(unitId, shapeRange);
+ break;
+ }
+ }
+ }
}