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.

15.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 { ICommandInfo, IRange, Nullable, Workbook } from '@univerjs/core';
import type { ISetDefinedNameMutationParam } from '@univerjs/engine-formula';
import type {
    IDeleteRangeMoveLeftCommandParams,
    IDeleteRangeMoveUpCommandParams,
    IInsertColCommandParams,
    IInsertRangeMoveDownCommandParams,
    IInsertRangeMoveRightCommandParams,
    IInsertRowCommandParams,
    IMoveColsCommandParams,
    IMoveRangeCommandParams,
    IMoveRowsCommandParams,
    IRemoveRowColCommandInterceptParams,
    IRemoveSheetCommandParams,
    ISetWorkbookNameCommandParams,
    ISetWorksheetNameCommandParams,
} from '@univerjs/sheets';
import type { IFormulaReferenceMoveParam } from './ref-range-formula';
import { RANGE_TYPE } from '@univerjs/core';
import {
    DeleteRangeMoveLeftCommand,
    DeleteRangeMoveUpCommand,
    InsertColCommand,
    InsertRangeMoveDownCommand,
    InsertRangeMoveRightCommand,
    InsertRowCommand,
    MoveColsCommand,
    MoveRangeCommand,
    MoveRowsCommand,
    RemoveColCommand,
    RemoveDefinedNameCommand,
    RemoveRowCommand,
    RemoveSheetCommand,
    SetDefinedNameCommand,
    SetWorkbookNameCommand,
    SetWorksheetNameCommand,
} from '@univerjs/sheets';
import { FormulaReferenceMoveType } from './ref-range-formula';

const SET_SHEET_TABLE_COMMAND_ID = 'sheet.command.set-table-config';
const DELETE_SHEET_TABLE_COMMAND_ID = 'sheet.command.delete-table';
const REMOVE_SHEET_TABLE_COLUMN_AT_COMMAND_ID = 'sheet.command.table-remove-column-at';
const REMOVE_SHEET_TABLE_COLUMN_COMMAND_ID = 'sheet.command.table-remove-col';

interface ISetSheetTableNameCommandParam {
    unitId: string;
    name?: string;
    oldTableName?: string;
}

interface IDeleteSheetTableCommandParam {
    unitId: string;
    tableName?: string;
}

interface IRemoveSheetTableColumnCommandParam {
    unitId: string;
    subUnitId?: string;
    range?: IRange;
    tableName?: string;
    removedColumnNames?: string[];
}

export function getReferenceMoveParams(workbook: Workbook, command: ICommandInfo) {
    const { id } = command;
    let result: Nullable<IFormulaReferenceMoveParam> = null;

    switch (id) {
        case MoveRangeCommand.id:
            result = handleRefMoveRange(command as ICommandInfo<IMoveRangeCommandParams>, workbook);
            break;
        case MoveRowsCommand.id:
            result = handleRefMoveRows(command as ICommandInfo<IMoveRowsCommandParams>, workbook);
            break;
        case MoveColsCommand.id:
            result = handleRefMoveCols(command as ICommandInfo<IMoveColsCommandParams>, workbook);
            break;
        case InsertRowCommand.id:
            result = handleRefInsertRow(command as ICommandInfo<IInsertRowCommandParams>);
            break;
        case InsertColCommand.id:
            result = handleRefInsertCol(command as ICommandInfo<IInsertColCommandParams>);
            break;
        case InsertRangeMoveRightCommand.id:
            result = handleRefInsertRangeMoveRight(command as ICommandInfo<IInsertRangeMoveRightCommandParams>, workbook);
            break;
        case InsertRangeMoveDownCommand.id:
            result = handleRefInsertRangeMoveDown(command as ICommandInfo<IInsertRangeMoveDownCommandParams>, workbook);
            break;
        case RemoveRowCommand.id:
            result = handleRefRemoveRow(command as ICommandInfo<IRemoveRowColCommandInterceptParams>, workbook);
            break;
        case RemoveColCommand.id:
            result = handleRefRemoveCol(command as ICommandInfo<IRemoveRowColCommandInterceptParams>);
            break;
        case DeleteRangeMoveUpCommand.id:
            result = handleRefDeleteRangeMoveUp(command as ICommandInfo<IDeleteRangeMoveUpCommandParams>, workbook);
            break;
        case DeleteRangeMoveLeftCommand.id:
            result = handleRefDeleteRangeMoveLeft(command as ICommandInfo<IDeleteRangeMoveLeftCommandParams>, workbook);
            break;
        case SetWorksheetNameCommand.id:
            result = handleRefSetWorksheetName(command as ICommandInfo<ISetWorksheetNameCommandParams>, workbook);
            break;
        case SetWorkbookNameCommand.id:
            result = handleRefSetWorkbookName(command as ICommandInfo<ISetWorkbookNameCommandParams>);
            break;
        case RemoveSheetCommand.id:
            result = handleRefRemoveWorksheet(command as ICommandInfo<IRemoveSheetCommandParams>, workbook);
            break;
        case SetDefinedNameCommand.id:
            result = handleRefSetDefinedName(command as ICommandInfo<ISetDefinedNameMutationParam>, workbook);
            break;
        case RemoveDefinedNameCommand.id:
            result = handleRefRemoveDefinedName(command as ICommandInfo<ISetDefinedNameMutationParam>, workbook);
            break;
        case SET_SHEET_TABLE_COMMAND_ID:
            result = handleRefSetSheetTableName(command as ICommandInfo<ISetSheetTableNameCommandParam>, workbook);
            break;
        case DELETE_SHEET_TABLE_COMMAND_ID:
            result = handleRefRemoveSheetTableName(command as ICommandInfo<IDeleteSheetTableCommandParam>, workbook);
            break;
        case REMOVE_SHEET_TABLE_COLUMN_AT_COMMAND_ID:
        case REMOVE_SHEET_TABLE_COLUMN_COMMAND_ID:
            result = handleRefRemoveSheetTableColumn(command as ICommandInfo<IRemoveSheetTableColumnCommandParam>, workbook);
            break;
    }

    return result;
}

