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.

9.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 {
    ICellData,
    IColumnData,
    IObjectArrayPrimitiveType,
    IObjectMatrixPrimitiveType,
    IRange,
    IRowData,
    IUnitRange,
    IWorkbookCalculationSettings,
    Nullable,
    ObjectMatrix,
    Styles,
    UniverInstanceType,
} from '@univerjs/core';
import type { sequenceNodeType } from '../engine/utils/sequence';
import type { IImageFormulaInfo } from '../engine/value-object/primitive-object';

export const ERROR_VALUE_OBJECT_CLASS_TYPE = 'errorValueObject';

export const ASYNC_OBJECT_CLASS_TYPE = 'asyncObject';

export const REFERENCE_OBJECT_CLASS_TYPE = 'referenceObject';

export const VALUE_OBJECT_CLASS_TYPE = 'valueObject';

export enum BooleanValue {
    FALSE = 'FALSE',
    TRUE = 'TRUE',
}

export enum AstNodePromiseType {
    SUCCESS,
    ERROR,
}

export interface ISheetItem {
    cellData: ObjectMatrix<ICellData>;
    rowCount: number;
    columnCount: number;
    rowData: IObjectArrayPrimitiveType<Partial<IRowData>>;
    columnData: IObjectArrayPrimitiveType<Partial<IColumnData>>;
    defaultColumnWidth?: number;
    defaultRowHeight?: number;
}

export interface ISheetData {
    [sheetId: string]: ISheetItem;
}

/**
 * The subset of workbook data needs to be assembled into a new reference object when being passed in,
 * and then input through the FormulaCurrentConfigService.
 */
export interface IUnitData {
    [unitId: string]: ISheetData;
}

/**
 * Style data, numfmt needs to be queried from the style sheet
 */
export interface IUnitStylesData {
    [unitId: string]: Styles;
}

export interface IRuntimeUnitDataType {
    [unitId: string]: Nullable<{ [sheetId: string]: ObjectMatrix<Nullable<ICellData>> }>;
}

export interface IRuntimeUnitDataPrimitiveType {
    [unitId: string]: Nullable<{ [sheetId: string]: IObjectMatrixPrimitiveType<Nullable<ICellData>> }>;
}

export interface IRuntimeOtherUnitDataType {
    [unitId: string]: Nullable<{ [sheetId: string]: Nullable<{ [formulaId: string]: IObjectMatrixPrimitiveType<Nullable<ICellData>[][]> }> }>;
}

export interface IUnitSheetNameMap {
    [unitId: string]: Nullable<{ [sheetName: string]: string }>;
}

export type FormulaUnitType = UniverInstanceType.UNIVER_SHEET | UniverInstanceType.UNIVER_BASE;

export interface IFormulaUnitNameMapItem {
    name: string;
    unitType: FormulaUnitType;
}

/** Runtime Unit metadata keyed by stable unitId. */
export interface IFormulaUnitNameMap {
    [unitId: string]: IFormulaUnitNameMapItem;
}

export interface IFormulaExternalReferenceBinding {
    qualifier: string;
    sourceUnitId: string;
    sourceUnitType: FormulaUnitType;
}

export interface IFormulaExternalReferenceResource {
    references: Record<string, IFormulaExternalReferenceBinding>;
}

/** Host-scoped External Reference resources keyed by the Host unitId. */
export interface IFormulaExternalReferences {
    [hostUnitId: string]: IFormulaExternalReferenceResource;
}

export interface IUnitSheetIdToNameMap {
    [unitId: string]: Nullable<{ [sheetId: string]: string }>;
}

export interface IDirtyUnitSheetNameMap {
    [unitId: string]: Nullable<{ [sheetId: string]: string }>;
}

export interface IDirtyUnitDefinedNameMap {
    [unitId: string]: Nullable<{ [definedName: string]: string }>;
}

export interface IDirtyUnitSuperTableMap {
    [unitId: string]: Nullable<{ [tableName: string]: string }>;
}

export interface IDirtyUnitFeatureMap {
    [unitId: string]: Nullable<{ [sheetId: string]: { [featureId: string]: boolean } }>;
}

export interface IDirtyUnitOtherFormulaMap {
    [unitId: string]: Nullable<{ [sheetId: string]: { [formulaId: string]: boolean } }>;
}

export interface IArrayFormulaRangeType {
    [unitId: string]: Nullable<{ [sheetId: string]: IObjectMatrixPrimitiveType<IRange> }>;
}

export interface IArrayFormulaEmbeddedMap {
    [unitId: string]: Nullable<{ [sheetId: string]: IObjectMatrixPrimitiveType<boolean> }>;
}

export interface IFeatureDirtyRangeType {
    [unitId: string]: Nullable<{ [sheetId: string]: IRange[] }>;
}

export interface IRuntimeImageFormulaDataType extends IImageFormulaInfo {
    unitId: string;
    sheetId: string;
    row: number;
    column: number;
}

