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.

21.1 KB
Raw Blame
/**
 * Copyright 2023-present DreamNum Co., Ltd.
 *
 * 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 { BaseDataModel, ITableSnapshot, IUnitRange, IWorkbookCalculationSettings, LocaleType, Nullable, Workbook } from '@univerjs/core';
import {
    createIdentifier,
    DateSystem,
    Disposable,
    Inject,
    IUniverInstanceService,
    LocaleService,
    ObjectMatrix,
    UniverInstanceType,
} from '@univerjs/core';
import type {
    IArrayFormulaRangeType,
    IDirtyUnitDefinedNameMap,
    IDirtyUnitFeatureMap,
    IDirtyUnitOtherFormulaMap,
    IDirtyUnitSheetNameMap,
    IDirtyUnitSuperTableMap,
    IFormulaData,
    IFormulaDatasetConfig,
    IFormulaExternalReferences,
    IFormulaUnitNameMap,
    IRuntimeUnitDataType,
    IUnitData,
    IUnitExcludedCell,
    IUnitRowData,
    IUnitSheetIdToNameMap,
    IUnitSheetNameMap,
    IUnitStylesData,
} from '../basics/common';
import type { IFormulaDataSnapshot } from '../models/formula-data-snapshot';
import { convertUnitDataToRuntime, copyUnitData } from '../basics/runtime';
import { FormulaDataModel } from '../models/formula-data.model';
import { ISheetRowFilteredService } from './sheet-row-filtered.service';

const DEFERRED_FORMULA_DATA_SNAPSHOTS = new WeakSet<object>();

/**
 * Internal implementation bookkeeping, not an application integration API.
 * Query deferred-input bookkeeping without observing service Proxy properties or getters.
 */
export function hasDeferredFormulaDataSnapshot(service: unknown): boolean {
    return DEFERRED_FORMULA_DATA_SNAPSHOTS.has(service as object);
}

export interface IFormulaDirtyData {
    dirtyReferenceUnits?: string[];
    dirtyIterationUnits?: string[];
    forceCalculation: boolean;
    dirtyRanges: IUnitRange[];
    dirtyNameMap: IDirtyUnitSheetNameMap;
    dirtyDefinedNameMap: IDirtyUnitDefinedNameMap;
    dirtySuperTableMap?: IDirtyUnitSuperTableMap;
    dirtyUnitFeatureMap: IDirtyUnitFeatureMap;
    dirtyUnitOtherFormulaMap: IDirtyUnitOtherFormulaMap;
    clearDependencyTreeCache: IDirtyUnitSheetNameMap; // unitId -> sheetId
    calculationSettings?: Record<string, IWorkbookCalculationSettings>;
    isCalculateTreeModel?: boolean; // whether to calculate the dependency tree model
    rowData?: IUnitRowData; // Include rows hidden by filters
    externalReferences?: IFormulaExternalReferences;
}

export interface IFormulaCurrentConfigService {
    load(config: IFormulaDatasetConfig): void;
    /** Internal engine capability to defer formula DTO materialization. */
    loadWithFormulaDataSnapshot?(config: Omit<IFormulaDatasetConfig, 'formulaData'>, snapshot: IFormulaDataSnapshot): void;

    getUnitData(): IUnitData;

    /**
     * Get the unit styles data.
     */
    getUnitStylesData(): IUnitStylesData;

    getFormulaData(): IFormulaData;

    getSheetNameMap(): IUnitSheetNameMap;

    getUnitNameMap(): IFormulaUnitNameMap;

    getExternalReferences(): IFormulaExternalReferences;

    getCalculationGeneration(): number;

    isForceCalculate(): boolean;

    getDirtyRanges(): IUnitRange[];

    getDirtyNameMap(): IDirtyUnitSheetNameMap;

    getDirtyDefinedNameMap(): IDirtyUnitDefinedNameMap;

    getDirtySuperTableMap(): IDirtyUnitSuperTableMap;

    getDirtyUnitFeatureMap(): IDirtyUnitFeatureMap;

    registerUnitData(unitData: IUnitData): void;

    registerFormulaData(formulaData: IFormulaData): void;

    registerSheetNameMap(sheetNameMap: IUnitSheetNameMap): void;

    registerUnitNameMap(unitNameMap: IFormulaUnitNameMap): void;

    getExcludedRange(): Nullable<IUnitExcludedCell>;

    loadDirtyRangesAndExcludedCell(dirtyRanges: IUnitRange[], excludedCell?: IUnitExcludedCell): void;