function getCurrentSheetInfo(workbook: Workbook) {
    const unitId = workbook.getUnitId();
    const sheetId = workbook.getActiveSheet()?.getSheetId() || '';

    return {
        unitId,
        sheetId,
    };
}

function handleRefMoveRange(command: ICommandInfo<IMoveRangeCommandParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const {
        fromRange,
        toRange,
        fromUnitId,
        fromSubUnitId,
        toUnitId,
        toSubUnitId,
    } = params;
    if (!fromRange || !toRange) return null;

    const { unitId: currentUnitId, sheetId: currentSheetId } = getCurrentSheetInfo(workbook);
    const unitId = fromUnitId || toUnitId || currentUnitId;
    const sheetId = fromSubUnitId || currentSheetId;
    const sheetName = workbook.getSheetBySheetId(sheetId)?.getName();
    const targetSheetId = toSubUnitId || fromSubUnitId || currentSheetId;
    const targetUnitId = toUnitId || fromUnitId || currentUnitId;
    const targetSheetName = workbook.getSheetBySheetId(targetSheetId)?.getName();

    return {
        type: FormulaReferenceMoveType.MoveRange,
        from: fromRange,
        to: toRange,
        unitId,
        sheetId,
        sheetName,
        targetUnitId,
        targetSheetId,
        targetSheetName,
    };
}

function handleRefMoveRows(command: ICommandInfo<IMoveRowsCommandParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const {
        fromRange: { startRow: fromStartRow, endRow: fromEndRow },
        toRange: { startRow: toStartRow, endRow: toEndRow },
    } = params;

    const unitId = workbook.getUnitId();
    const worksheet = workbook.getActiveSheet();
    if (!worksheet) return null;

    const sheetId = worksheet.getSheetId();

    const from = {
        startRow: fromStartRow,
        startColumn: 0,
        endRow: fromEndRow,
        endColumn: worksheet.getColumnCount() - 1,
        rangeType: RANGE_TYPE.ROW,
    };
    const to = {
        startRow: toStartRow,
        startColumn: 0,
        endRow: toEndRow,
        endColumn: worksheet.getColumnCount() - 1,
        rangeType: RANGE_TYPE.ROW,
    };

    return {
        type: FormulaReferenceMoveType.MoveRows,
        from,
        to,
        unitId,
        sheetId,
    };
}

function handleRefMoveCols(command: ICommandInfo<IMoveColsCommandParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const {
        fromRange: { startColumn: fromStartCol, endColumn: fromEndCol },
        toRange: { startColumn: toStartCol, endColumn: toEndCol },
    } = params;

    const unitId = workbook.getUnitId();
    const worksheet = workbook.getActiveSheet();
    if (!worksheet) return null;

    const sheetId = worksheet.getSheetId();

    const from = {
        startRow: 0,
        startColumn: fromStartCol,
        endRow: worksheet.getRowCount() - 1,
        endColumn: fromEndCol,
        rangeType: RANGE_TYPE.COLUMN,
    };
    const to = {
        startRow: 0,
        startColumn: toStartCol,
        endRow: worksheet.getRowCount() - 1,
        endColumn: toEndCol,
        rangeType: RANGE_TYPE.COLUMN,
    };

    return {
        type: FormulaReferenceMoveType.MoveCols,
        from,
        to,
        unitId,
        sheetId,
    };
}

function handleRefInsertRow(command: ICommandInfo<IInsertRowCommandParams>) {
    const { params } = command;
    if (!params) return null;

    const { range, unitId, subUnitId } = params;
    return {
        type: FormulaReferenceMoveType.InsertRow,
        range,
        unitId,
        sheetId: subUnitId,
    };
}

function handleRefInsertCol(command: ICommandInfo<IInsertColCommandParams>) {
    const { params } = command;
    if (!params) return null;

    const { range, unitId, subUnitId } = params;
    return {
        type: FormulaReferenceMoveType.InsertColumn,
        range,
        unitId,
        sheetId: subUnitId,
    };
}