export interface IUnitImageFormulaDataType {
    [unitId: string]: Nullable<{ [sheetId: string]: ObjectMatrix<Nullable<IImageFormulaInfo>> }>;
}

export interface IArrayFormulaUnitCellType extends IRuntimeUnitDataPrimitiveType { }

export interface IFormulaData {
    [unitId: string]: Nullable<{ [sheetId: string]: Nullable<IObjectMatrixPrimitiveType<Nullable<IFormulaDataItem>>> }>;
}

export interface IFormulaIdMap {
    f: string;
    r: number;
    c: number;
}

export interface IFormulaStringMap {
    [unitId: string]: Nullable<{ [sheetId: string]: IObjectMatrixPrimitiveType<string[]> }>;
}

export interface IFormulaExecuteResultItem {
    value: Nullable<number | string | boolean | Array<Array<number | string | boolean | null>>>;
    formula: string;
}

export interface IFormulaExecuteResultMap {
    [unitId: string]: Nullable<{ [sheetId: string]: IObjectMatrixPrimitiveType<IFormulaExecuteResultItem[]> }>;
}

export interface IFormulaIdMapData {
    [unitId: string]: Nullable<{ [subUnitId: string]: Nullable<{ [formulaId: string]: IFormulaIdMap }> }>;
}

export interface IOtherFormulaData {
    [unitId: string]: Nullable<{ [subUnitId: string]: Nullable<{ [formulaId: string]: IOtherFormulaDataItem }> }>;
}
/**
 * @f  formulaString, the text string of the formula.
 * @si The formula ID can be utilized in scenarios such as copy-pasting and drag-filling to convert formulas into references, eliminating the need for recreating the formulaString.
 */
export interface IFormulaDataItem {
    f: string; // formulaString
    x?: number; // Offset from x direction
    y?: number; // Offset from y direction
    si?: string; // formulaId,
    // row: number;
    // column: number;
    // sheetId: string;
}

export interface IOtherFormulaDataItem {
    f: string; // formulaString
    ranges: IRange[];
}

export interface ISuperTable {
    sheetId: string;
    titleMap: Map<string, number>;
    range: IRange;
    /**
     * Whether the projected range contains a physical header row.
     * Sheet tables default to true; Base virtual tables store records from row 0.
     */
    showHeader?: boolean;
    /** Whether the projected range contains a physical totals row. */
    showFooter?: boolean;
}

export interface ISuperTableData extends Omit<ISuperTable, 'titleMap'> {
    titleMap: Record<string, number>;
}

export enum TableOptionType {
    ALL = '#All',
    DATA = '#Data',
    HEADERS = '#Headers',
    TOTALS = '#Totals',
    THIS_ROW = '#This Row',
}

export interface IUnitExcludedCell {
    [unitId: string]: Nullable<{ [sheetId: string]: ObjectMatrix<boolean> }>;
}

export interface IUnitRowData {
    // Calculation transport only: distinguish filter exclusions from manually hidden rows in Workers.
    [unitId: string]: Nullable<{ [sheetId: string]: IObjectArrayPrimitiveType<Partial<IRowData> & { filtered?: boolean }> }>;
}

export interface IFormulaDatasetConfig {
    /** Sheet metadata changed; rebind and calculate affected references without forcing all formulas. */
    dirtyReferenceUnits?: string[];
    /** Recalculate iteration-sensitive groups and their dependents in these workbooks. */
    dirtyIterationUnits?: string[];
    /** Snapshot replay limits calculation to affected cycles and their required precedents. */
    calculateCircularOnly?: boolean;
    /** Finalize replay by calculating ordinary formulas without advancing complete circular caches. */
    preserveCircularUnits?: string[];
    formulaData: IFormulaData;
    arrayFormulaCellData: IArrayFormulaUnitCellType;
    arrayFormulaRange: IArrayFormulaRangeType;
    forceCalculate: boolean;
    dirtyRanges: IUnitRange[];
    dirtyNameMap: IDirtyUnitSheetNameMap;
    dirtyDefinedNameMap: IDirtyUnitDefinedNameMap;
    dirtySuperTableMap?: IDirtyUnitSuperTableMap;
    dirtyUnitFeatureMap: IDirtyUnitFeatureMap;
    dirtyUnitOtherFormulaMap: IDirtyUnitOtherFormulaMap;
    clearDependencyTreeCache?: IUnitSheetIdToNameMap;
    excludedCell?: IUnitExcludedCell;
    allUnitData?: IUnitData;
    unitStylesData?: IUnitStylesData;
    unitSheetNameMap?: IUnitSheetNameMap;
    unitNameMap?: IFormulaUnitNameMap;
    externalReferences?: IFormulaExternalReferences;
    calculationSettings?: Record<string, IWorkbookCalculationSettings>;
    isCalculateTreeModel?: boolean;
    rowData?: IUnitRowData; // Include rows hidden by filters
}

export enum ConcatenateType {
    FRONT,
    BACK,
}

export interface IExprTreeNode {
    value: string;
    children: IExprTreeNode[];
    startIndex: number;
    type?: sequenceNodeType;
}