    getArrayFormulaCellData(): IRuntimeUnitDataType;

    getArrayFormulaRange(): IArrayFormulaRangeType;

    getSheetName(unitId: string, sheetId: string): string;

    getDirtyUnitOtherFormulaMap(): IDirtyUnitOtherFormulaMap;

    getExecuteUnitId(): Nullable<string>;
    getExecuteSubUnitId(): Nullable<string>;

    setExecuteUnitId(unitId: string): void;
    setExecuteSubUnitId(subUnitId: string): void;

    getDirtyData(forFeatureCalculation?: boolean): IFormulaDirtyData;

    getClearDependencyTreeCache(): IDirtyUnitSheetNameMap;

    getLocale(): LocaleType;
    getDateSystem(unitId?: string): DateSystem;

    getSheetsInfo(unitId?: string): {
        sheetOrder: string[];
        sheetNameMap: { [sheetId: string]: string };
    };

    getSheetRowColumnCount(unitId: string, sheetId: string): { rowCount: number; columnCount: number };

    getFilteredOutRows(unitId: string, sheetId: string, startRow: number, endRow: number): number[];

    setSheetNameMap(sheetIdToNameMap: IUnitSheetIdToNameMap): void;

    loadDataLite(rowData?: IUnitRowData): void;
}

export class FormulaCurrentConfigService extends Disposable implements IFormulaCurrentConfigService {
    private _dirtyReferenceUnits: string[] = [];
    private _dirtyIterationUnits: string[] = [];
    private _unitData: IUnitData = Object.create(null);

    private _unitStylesData: IUnitStylesData = Object.create(null);

    private _arrayFormulaCellData: IRuntimeUnitDataType = Object.create(null);

    private _arrayFormulaRange: IArrayFormulaRangeType = Object.create(null);

    private _formulaData: IFormulaData = Object.create(null);
    private _formulaDataSnapshot: IFormulaDataSnapshot | undefined;

    private _sheetNameMap: IUnitSheetNameMap = Object.create(null);

    private _unitNameMap: IFormulaUnitNameMap = Object.create(null);

    private _externalReferences: IFormulaExternalReferences = Object.create(null);

    private _calculationRowData: IUnitRowData = Object.create(null);

    private _calculationGeneration = 0;

    private _forceCalculate: boolean = false;

    private _clearDependencyTreeCache: IDirtyUnitSheetNameMap = Object.create(null);

    private _dirtyRanges: IUnitRange[] = [];

    private _dirtyNameMap: IDirtyUnitSheetNameMap = Object.create(null);

    private _dirtyDefinedNameMap: IDirtyUnitDefinedNameMap = Object.create(null);

    private _dirtySuperTableMap: IDirtyUnitSuperTableMap = Object.create(null);

    private _dirtyUnitFeatureMap: IDirtyUnitFeatureMap = Object.create(null);

    private _dirtyUnitOtherFormulaMap: IDirtyUnitOtherFormulaMap = Object.create(null);

    private _excludedCell: Nullable<IUnitExcludedCell>;

    private _sheetIdToNameMap: IUnitSheetIdToNameMap = Object.create(null);

    private _executeUnitId: Nullable<string> = '';
    private _executeSubUnitId: Nullable<string> = '';

    constructor(
        @IUniverInstanceService private readonly _univerInstanceService: IUniverInstanceService,
        @Inject(LocaleService) private readonly _localeService: LocaleService,
        @Inject(FormulaDataModel) private readonly _formulaDataModel: FormulaDataModel,
        @Inject(ISheetRowFilteredService) private readonly _sheetRowFilteredService: ISheetRowFilteredService
    ) {
        super();
    }

    override dispose(): void {
        super.dispose();
        this._unitData = Object.create(null);
        this._unitStylesData = Object.create(null);
        this._arrayFormulaCellData = Object.create(null);
        this._arrayFormulaRange = Object.create(null);
        this._formulaData = Object.create(null);
        this._formulaDataSnapshot = undefined;
        DEFERRED_FORMULA_DATA_SNAPSHOTS.delete(this);
        this._sheetNameMap = Object.create(null);
        this._unitNameMap = Object.create(null);
        this._externalReferences = Object.create(null);
        this._calculationRowData = Object.create(null);
        this._clearDependencyTreeCache = Object.create(null);
        this._dirtyRanges = [];
        this._dirtyNameMap = Object.create(null);
        this._dirtyDefinedNameMap = Object.create(null);
        this._dirtySuperTableMap = Object.create(null);
        this._dirtyUnitFeatureMap = Object.create(null);
        this._dirtyUnitOtherFormulaMap = Object.create(null);
        this._excludedCell = Object.create(null);
        this._sheetIdToNameMap = Object.create(null);
    }

