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.

6.9 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 { BaseReferenceObject } from '../../../engine/reference-object/base-reference-object';
import type { ArrayValueObject } from '../../../engine/value-object/array-value-object';
import type { BaseValueObject } from '../../../engine/value-object/base-value-object';
import { MAX_COLUMN_COUNT, MAX_ROW_COUNT } from '@univerjs/core';
import { ErrorType } from '../../../basics/error-type';
import { regexTestColumn, regexTestRow, regexTestSingeRange } from '../../../basics/regex';
import { operatorToken } from '../../../basics/token';
import { CellReferenceObject } from '../../../engine/reference-object/cell-reference-object';
import { ColumnReferenceObject } from '../../../engine/reference-object/column-reference-object';
import { RangeReferenceObject } from '../../../engine/reference-object/range-reference-object';
import { RowReferenceObject } from '../../../engine/reference-object/row-reference-object';
import { deserializeRangeForR1C1 } from '../../../engine/utils/r1c1-reference';
import { deserializeRangeWithSheetWithCache } from '../../../engine/utils/reference-cache';
import { ErrorValueObject } from '../../../engine/value-object/base-value-object';
import { BaseFunction } from '../../base-function';

const INDIRECT_ERROR_LITERALS = [
    ErrorType.NULL,
    ErrorType.DIV_BY_ZERO,
    ErrorType.VALUE,
    ErrorType.REF,
    ErrorType.NAME,
    ErrorType.NUM,
    ErrorType.NA,
    ErrorType.CYCLE,
    ErrorType.CALC,
] as const;

export class Indirect extends BaseFunction {
    override minParams = 1;

    override maxParams = 2;

    override needsUnitReferenceResolver = true;

    override isAddress() {
        return true;
    }

    override calculate(refText: BaseValueObject, a1?: BaseValueObject) {
        if (refText.isError()) {
            return refText;
        }

        if (a1?.isError()) {
            return a1;
        }

        let a1Value = this.getZeroOrOneByOneDefault(a1);

        if (a1Value == null) {
            a1Value = 1;
        }

        let _refText = refText;

        if (refText.isArray()) {
            const rowCount = (refText as ArrayValueObject).getRowCount();
            const columnCount = (refText as ArrayValueObject).getColumnCount();

            if (rowCount > 1 || columnCount > 1) {
                // TODO
                return refText.map(() => {
                    return ErrorValueObject.create(ErrorType.VALUE);
                });
            }

            _refText = (refText as ArrayValueObject).getFirstCell();
        }

        return this._handleSingleObject(_refText, a1Value);
    }

    private _handleSingleObject(refTextObject: BaseValueObject, a1Value: number) {
        const refTextValue = `${refTextObject.getValue()}`;

        if (refTextValue.trim() === '') {
            return ErrorValueObject.create(ErrorType.REF);
        }

        const embeddedError = this._matchErrorLiteral(refTextValue);
        if (embeddedError != null) {
            return ErrorValueObject.create(embeddedError);
        }

        const refTextV = this._convertToDefinedName(refTextValue);

        if (a1Value === 0) {
            const gridRange = deserializeRangeForR1C1(refTextV);

            const { range, sheetName, unitId } = gridRange;

            const rangeReferenceObject = new RangeReferenceObject(range);

            rangeReferenceObject.setUnitQualifier(unitId);
            rangeReferenceObject.setForcedUnitIdDirect(unitId);
            rangeReferenceObject.setForcedSheetName(sheetName);

            return this._setDefault(rangeReferenceObject);
        }

        if (regexTestSingeRange(refTextV)) {
            return this._setDefault(new CellReferenceObject(refTextV));
        }
        if (regexTestRow(refTextV)) {
            return this._setDefault(new RowReferenceObject(refTextV));
        }

        if (regexTestColumn(refTextV)) {
            return this._setDefault(new ColumnReferenceObject(refTextV));
        }

        const gridRange = deserializeRangeWithSheetWithCache(refTextV);

        const { range, sheetName, unitId } = gridRange;

        if (Number.isNaN(range.startRow) || range.endRow + 1 > MAX_ROW_COUNT || Number.isNaN(range.startColumn) || range.endColumn + 1 > MAX_COLUMN_COUNT) {
            return ErrorValueObject.create(ErrorType.REF);
        }

        const rangeReferenceObject = new RangeReferenceObject(range);

        rangeReferenceObject.setUnitQualifier(unitId);
        rangeReferenceObject.setForcedUnitIdDirect(unitId);
        rangeReferenceObject.setForcedSheetName(sheetName);

        return this._setDefault(rangeReferenceObject);
    }

    private _setDefault(object: BaseReferenceObject) {
        if (this.unitId == null || this.subUnitId == null) {
            return ErrorValueObject.create(ErrorType.REF);
        }
        const unitQualifier = object.getUnitQualifier();
        if (unitQualifier && this._unitReferenceResolver) {
            const resolution = this._unitReferenceResolver.resolve({
                hostUnitId: this.unitId,
                qualifier: unitQualifier,
                referenceKind: 'a1',
            });
            if (typeof resolution === 'string') {
                return ErrorValueObject.create(resolution);
            }
            object.setForcedUnitIdDirect(resolution.unitId);
        }
        object.setDefaultUnitId(this.unitId);
        object.setDefaultSheetId(this.subUnitId);
        return object;
    }

    /**
     * In Excel, to inject a defined name into a function that has positioning capabilities,
     * such as using the INDIRECT function to reference a named range,
     * you can write it as follows:
     * =INDIRECT("DefinedName1")
     */
    private _convertToDefinedName(refText: string) {
        const definedName = this.getDefinedName(refText);
        if (definedName == null) {
            return refText;
        }

        const formulaOrRefString = definedName.formulaOrRefString;

        if (formulaOrRefString == null) {
            return refText;
        }

        if (formulaOrRefString.startsWith(operatorToken.EQUALS)) {
            return formulaOrRefString.slice(1);
        }

        return formulaOrRefString;
    }

    private _matchErrorLiteral(refText: string) {
        const normalizedRefText = refText.toUpperCase();
        return INDIRECT_ERROR_LITERALS.find((errorType) => normalizedRefText.includes(errorType));
    }
}