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.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 { Observable } from 'rxjs';
import type { Nullable } from '../shared';
import type { IStyleData } from '../types/interfaces';
import type { IWorkbookCalculationSettings } from './calculation-settings';
import type { CustomData, IRangeType, IWorkbookData, IWorksheetData } from './typedef';
import { BehaviorSubject, Subject } from 'rxjs';
import { UnitModel, UniverInstanceType } from '../common/unit';
import { ILogService } from '../services/log/log.service';
import { Tools } from '../shared';
import { generateRandomId } from '../shared/random-id';
import { BooleanNumber } from '../types/enum';
import { DateSystem } from '../types/enum/date-system';
import { normalizeWorkbookCalculationSettings } from './calculation-settings';
import { getEmptySnapshot } from './empty-snapshot';
import { Styles } from './styles';
import { WorksheetHiddenState } from './typedef';
import { Worksheet } from './worksheet';

export function getWorksheetUID(workbook: Workbook, worksheet: Worksheet): string {
    return `${workbook.getUnitId()}|${worksheet.getSheetId()}`;
}

/**
 * Access and create Univer Sheets files
 */
export class Workbook extends UnitModel<IWorkbookData, UniverInstanceType.UNIVER_SHEET> {
    override type: UniverInstanceType.UNIVER_SHEET = UniverInstanceType.UNIVER_SHEET;

    private readonly _sheetCreated$ = new Subject<Worksheet>();
    readonly sheetCreated$ = this._sheetCreated$.asObservable();

    private readonly _sheetDisposed$ = new Subject<Worksheet>();
    readonly sheetDisposed$ = this._sheetDisposed$.asObservable();

    private readonly _activeSheet$ = new BehaviorSubject<Nullable<Worksheet>>(null);
    private get _activeSheet(): Nullable<Worksheet> { return this._activeSheet$.getValue(); }
    readonly activeSheet$ = this._activeSheet$.asObservable();

    /**
     * sheets list
     * @private
     */
    private _worksheets: Map<string, Worksheet>;

    /**
     * Common style
     * @private
     */
    private _styles: Styles;

    /**
     * number format
     * @private
     */
    // private _formatManage: FormatManager;

    private _snapshot: IWorkbookData;

    private _unitId: string;

    private _count: number;

    private readonly _name$: BehaviorSubject<string>;
    readonly name$: Observable<string>;
    get name(): string {
        return this._name$.getValue();
    }

    static isIRangeType(range: IRangeType | IRangeType[]): boolean {
        return typeof range === 'string' || 'startRow' in range || 'row' in range;
    }

    constructor(
        workbookData: Partial<IWorkbookData> = {},
        @ILogService private readonly _logService: ILogService
    ) {
        super();

        const DEFAULT_WORKBOOK = getEmptySnapshot();
        if (Tools.isEmptyObject(workbookData)) {
            this._snapshot = DEFAULT_WORKBOOK;
        } else {
            this._snapshot = Tools.commonExtend(DEFAULT_WORKBOOK, workbookData);
            if (workbookData.dateSystem == null) {
                delete this._snapshot.dateSystem;
            } else if (workbookData.dateSystem !== DateSystem.Date1900 && workbookData.dateSystem !== DateSystem.Date1904) {
                this._logService.warn('[Workbook]', `Unknown date system "${String(workbookData.dateSystem)}"; falling back to Excel 1900.`);
                this._snapshot.dateSystem = DateSystem.Date1900;
            }
        }

        this._snapshot.calculationSettings = normalizeWorkbookCalculationSettings(workbookData.calculationSettings);

        const { styles } = this._snapshot;
        if (this._snapshot.id == null || this._snapshot.id.length === 0) {
            this._snapshot.id = generateRandomId(6);
        }

        this._unitId = this._snapshot.id;
        this._styles = new Styles(styles);
        this._count = 1;
        this._worksheets = new Map<string, Worksheet>();

        this._name$ = new BehaviorSubject(workbookData.name || '');
        this.name$ = this._name$.asObservable();

        this._parseWorksheetSnapshots();
    }

    override dispose(): void {
        super.dispose();

        this._sheetCreated$.complete();
        this._sheetDisposed$.complete();
        this._activeSheet$.complete();
        this._name$.complete();

        const sheetIds = Array.from(this._worksheets.keys());
        sheetIds.forEach((id) => {
            this._removeSheet(id);
        });
    }

    /**
     * Create a clone of the current snapshot.
     * Call resourceLoaderService.saveWorkbook to save the data associated with the current plugin if needed.
     * @memberof Workbook
     */
    save(): IWorkbookData {
        return Tools.deepClone(this._snapshot);
    }

    /**
     * Get current snapshot reference.
     * Call resourceLoaderService.saveWorkbook to save the data associated with the current plugin if needed.
     * @return {*}  {IWorkbookData}
     * @memberof Workbook
     */
    getSnapshot(): IWorkbookData {
        return this._snapshot;
    }