    getExecuteUnitId() {
        return this._executeUnitId;
    }

    getExecuteSubUnitId() {
        return this._executeSubUnitId;
    }

    setExecuteUnitId(unitId: string) {
        this._executeUnitId = unitId;
    }

    setExecuteSubUnitId(subUnitId: string) {
        this._executeSubUnitId = subUnitId;
    }

    getExcludedRange() {
        return this._excludedCell;
    }

    getUnitData() {
        return this._unitData;
    }

    getUnitStylesData(): IUnitStylesData {
        return this._unitStylesData;
    }

    getFormulaData(): IFormulaData {
        if (this._formulaDataSnapshot) {
            const data = this._formulaDataSnapshot.materialize();
            this._formulaData = data;
            this._formulaDataSnapshot = undefined;
            DEFERRED_FORMULA_DATA_SNAPSHOTS.delete(this);
        }
        return this._formulaData;
    }

    getArrayFormulaCellData() {
        return this._arrayFormulaCellData;
    }

    getArrayFormulaRange() {
        return this._arrayFormulaRange;
    }

    getSheetNameMap() {
        return this._sheetNameMap;
    }

    getUnitNameMap() {
        return this._unitNameMap;
    }

    getExternalReferences() {
        return this._externalReferences;
    }

    getCalculationGeneration() {
        return this._calculationGeneration;
    }

    isForceCalculate() {
        return this._forceCalculate;
    }

    getDirtyRanges() {
        return this._dirtyRanges;
    }

    getDirtyNameMap() {
        return this._dirtyNameMap;
    }

    getDirtyDefinedNameMap() {
        return this._dirtyDefinedNameMap;
    }

    getDirtySuperTableMap() {
        return this._dirtySuperTableMap;
    }

    getDirtyUnitFeatureMap() {
        return this._dirtyUnitFeatureMap;
    }

    getDirtyUnitOtherFormulaMap() {
        return this._dirtyUnitOtherFormulaMap;
    }

    getSheetName(unitId: string, sheetId: string) {
        if (this._sheetIdToNameMap[unitId] == null) {
            return '';
        }

        return this._sheetIdToNameMap[unitId]![sheetId] || '';
    }

    setSheetNameMap(sheetIdToNameMap: IUnitSheetIdToNameMap) {
        this._sheetIdToNameMap = copyUnitData(sheetIdToNameMap);
    }

    getClearDependencyTreeCache() {
        return this._clearDependencyTreeCache;
    }

    getLocale() {
        return this._localeService.getCurrentLocale();
    }