function handleRefInsertRangeMoveRight(command: ICommandInfo<IInsertRangeMoveRightCommandParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const { range } = params;
    const { unitId, sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.InsertMoveRight,
        range,
        unitId,
        sheetId,
    };
}

function handleRefInsertRangeMoveDown(command: ICommandInfo<IInsertRangeMoveDownCommandParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const { range } = params;
    const { unitId, sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.InsertMoveDown,
        range,
        unitId,
        sheetId,
    };
}

function handleRefRemoveRow(command: ICommandInfo<IRemoveRowColCommandInterceptParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const { range, unitId, subUnitId: sheetId } = params;

    return {
        type: FormulaReferenceMoveType.RemoveRow,
        range,
        unitId,
        sheetId,
        rangeFilteredRows: workbook.getSheetBySheetId(sheetId)?.getRangeFilterRows(range) ?? [],
    };
}

function handleRefRemoveCol(command: ICommandInfo<IRemoveRowColCommandInterceptParams>) {
    const { params } = command;
    if (!params) return null;

    const { range, unitId, subUnitId: sheetId } = params;

    return {
        type: FormulaReferenceMoveType.RemoveColumn,
        range,
        unitId,
        sheetId,
    };
}

function handleRefDeleteRangeMoveUp(command: ICommandInfo<IDeleteRangeMoveUpCommandParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const { range } = params;
    const { unitId, sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.DeleteMoveUp,
        range,
        unitId,
        sheetId,
    };
}

function handleRefDeleteRangeMoveLeft(command: ICommandInfo<IDeleteRangeMoveLeftCommandParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const { range } = params;
    const { unitId, sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.DeleteMoveLeft,
        range,
        unitId,
        sheetId,
    };
}

function handleRefSetWorksheetName(command: ICommandInfo<ISetWorksheetNameCommandParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const { unitId, subUnitId, name } = params;

    const { unitId: workbookId, sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.SetName,
        unitId: unitId || workbookId,
        sheetId: subUnitId || sheetId,
        sheetName: name,
    };
}

function handleRefSetWorkbookName(command: ICommandInfo<ISetWorkbookNameCommandParams>): Nullable<IFormulaReferenceMoveParam> {
    const { params } = command;
    if (!params) return null;
    return {
        type: FormulaReferenceMoveType.SetUnitName,
        unitId: params.unitId,
        sheetId: '',
        unitName: params.name,
    };
}

function handleRefRemoveWorksheet(command: ICommandInfo<IRemoveSheetCommandParams>, workbook: Workbook) {
    const { params } = command;
    if (!params) return null;

    const { unitId, subUnitId } = params;

    const { unitId: workbookId, sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.RemoveSheet,
        unitId: unitId || workbookId,
        sheetId: subUnitId || sheetId,
    };
}

function handleRefSetDefinedName(command: ICommandInfo<ISetDefinedNameMutationParam>, workbook: Workbook): Nullable<IFormulaReferenceMoveParam> {
    const { params } = command;
    if (!params) return null;

    const { unitId, name, id } = params;

    const { sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.SetDefinedName,
        unitId,
        sheetId,
        definedName: name,
        definedNameId: id,
    };
}

function handleRefRemoveDefinedName(command: ICommandInfo<ISetDefinedNameMutationParam>, workbook: Workbook): Nullable<IFormulaReferenceMoveParam> {
    const { params } = command;
    if (!params) return null;

    const { unitId, name, id } = params;

    const { sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.RemoveDefinedName,
        unitId,
        sheetId,
        definedName: name,
        definedNameId: id,
    };
}

function handleRefSetSheetTableName(command: ICommandInfo<ISetSheetTableNameCommandParam>, workbook: Workbook): Nullable<IFormulaReferenceMoveParam> {
    const { params } = command;
    if (!params || !params.name || !params.oldTableName || params.oldTableName === params.name) return null;

    const { unitId, name: tableName, oldTableName } = params;
    const { sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.SetSuperTableName,
        unitId,
        sheetId,
        tableName,
        oldTableName,
    };
}

function handleRefRemoveSheetTableName(command: ICommandInfo<IDeleteSheetTableCommandParam>, workbook: Workbook): Nullable<IFormulaReferenceMoveParam> {
    const { params } = command;
    if (!params || !params.tableName) return null;

    const { unitId, tableName } = params;
    const { sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.RemoveSuperTableName,
        unitId,
        sheetId,
        oldTableName: tableName,
    };
}

function handleRefRemoveSheetTableColumn(command: ICommandInfo<IRemoveSheetTableColumnCommandParam>, workbook: Workbook): Nullable<IFormulaReferenceMoveParam> {
    const { params } = command;
    if (!params || !params.tableName || !params.removedColumnNames?.length) return null;

    const { unitId, subUnitId, range, tableName, removedColumnNames } = params;
    const { sheetId } = getCurrentSheetInfo(workbook);

    return {
        type: FormulaReferenceMoveType.RemoveSuperTableColumn,
        unitId,
        sheetId: subUnitId || sheetId,
        range,
        oldTableName: tableName,
        tableColumnNames: removedColumnNames,
    };
}