    getCalculationSettings(): IWorkbookCalculationSettings {
        return normalizeWorkbookCalculationSettings(this._snapshot.calculationSettings);
    }

    setCalculationSettings(settings: IWorkbookCalculationSettings): void {
        this._snapshot.calculationSettings = { ...settings };
    }

    setName(name: string): void {
        this._name$.next(name);
        this._snapshot.name = name;
    }

    getUnitId() {
        return this._unitId;
    }

    getDateSystem(): DateSystem {
        return this._snapshot.dateSystem === DateSystem.Date1904
            ? DateSystem.Date1904
            : DateSystem.Date1900;
    }

    override getRev(): number {
        return this._snapshot.rev ?? 1; // the revision number should start with one
    }

    override incrementRev(): void {
        this._snapshot.rev = this.getRev() + 1;
    }

    override setRev(rev: number): void {
        this._snapshot.rev = rev;
    }

    /**
     * Add a Worksheet into Workbook.
     */
    addWorksheet(id: string, index: number, worksheetSnapshot: Partial<IWorksheetData>): boolean {
        const { sheets, sheetOrder } = this._snapshot;
        if (sheets[id]) {
            return false;
        }

        sheets[id] = worksheetSnapshot;
        sheetOrder.splice(index, 0, id);
        this.ensureSheetOrderUnique();
        const worksheet = new Worksheet(this._unitId, worksheetSnapshot, this._styles);
        this._worksheets.set(id, worksheet);
        this._sheetCreated$.next(worksheet);

        return true;
    }

    getSheetOrders(): Readonly<string[]> {
        return this._snapshot.sheetOrder;
    }

    // Ensure sheet order is unique
    ensureSheetOrderUnique() {
        const seen = new Set<string>();
        const result: string[] = [];

        for (const item of this._snapshot.sheetOrder) {
            if (!seen.has(item)) {
                seen.add(item);
                result.push(item);
            }
        }

        this._snapshot.sheetOrder = result;
        seen.clear();
    }

    getWorksheets(): Map<string, Worksheet> {
        return this._worksheets;
    }

    getActiveSpreadsheet(): Workbook {
        return this;
    }

    getStyles(): Styles {
        return this._styles;
    }

    addStyles(styles: Record<string, Nullable<IStyleData>>): void {
        Object.entries(styles).forEach(([id, data]) => {
            this._styles.addCustomStyle(id, data);
        });
    }

    removeStyles(ids: string[]) {
        ids.forEach((id) => {
            this._styles.remove(id);
        });
    }

    getConfig(): IWorkbookData {
        return this._snapshot;
    }

    getIndexBySheetId(sheetId: string): number {
        const { sheetOrder } = this._snapshot;
        return sheetOrder.findIndex((id) => id === sheetId);
    }

    /**
     * Get the active sheet.
     */
    getActiveSheet(): Worksheet;
    getActiveSheet(allowNull: true): Nullable<Worksheet>;
    getActiveSheet(allowNull?: true): Nullable<Worksheet> {
        if (!this._activeSheet && typeof allowNull === 'undefined') {
            throw new Error(`[Workbook]: no active Worksheet on Workbook ${this._unitId}!`);
        }

        return this._activeSheet;
    }

    /**
     * If there is no active sheet, the first sheet would
     * be set active.
     * @returns
     */
    ensureActiveSheet() {
        const currentActive = this._activeSheet;
        if (currentActive) {
            return currentActive;
        }

        /**
         * If the first sheet is hidden, we should set the first unhidden sheet to be active.
         */
        const sheetOrder = this._snapshot.sheetOrder;
        for (let i = 0, len = sheetOrder.length; i < len; i++) {
            const worksheet = this._worksheets.get(sheetOrder[i]);
            if (worksheet && worksheet.isSheetHidden() !== BooleanNumber.TRUE) {
                this.setActiveSheet(worksheet);
                return worksheet;
            }
        }

        const worksheet = this._worksheets.get(sheetOrder[0])!;
        this.setActiveSheet(worksheet);
        return worksheet;
    }

    /**
     * ActiveSheet should not be null!
     * There is at least one sheet in a workbook. You can not delete all sheets in a workbook.
     * @param worksheet
     */
    setActiveSheet(worksheet: Worksheet): void {
        this._activeSheet$.next(worksheet);
    }

    private _removeSheet(sheetId: string): boolean {
        const sheetToRemove = this._worksheets.get(sheetId);
        if (!sheetToRemove) {
            return false;
        }

        this._worksheets.delete(sheetId);
        this._snapshot.sheetOrder.splice(this._snapshot.sheetOrder.indexOf(sheetId), 1);
        this.ensureSheetOrderUnique();
        this._sheetDisposed$.next(sheetToRemove);

        return true;
    }

    removeSheet(sheetId: string): boolean {
        const success = this._removeSheet(sheetId);
        if (success) {
            delete this._snapshot.sheets[sheetId];
        }

        return success;
    }

    getActiveSheetIndex(): number {
        const { sheetOrder } = this._snapshot;
        return sheetOrder.findIndex((sheetId) => {
            const worksheet = this._worksheets.get(sheetId)!;
            return worksheet === this._activeSheet;
        });
    }

