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: add ref property of cell data type (#5393)
Evan Huda360afa660a
diff --git a/packages/core/src/sheets/typedef.ts b/packages/core/src/sheets/typedef.tsindex 8035cbd4da..306291abaa 100644--- a/packages/core/src/sheets/typedef.ts+++ b/packages/core/src/sheets/typedef.ts@@ -259,6 +259,12 @@ export interface ICellData {*/f?: Nullable<string>;+ /**+ * If the formula is a formula array, this field is used to store the referencing range.+ * @ignore+ */+ ref?: Nullable<string>;+/*** Id of the formula.*/diff --git a/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts b/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.tsindex 4d6e5f4e93..4602ea1bc7 100644--- a/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts+++ b/packages/sheets-formula/src/controllers/array-formula-cell-interceptor.controller.ts@@ -14,12 +14,12 @@* limitations under the License.*/-import type { ICommandInfo } from '@univerjs/core';-import type { IArrayFormulaUnitCellType, ISetArrayFormulaDataMutationParams } from '@univerjs/engine-formula';+import type { ICellData, ICommandInfo } from '@univerjs/core';+import type { IArrayFormulaRangeType, IArrayFormulaUnitCellType, ISetArrayFormulaDataMutationParams } from '@univerjs/engine-formula';import type { ISetRangeValuesMutationParams } from '@univerjs/sheets';import type { IUniverSheetsFormulaBaseConfig } from './config.schema';-import { CellValueType, Disposable, ICommandService, IConfigService, Inject, InterceptorEffectEnum, isRealNum } from '@univerjs/core';-import { FormulaDataModel, SetArrayFormulaDataMutation, stripErrorMargin } from '@univerjs/engine-formula';+import { CellValueType, Disposable, ICommandService, IConfigService, Inject, InterceptorEffectEnum, isRealNum, ObjectMatrix } from '@univerjs/core';+import { FormulaDataModel, serializeRange, SetArrayFormulaDataMutation, stripErrorMargin } from '@univerjs/engine-formula';import { INTERCEPTOR_POINT, SetRangeValuesMutation, SheetInterceptorService } from '@univerjs/sheets';import { PLUGIN_CONFIG_KEY_BASE } from './config.schema';@@ -59,14 +59,35 @@ export class ArrayFormulaCellInterceptorController extends Disposable {this._formulaDataModel.setArrayFormulaRange(arrayFormulaRange);this._formulaDataModel.setArrayFormulaCellData(arrayFormulaCellData);+ // Note the logic should only be executed in exporting and hence shall be set once.if (this._configService.getConfig<IUniverSheetsFormulaBaseConfig>(PLUGIN_CONFIG_KEY_BASE)?.writeArrayFormulaToSnapshot) {- this._writeArrayFormulaToSnapshot(arrayFormulaCellData);+ this._writeArrayFormulaToSnapshot(arrayFormulaRange, arrayFormulaCellData);}}));}- private _writeArrayFormulaToSnapshot(arrayFormulaCellData: IArrayFormulaUnitCellType) {+ private _writeArrayFormulaToSnapshot(arrayFormulaRange: IArrayFormulaRangeType, arrayFormulaCellData: IArrayFormulaUnitCellType) {+ // Write values to the `ref` property of the array formula range.+ Object.entries(arrayFormulaRange).forEach(([unitId, subUnitData]) => {+ subUnitData && Array.from(Object.entries(subUnitData)).forEach(([subUnitId, rangeData]) => {+ // Convert from IObjectMatrixPrimitiveType<IRange> to IObjectMatrixPrimitiveType<ICellData>+ const cellValue = new ObjectMatrix<ICellData>();+ const matrix = new ObjectMatrix(rangeData);+ matrix.forValue((row, col, value) => {+ cellValue.setValue(row, col, { ref: serializeRange(value) }); // convert to ref string+ });++ // Keep this local to avoid triggering re-calculate in the worker.+ this._commandService.executeCommand<ISetRangeValuesMutationParams>(SetRangeValuesMutation.id, {+ unitId,+ subUnitId,+ cellValue: cellValue.getMatrix(),+ }, { onlyLocal: true });+ });+ });++ // Write values to the 'v' property of the cell data.Array.from(Object.entries(arrayFormulaCellData)).forEach(([unitId, subUnitData]) => {subUnitData && Array.from(Object.entries(subUnitData)).forEach(([subUnitId, rowData]) => {// Keep this local to avoid triggering re-calculate in the worker.diff --git a/packages/sheets-formula/src/controllers/config.schema.ts b/packages/sheets-formula/src/controllers/config.schema.tsindex c6b77a5cab..17574d4213 100644--- a/packages/sheets-formula/src/controllers/config.schema.ts+++ b/packages/sheets-formula/src/controllers/config.schema.ts@@ -52,7 +52,7 @@ export interface IUniverSheetsFormulaBaseConfig {initialFormulaComputing?: CalculationMode;/**- * If true, the plugin will write array formula results to the snapshot. Normally you should not set this to true.+ * If true, the plugin will write array formula results to the snapshot. Normally you should not set this value.*/writeArrayFormulaToSnapshot?: boolean;}@@ -66,8 +66,7 @@ export const PLUGIN_CONFIG_KEY_REMOTE = 'sheets-formula.remote.config';export const configSymbolRemote = Symbol(PLUGIN_CONFIG_KEY_REMOTE);-export interface IUniverSheetsFormulaRemoteConfig {-}+export interface IUniverSheetsFormulaRemoteConfig {}export const defaultPluginRemoteConfig: IUniverSheetsFormulaRemoteConfig = {};diff --git a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts b/packages/sheets/src/commands/mutations/set-range-values.mutation.tsindex cabebd9d79..25a4f9b67c 100644--- a/packages/sheets/src/commands/mutations/set-range-values.mutation.ts+++ b/packages/sheets/src/commands/mutations/set-range-values.mutation.ts@@ -141,8 +141,8 @@ export const SetRangeValuesMutation: IMutation<ISetRangeValuesMutationParams, bo},};+const overwriteCellPropertiesSet = new Set(['f', 'p', 'si', 'custom', 'ref']);function mergeCellData(newValue: ICellData, oldValue: ICellData, styles: Styles) {- const overwriteCellPropertiesSet = new Set(['f', 'p', 'si', 'custom']);const type = getCellType(styles, newValue, oldValue);Object.keys(newValue).forEach((key) => {const cellPropertyKey = key as keyof ICellData;@@ -162,6 +162,7 @@ function mergeCellData(newValue: ICellData, oldValue: ICellData, styles: Styles)oldValue.t = type;oldValue.v = getCellValue(type, oldValue);}+if (oldValue.v === null) {delete oldValue.t;delete oldValue.v;