    getDateSystem(unitId = this._executeUnitId ?? undefined): DateSystem {
        if (!unitId) return DateSystem.Date1900;
        return this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)?.getDateSystem() ?? DateSystem.Date1900;
    }

    getSheetsInfo(unitId?: string) {
        const workbook = unitId
            ? this._univerInstanceService.getUnit<Workbook>(unitId, UniverInstanceType.UNIVER_SHEET)
            : this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET);
        if (!workbook) {
            return {
                sheetOrder: [],
                sheetNameMap: {},
            };
        }
        const { id, sheetOrder } = workbook.getSnapshot();

        return {
            sheetOrder,
            sheetNameMap: this._sheetIdToNameMap[id] ?? {},
        };
    }

    getSheetRowColumnCount(unitId: string, sheetId: string) {
        if (this._univerInstanceService.getUnitType(unitId) !== UniverInstanceType.UNIVER_SHEET) {
            return { rowCount: 0, columnCount: 0 };
        }

        const workbook = this._univerInstanceService.getUnit<Workbook>(unitId);
        const worksheet = workbook?.getSheetBySheetId(sheetId);
        const snapshot = worksheet?.getSnapshot();

        if (!snapshot) {
            return { rowCount: 0, columnCount: 0 };
        }

        const { rowCount, columnCount } = snapshot;

        return { rowCount, columnCount };
    }

    getFilteredOutRows(unitId: string, sheetId: string, startRow: number, endRow: number) {
        const filteredOutRows: number[] = [];

        for (let r = startRow; r <= endRow; r++) {
            if (this._calculationRowData[unitId]?.[sheetId]?.[r]?.filtered
                || this._sheetRowFilteredService.getRowFiltered(unitId, sheetId, r)) {
                filteredOutRows.push(r);
            }
        }

        return filteredOutRows;
    }

    load(config: IFormulaDatasetConfig) {
        this._loadFormulaDataset(config);
    }

    /** Internal engine capability to defer formula DTO materialization. */
    loadWithFormulaDataSnapshot(config: Omit<IFormulaDatasetConfig, 'formulaData'>, snapshot: IFormulaDataSnapshot): void {
        this._loadFormulaDataset(config, snapshot);
    }

    private _loadFormulaDataset(config: IFormulaDatasetConfig | Omit<IFormulaDatasetConfig, 'formulaData'>, snapshot?: IFormulaDataSnapshot): void {
        this._calculationGeneration++;
        this._calculationRowData = Object.create(null);
        if (config.allUnitData && config.unitSheetNameMap && config.unitStylesData) {
            this._unitData = copyUnitData(config.allUnitData);
            this._unitStylesData = config.unitStylesData;
            this._sheetNameMap = copyUnitData(config.unitSheetNameMap);
            this._unitNameMap = config.unitNameMap || {};
        } else {
            const { allUnitData, unitNameMap, unitSheetNameMap, unitStylesData } = this._loadSheetData();

            this._unitData = copyUnitData(allUnitData);

            this._unitStylesData = unitStylesData;

            this._sheetNameMap = copyUnitData(unitSheetNameMap);

            this._unitNameMap = unitNameMap;
        }

        // apply row data, including rows hidden by filters
        if (config.rowData) {
            this._applyUnitRowData(config.rowData);
        }

        this._formulaData = snapshot ? Object.create(null) : (config as IFormulaDatasetConfig).formulaData;
        this._formulaDataSnapshot = snapshot;
        if (snapshot) DEFERRED_FORMULA_DATA_SNAPSHOTS.add(this);
        else DEFERRED_FORMULA_DATA_SNAPSHOTS.delete(this);
        this._dirtyIterationUnits = config.dirtyIterationUnits ?? [];
        this._dirtyReferenceUnits = config.dirtyReferenceUnits ?? [];

        this._arrayFormulaCellData = convertUnitDataToRuntime(config.arrayFormulaCellData);

        this._arrayFormulaRange = config.arrayFormulaRange;

        this._forceCalculate = config.forceCalculate;

        this._clearDependencyTreeCache = config.clearDependencyTreeCache || {};

        this._dirtyRanges = config.dirtyRanges;

        this._dirtyNameMap = config.dirtyNameMap;

        this._dirtyDefinedNameMap = config.dirtyDefinedNameMap;

        this._dirtySuperTableMap = config.dirtySuperTableMap || {};

        this._dirtyUnitFeatureMap = config.dirtyUnitFeatureMap;

        this._dirtyUnitOtherFormulaMap = config.dirtyUnitOtherFormulaMap;

        this._externalReferences = config.externalReferences ?? {};

        this._excludedCell = config.excludedCell;

        this._mergeNameMap(this._sheetNameMap, this._dirtyNameMap);
    }

    loadDataLite(rowData?: IUnitRowData) {
        const { allUnitData, unitNameMap, unitSheetNameMap, unitStylesData } = this._loadSheetData();

        this._unitData = copyUnitData(allUnitData);

        this._unitStylesData = unitStylesData;

        this._sheetNameMap = copyUnitData(unitSheetNameMap);

        this._unitNameMap = unitNameMap;

        this._formulaData = this._formulaDataModel.getFormulaData();
        this._formulaDataSnapshot = undefined;
        DEFERRED_FORMULA_DATA_SNAPSHOTS.delete(this);
        this._arrayFormulaCellData = convertUnitDataToRuntime(this._formulaDataModel.getArrayFormulaCellData());
        this._arrayFormulaRange = this._formulaDataModel.getArrayFormulaRange();

        // apply row data, including rows hidden by filters
        rowData && this._applyUnitRowData(rowData);
    }

    // Feature callbacks expose no formula metadata; keep their conservative invalidation local.
    getDirtyData(forFeatureCalculation = false): IFormulaDirtyData {
        return {
            dirtyIterationUnits: this._dirtyIterationUnits,
            dirtyReferenceUnits: this._dirtyReferenceUnits,
            forceCalculation: this._forceCalculate || (forFeatureCalculation && this._dirtyReferenceUnits.length > 0),
            dirtyRanges: this._dirtyRanges,
            dirtyNameMap: this._dirtyNameMap,
            dirtyDefinedNameMap: this._dirtyDefinedNameMap,
            dirtySuperTableMap: this._dirtySuperTableMap,
            dirtyUnitFeatureMap: this._dirtyUnitFeatureMap,
            dirtyUnitOtherFormulaMap: this._dirtyUnitOtherFormulaMap,
            clearDependencyTreeCache: this._clearDependencyTreeCache,
            externalReferences: this._externalReferences,
        };
    }

    loadDirtyRangesAndExcludedCell(dirtyRanges: IUnitRange[], excludedCell?: IUnitExcludedCell) {
        this._dirtyIterationUnits = [];
        this._dirtyReferenceUnits = [];
        this._dirtyRanges = dirtyRanges;

        this._excludedCell = excludedCell;

        /**
         * Mark dirty for expansion of array formulas, need to clear the worksheet's dirty flag.
         */
        this._dirtyNameMap = Object.create(null);
    }

    registerUnitData(unitData: IUnitData) {
        this._unitData = copyUnitData(unitData);
    }

    registerFormulaData(formulaData: IFormulaData) {
        this._formulaData = formulaData;
        this._formulaDataSnapshot = undefined;
        DEFERRED_FORMULA_DATA_SNAPSHOTS.delete(this);
    }

    registerSheetNameMap(sheetNameMap: IUnitSheetNameMap) {
        this._sheetNameMap = copyUnitData(sheetNameMap);
    }

    registerUnitNameMap(unitNameMap: IFormulaUnitNameMap) {
        this._unitNameMap = unitNameMap;
    }

    private _mergeNameMap(unitSheetNameMap: IUnitSheetNameMap, dirtyNameMap: IDirtyUnitSheetNameMap) {
        Object.keys(dirtyNameMap).forEach((unitId) => {
            if (dirtyNameMap[unitId]) {
                Object.keys(dirtyNameMap[unitId]!).forEach((sheetId) => {
                    if (unitSheetNameMap[unitId] == null) {
                        unitSheetNameMap[unitId] = Object.create(null);
                    }
                    unitSheetNameMap[unitId]![dirtyNameMap[unitId]![sheetId]] = sheetId;
                });
            }
        });

        this._sheetIdToNameMap = Object.create(null);

        Object.keys(unitSheetNameMap).forEach((unitId) => {
            Object.keys(unitSheetNameMap[unitId]!).forEach((sheetName) => {
                if (this._sheetIdToNameMap[unitId] == null) {
                    this._sheetIdToNameMap[unitId] = Object.create(null);
                }
                this._sheetIdToNameMap[unitId]![unitSheetNameMap[unitId]![sheetName]] = sheetName;
            });
        });
    }

    private _loadSheetData() {
        const workbook = this._univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
        const worksheet = workbook?.getActiveSheet();
        const base = workbook ? null : this._univerInstanceService.getCurrentUnitOfType<BaseDataModel>(UniverInstanceType.UNIVER_BASE);
        const table = base ? getFirstBaseTable(base.getSnapshot().tables, base.getSnapshot().tableOrder) : null;

        this._executeUnitId = workbook?.getUnitId() ?? base?.getUnitId();
        this._executeSubUnitId = worksheet?.getSheetId() ?? table?.id;

        return this._formulaDataModel.getCalculateData();
    }

    /**
     * There is no filter information in the worker, it must be passed in from the main thread after it is ready
     * @param rowData
     */
    private _applyUnitRowData(rowData: IUnitRowData) {
        this._calculationRowData = rowData;
        for (const unitId of Object.keys(rowData)) {
            if (rowData[unitId] == null) {
                continue;
            }

            for (const sheetId of Object.keys(rowData[unitId])) {
                if (rowData[unitId][sheetId] == null) {
                    continue;
                }

                if (this._unitData[unitId] == null) {
                    this._unitData[unitId] = Object.create(null);
                }

                if (this._unitData[unitId][sheetId] == null) {
                    this._unitData[unitId][sheetId] = {
                        cellData: new ObjectMatrix({}),
                        rowCount: 0,
                        columnCount: 0,
                        rowData: {},
                        columnData: {},
                    };
                }

                this._unitData[unitId][sheetId].rowData = rowData[unitId][sheetId];
            }
        }
    }
}

function getFirstBaseTable(tables: Record<string, ITableSnapshot>, tableOrder: string[]): ITableSnapshot | null {
    for (const tableId of tableOrder) {
        const table = tables[tableId];
        if (table) {
            return table;
        }
    }

    return Object.values(tables)[0] ?? null;
}

export const IFormulaCurrentConfigService = createIdentifier<IFormulaCurrentConfigService>(
    'univer.formula.current-data.service'
);