    getSheetSize(): number {
        return this._snapshot.sheetOrder.length;
    }

    getSheets(): Worksheet[] {
        const { sheetOrder } = this._snapshot;

        return sheetOrder.map((sheetId) => this._worksheets.get(sheetId)) as Worksheet[];
    }

    getSheetsName(): string[] {
        const { sheetOrder } = this._snapshot;

        const names: string[] = [];

        sheetOrder.forEach((sheetId) => {
            const worksheet = this._worksheets.get(sheetId);
            if (worksheet) {
                names.push(worksheet.getName());
            }
        });

        return names;
    }

    getSheetIndex(sheet: Worksheet): number {
        const { sheetOrder } = this._snapshot;

        return sheetOrder.findIndex((sheetId) => {
            if (sheet.getSheetId() === sheetId) {
                return true;
            }
            return false;
        });
    }

    getSheetBySheetName(name: string): Nullable<Worksheet> {
        const { sheetOrder } = this._snapshot;
        const sheetId = sheetOrder.find((sheetId) => {
            const worksheet = this._worksheets.get(sheetId) as Worksheet;
            return worksheet.getName() === name;
        }) as string;
        return this._worksheets.get(sheetId);
    }

    getSheetBySheetId(sheetId: string): Nullable<Worksheet> {
        return this._worksheets.get(sheetId);
    }

    getSheetByIndex(index: number): Nullable<Worksheet> {
        const { sheetOrder } = this._snapshot;
        return this._worksheets.get(sheetOrder[index]);
    }

    getHiddenWorksheets(includeVeryHidden = true): string[] {
        return this.getSheets()
            .filter((s) => s.isSheetHidden() === BooleanNumber.TRUE)
            .filter((s) => includeVeryHidden || s.getHiddenState() !== WorksheetHiddenState.VERY_HIDDEN)
            .map((s) => s.getConfig().id);
    }

    getUnhiddenWorksheets(): string[] {
        return this.getSheets()
            .filter((s) => s.isSheetHidden() !== BooleanNumber.TRUE)
            .map((s) => s.getConfig().id);
    }

    load(config: IWorkbookData) {
        // TODO: new Command
        this._snapshot = config;
    }

    /**
     * Check if sheet name is unique, ignore case sensitivity
     * @param name sheet name
     * @returns True if sheet name is unique
     */
    checkSheetName(name: string): boolean {
        const sheetsName = this.getSheetsName();
        return sheetsName.some((sheetName) => sheetName.toLowerCase() === name.toLowerCase());
    }

    /**
     *  Check whether the sheet name is unique and generate a new unique sheet name
     * @param name sheet name
     * @returns Unique sheet name
     */
    uniqueSheetName(name: string = 'Sheet1'): string {
        let output = name;
        while (this.checkSheetName(output)) {
            output = name + this._count;
            this._count++;
        }
        return output;
    }

    /**
     * Automatically generate new sheet name
     * @param name sheet name
     * @returns New sheet name
     */
    generateNewSheetName(name: string) {
        let output = name + this._count;
        while (this.checkSheetName(output)) {
            output = name + this._count;
            this._count++;
        }
        return output;
    }

    // FIXME: now we always create worksheet from DEFAULT_WORKSHEET?

    /**
     * Get Default Sheet
     */
    private _parseWorksheetSnapshots(): void {
        const { _snapshot, _worksheets } = this;
        const { sheets, sheetOrder } = _snapshot;

        // If there's no sheets in the snapshot, we should create one.
        if (Tools.isEmptyObject(sheets)) {
            const firstSheetId = generateRandomId();
            sheets[firstSheetId] = { id: firstSheetId };
        }

        // Pass all existing sheets.
        for (const sheetId in sheets) {
            const worksheetSnapshot = sheets[sheetId];
            const { name } = worksheetSnapshot;

            worksheetSnapshot.name = this.uniqueSheetName(name);
            if (worksheetSnapshot.name !== name) {
                this._logService.debug('[Workbook]', `The worksheet name ${name} is duplicated, we changed it to ${worksheetSnapshot.name}. Please fix the problem in your snapshot.`);
            }

            const worksheet = new Worksheet(this._unitId, worksheetSnapshot, this._styles);
            _worksheets.set(sheetId, worksheet);

            if (!sheetOrder.includes(sheetId)) {
                sheetOrder.push(sheetId);
            }
        }

        this.ensureSheetOrderUnique();

        // Active the first sheet.
        this.ensureActiveSheet();
    }

    /**
     * Get custom metadata of workbook
     * @returns {CustomData | undefined} custom metadata
     */
    getCustomMetadata(): CustomData | undefined {
        return this._snapshot.custom;
    }

    /**
     * Set custom metadata of workbook
     * @param {CustomData | undefined} custom custom metadata
     */
    setCustomMetadata(custom: CustomData | undefined) {
        this._snapshot.custom = custom;
    }
}