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.

4.5 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 { IWorksheetData } from './typedef';
import { BooleanNumber } from '../types/enum/text-style';
import { isValidSheetDimension, normalizeSheetDimension } from './sheet-dimensions';

// TODO@wzhudev: default value should not be exposed, but the keys.

export const DEFAULT_WORKSHEET_ROW_COUNT_KEY = 'DEFAULT_WORKSHEET_ROW_COUNT';
export const DEFAULT_WORKSHEET_ROW_COUNT = 1000;

export const DEFAULT_WORKSHEET_COLUMN_COUNT_KEY = 'DEFAULT_WORKSHEET_COLUMN_COUNT';
export const DEFAULT_WORKSHEET_COLUMN_COUNT = 20;

export const DEFAULT_WORKSHEET_ROW_HEIGHT_KEY = 'DEFAULT_WORKSHEET_ROW_HEIGHT';
export const DEFAULT_WORKSHEET_ROW_HEIGHT = 24;

export const DEFAULT_WORKSHEET_COLUMN_WIDTH_KEY = 'DEFAULT_WORKSHEET_COLUMN_WIDTH';
export const DEFAULT_WORKSHEET_COLUMN_WIDTH = 88;

export const DEFAULT_WORKSHEET_ROW_TITLE_WIDTH_KEY = 'DEFAULT_WORKSHEET_ROW_TITLE_WIDTH';
export const DEFAULT_WORKSHEET_ROW_TITLE_WIDTH = 46;

export const DEFAULT_WORKSHEET_COLUMN_TITLE_HEIGHT_KEY = 'DEFAULT_WORKSHEET_COLUMN_TITLE_HEIGHT';
export const DEFAULT_WORKSHEET_COLUMN_TITLE_HEIGHT = 20;

/**
 * This function is used to merge the user passed in snapshot with the default snapshot
 * without changing the user's snapshot's reference.
 *
 * @param snapshot user passed in snapshot
 * @returns merged snapshot
 */
export function mergeWorksheetSnapshotWithDefault(snapshot: Partial<IWorksheetData>): IWorksheetData {
    const defaultSnapshot: IWorksheetData = {
        name: 'Sheet1', // TODO: name should have i18n
        id: 'sheet-01',
        tabColor: '',
        hidden: BooleanNumber.FALSE,
        rowCount: DEFAULT_WORKSHEET_ROW_COUNT,
        columnCount: DEFAULT_WORKSHEET_COLUMN_COUNT,
        zoomRatio: 1,
        freeze: {
            xSplit: 0,
            ySplit: 0,
            startRow: -1,
            startColumn: -1,
        },
        scrollTop: 0,
        scrollLeft: 0,
        defaultColumnWidth: DEFAULT_WORKSHEET_COLUMN_WIDTH,
        defaultRowHeight: DEFAULT_WORKSHEET_ROW_HEIGHT,
        mergeData: [],
        cellData: {},
        rowData: {},
        columnData: {},
        showGridlines: BooleanNumber.TRUE,
        rowHeader: {
            width: DEFAULT_WORKSHEET_ROW_TITLE_WIDTH,
            hidden: BooleanNumber.FALSE,
        },
        columnHeader: {
            height: DEFAULT_WORKSHEET_COLUMN_TITLE_HEIGHT,
            hidden: BooleanNumber.FALSE,
        },
        rightToLeft: BooleanNumber.FALSE,
    };

    // Merge the user's snapshot with the default snapshot.
    Object.keys(defaultSnapshot).forEach((_key) => {
        const key = _key as keyof IWorksheetData;
        if (typeof snapshot[key] === 'undefined') {
            // @ts-ignore
            snapshot[key] = defaultSnapshot[key];
        }
    });

    const freeze = snapshot.freeze!;
    freeze.xSplit = freeze.xSplit ?? defaultSnapshot.freeze.xSplit;
    freeze.ySplit = freeze.ySplit ?? defaultSnapshot.freeze.ySplit;
    freeze.startRow = freeze.ySplit === 0 ? defaultSnapshot.freeze.startRow : (freeze.startRow ?? defaultSnapshot.freeze.startRow);
    freeze.startColumn = freeze.xSplit === 0 ? defaultSnapshot.freeze.startColumn : (freeze.startColumn ?? defaultSnapshot.freeze.startColumn);

    snapshot.defaultColumnWidth = normalizeSheetDimension(snapshot.defaultColumnWidth, DEFAULT_WORKSHEET_COLUMN_WIDTH, false);
    snapshot.defaultRowHeight = normalizeSheetDimension(snapshot.defaultRowHeight, DEFAULT_WORKSHEET_ROW_HEIGHT, false);

    for (const column of Object.values(snapshot.columnData!)) {
        if (column && column.w !== undefined && !isValidSheetDimension(column.w)) {
            column.w = 0;
        }
    }

    for (const row of Object.values(snapshot.rowData!)) {
        if (!row) {
            continue;
        }
        if (row.h !== undefined && !isValidSheetDimension(row.h)) {
            row.h = 0;
        }
        if (row.ah !== undefined && !isValidSheetDimension(row.ah)) {
            delete row.ah;
        }
    }

    return snapshot as IWorksheetData;
}