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.

28.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 { ICellDataForSheetInterceptor, Injector, Univer, Workbook, Worksheet } from '@univerjs/core';
import type { ISetNumfmtMutationParams, ISheetLocation } from '@univerjs/sheets';
import {
    CellModeEnum,
    CellValueType,
    createInterceptorKey,
    excelDateSerial,
    ICommandService,
    InterceptorManager,
    IUniverInstanceService,
    LocaleType,
    UniverInstanceType,
} from '@univerjs/core';
import { SetNumfmtMutation, SheetInterceptorService } from '@univerjs/sheets';
import { SheetsNumfmtCellContentController } from '@univerjs/sheets-numfmt';
import { getMatrixPlainText, IEditorBridgeService } from '@univerjs/sheets-ui';
import { beforeEach, describe, expect, it } from 'vitest';
import { NumfmtEditorController } from '../numfmt.editor.controller';
import { createTestBed } from './test.util';

const BEFORE_CELL_EDIT = createInterceptorKey<ICellDataForSheetInterceptor, ISheetLocation>('BEFORE_CELL_EDIT');
const AFTER_CELL_EDIT = createInterceptorKey<ICellDataForSheetInterceptor, ISheetLocation>('AFTER_CELL_EDIT');

class MockEditorBridgeService {
    interceptor = new InterceptorManager({
        BEFORE_CELL_EDIT,
        AFTER_CELL_EDIT,
    });
}

describe('test editor', () => {
    let unitId: string = '';
    let subUnitId: string = '';
    let workbook: Workbook;
    let worksheet: Worksheet;
    let testBed: ReturnType<typeof createTestBed>;
    let commandService: ICommandService;

    beforeEach(() => {
        testBed = createTestBed(undefined, [
            [NumfmtEditorController],
            [SheetsNumfmtCellContentController],
            [IEditorBridgeService, { useClass: MockEditorBridgeService }],
        ]);

        unitId = testBed.unitId;
        subUnitId = testBed.subUnitId;
        commandService = testBed.get(ICommandService);
        const univerInstanceService = testBed.get(IUniverInstanceService);
        testBed.get(NumfmtEditorController);
        testBed.get(SheetsNumfmtCellContentController);
        workbook = univerInstanceService.getCurrentUnitOfType<Workbook>(UniverInstanceType.UNIVER_SHEET)!;
        worksheet = workbook.getActiveSheet()!;
    });

    it('before edit with currency', () => {
        const params: ISetNumfmtMutationParams = {
            unitId,
            subUnitId,
            values: {
                1: {
                    ranges: [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 }],
                },
            },
            refMap: {
                1: {
                    pattern: '$#,##0;(#,##0)',
                },
            },
        };
        commandService.syncExecuteCommand(SetNumfmtMutation.id, params);
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = worksheet.getCell(0, 0);
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        expect(cellData!.v).toEqual('$0');
        expect(cellData!.t).toEqual(2);

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(BEFORE_CELL_EDIT)(cellData, location);
        // The currency  format needs to be entered in the editor with real values, not with currency symbols
        expect(result!.v).toEqual(0);
        expect(result!.t).toEqual(2);
    });

    it('before edit with a conditional text format uses the raw value', () => {
        const params: ISetNumfmtMutationParams = {
            unitId,
            subUnitId,
            values: {
                1: {
                    ranges: [{ startRow: 0, endRow: 0, startColumn: 1, endColumn: 1 }],
                },
            },
            refMap: {
                1: {
                    pattern: '[>0]"A";[<0]"B";"B"',
                },
            },
        };
        commandService.syncExecuteCommand(SetNumfmtMutation.id, params);
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = worksheet.getCell(0, 1);
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 1,
            origin: cellData,
        };

        expect(cellData!.v).toBe('A');

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(BEFORE_CELL_EDIT)(cellData, location);
        expect(result!.v).toBe(1);
        expect(result!.t).toBe(CellValueType.NUMBER);
    });

    it('before edit with data', () => {
        const params: ISetNumfmtMutationParams = {
            unitId,
            subUnitId,
            values: {
                1: {
                    ranges: [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 }],
                },
            },
            refMap: {
                1: {
                    pattern: 'h:mm',
                },
            },
        };
        commandService.syncExecuteCommand(SetNumfmtMutation.id, params);
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = worksheet.getCell(0, 0);
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(BEFORE_CELL_EDIT)(cellData, location);
        expect(cellData!.v).toEqual('0:00');
        expect(result!.v).toEqual('0:00:00');
    });

    it('before edit with percent', () => {
        const params: ISetNumfmtMutationParams = {
            unitId,
            subUnitId,
            values: {
                1: {
                    ranges: [{ startRow: 10, endRow: 10, startColumn: 0, endColumn: 0 }],
                },
            },
            refMap: {
                1: {
                    pattern: '0.00%',
                },
            },
        };
        commandService.syncExecuteCommand(SetNumfmtMutation.id, params);
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = worksheet.getCell(10, 0);
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 10,
            col: 0,
            origin: cellData,
        };

        expect(cellData!.v).toEqual('100.12%');
        expect(cellData!.t).toEqual(2);

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(BEFORE_CELL_EDIT)(cellData, location);
        // The currency  format needs to be entered in the editor with real values, not with currency symbols
        expect(result!.v).toEqual('100.1234567%');
        expect(result!.t).toEqual(2);
        expect(result!.isPercentFormat).toBe(true);
    });

    describe('after edit with percent', () => {
        function setPercentFormat() {
            const params: ISetNumfmtMutationParams = {
                unitId,
                subUnitId,
                values: {
                    1: {
                        ranges: [{ startRow: 10, endRow: 10, startColumn: 0, endColumn: 0 }],
                    },
                },
                refMap: {
                    1: {
                        pattern: '0.00%',
                    },
                },
            };
            commandService.syncExecuteCommand(SetNumfmtMutation.id, params);
        }

        it.each([
            ['35%', 0.35],
            ['0.35%', 0.0035],
        ])('stores edited percent text %s as %s', (content, expected) => {
            setPercentFormat();
            const sheetInterceptorService = testBed.get(SheetInterceptorService);
            const result = sheetInterceptorService.writeCellInterceptor
                .fetchThroughInterceptors(AFTER_CELL_EDIT)({ v: content, t: CellValueType.NUMBER }, {
                    workbook,
                    worksheet,
                    unitId,
                    subUnitId,
                    row: 10,
                    col: 0,
                });

            expect(result?.v).toBe(expected);
        });
    });

    it('after edit with data', () => {
        const params: ISetNumfmtMutationParams = {
            unitId,
            subUnitId,
            values: {
                1: {
                    ranges: [{ startRow: 0, endRow: 0, startColumn: 2, endColumn: 2 }],
                },
            },
            refMap: {
                1: {
                    pattern: 'h:mm',
                },
            },
        };
        commandService.syncExecuteCommand(SetNumfmtMutation.id, params);
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '12:33:22', t: 2 };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 2,
            origin: cellData,
        };
        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
        // The date-time drop is a numeric value, not a literal string
        expect(result?.v).toBe(2.523171296296296);
    });

    it('edit number will throw style in this editing', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = {
            p: {
                id: '__INTERNAL_EDITOR__DOCS_NORMAL',
                documentStyle: {
                    pageSize: {
                        width: 230.41758728027344,
                    },
                    marginTop: 6,
                    marginBottom: 2,
                    marginRight: 2,
                    marginLeft: 2,
                    renderConfig: {
                        verticalAlign: 0,
                        horizontalAlign: 0,
                        wrapStrategy: 0,
                        background: {},
                        centerAngle: 0,
                        vertexAngle: 0,
                    },
                },
                body: {
                    dataStream: '2022-11-11\r\n',
                    textRuns: [
                        {
                            st: 0,
                            ed: 2,
                            ts: {
                                ff: 'Arial',
                                fs: 11,
                            },
                        },
                        {
                            st: 2,
                            ed: 10,
                            ts: {
                                ff: 'Arial',
                                fs: 11,
                                bl: 1,
                            },
                        },
                    ],
                    paragraphs: [
                        {
                            startIndex: 10,
                            paragraphId: 'para_sheets_numfmt_edit_date',
                            paragraphStyle: {
                                horizontalAlign: 0,
                            },
                        },
                    ],
                    customRanges: [],
                    customDecorations: [],
                },
                drawings: {},
                drawingsOrder: [],
                settings: {
                    zoomRatio: 1,
                },
            },
            v: null,
            f: null,
            si: null,
        };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
        expect(result?.s).toBeFalsy();
        expect(result?.p).toBeFalsy();
        expect(result?.v).toBe(44876);
    });

    it('edit number with bullet should keep rich text', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const richTextParams = {
            id: '__INTERNAL_EDITOR__DOCS_NORMAL',
            documentStyle: {
                pageSize: {
                    width: 595,
                },
                documentFlavor: 0,
                marginTop: 0,
                marginBottom: 0,
                marginRight: 0,
                marginLeft: 0,
                renderConfig: {
                    vertexAngle: 0,
                    centerAngle: 0,
                },
            },
            drawings: {},
            drawingsOrder: [],
            body: {
                dataStream: '123123123\r\n',
                textRuns: [
                    {
                        st: 0,
                        ed: 9,
                        ts: {
                            ff: 'Arial',
                            fs: 11,
                        },
                    },
                ],
                paragraphs: [
                    {
                        startIndex: 9,
                        paragraphId: 'para_sheets_numfmt_bullet',
                        paragraphStyle: {
                            horizontalAlign: 0,
                        },
                        bullet: {
                            nestingLevel: 0,
                            textStyle: {
                                fs: 20,
                            },
                            listType: 'CHECK_LIST',
                            listId: 'xIECkc',
                        },
                    },
                ],
                sectionBreaks: [
                    {
                        sectionId: 'section_fixture_1028',
                        startIndex: 10,
                    },
                ],
                customRanges: [],
                customDecorations: [],
            },
        };
        const cellData = {
            p: richTextParams,
            v: null,
            f: null,
            si: null,
        };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
        expect(result?.p).toEqual(richTextParams);
        expect(result?.v).toBeFalsy();
    });

    it('edit number 123456789123456789', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '123456789123456789' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe('123456789123456789');
        expect(result?.t).toBe(CellValueType.FORCE_STRING);
    });

    it('edit number 1212121212121212.2345', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '1212121212121212.2345' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe('1212121212121212.2345');
        expect(result?.t).toBe(CellValueType.FORCE_STRING);
    });

    it('edit number 1212121212121212', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '1212121212121212' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe(1212121212121212);
        expect(result?.t).toBe(CellValueType.NUMBER);
    });

    it('edit number 1e3', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '1e3' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe(1000);
        expect(result?.t).toBe(CellValueType.NUMBER);
    });

    it('edit number 001', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '001' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe(1);
        expect(result?.t).toBe(CellValueType.NUMBER);
    });

    it('edit number +123.', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '+123.' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe(123);
        expect(result?.t).toBe(CellValueType.NUMBER);
    });

    it('edit content 5A', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '5A' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe('5A');
    });

    it('edit content 5 A', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '5 A' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe(0.2083333333333333);
        expect(result?.t).toBe(CellValueType.NUMBER);
    });

    it('edit content 1 23', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '1 23' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe('1 23');
    });

    it('edit content 2/3', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '2/3' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);
        expect(result?.v).toBe(excelDateSerial(new Date(Date.UTC(new Date().getUTCFullYear(), 1, 3))));
        expect(result?.t).toBe(CellValueType.NUMBER);
    });

    it('edit content 1000,', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '1000,' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe('1000,');
    });

    it('edit content 1000,1.00', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '1000,1.00' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe('1000,1.00');
    });

    it('edit content $1000', () => {
        const sheetInterceptorService = testBed.get(SheetInterceptorService);
        const cellData = { v: '$1000' };
        const location = {
            workbook,
            worksheet,
            unitId,
            subUnitId,
            row: 0,
            col: 0,
            origin: cellData,
        };

        const result = sheetInterceptorService.writeCellInterceptor.fetchThroughInterceptors(AFTER_CELL_EDIT)(cellData, location);

        expect(result?.v).toBe(1000);
    });
});

describe('test get cell text/plain', () => {
    let univer: Univer;
    let get: Injector['get'];
    let commandService: ICommandService;

    beforeEach(async () => {
        const testBed = createTestBed({
            id: 'test',
            appVersion: '3.0.0-alpha',
            sheets: {
                sheet1: {
                    id: 'sheet1',
                    cellData: {
                        0: {
                            0: {
                                v: 44876,
                                t: 2,
                            },
                            1: {
                                p: {
                                    id: '__INTERNAL_EDITOR__DOCS_NORMAL',
                                    documentStyle: {
                                        pageSize: {
                                            width: 72.90791320800781,
                                        },
                                        marginTop: 0,
                                        marginBottom: 2,
                                        marginRight: 2,
                                        marginLeft: 2,
                                        renderConfig: {
                                            verticalAlign: 0,
                                            horizontalAlign: 0,
                                            wrapStrategy: 0,
                                            background: {},
                                            centerAngle: 0,
                                            vertexAngle: 0,
                                            zeroWidthParagraphBreak: 1,
                                        },
                                    },
                                    body: {
                                        dataStream: 'univer\r\n',
                                        textRuns: [
                                            {
                                                st: 0,
                                                ed: 1,
                                                ts: {
                                                    ff: 'Arial',
                                                    fs: 11,
                                                },
                                            },
                                            {
                                                st: 1,
                                                ed: 4,
                                                ts: {
                                                    ff: 'Arial',
                                                    fs: 28,
                                                },
                                            },
                                            {
                                                st: 4,
                                                ed: 6,
                                                ts: {
                                                    ff: 'Arial',
                                                    fs: 11,
                                                },
                                            },
                                        ],
                                        paragraphs: [
                                            {
                                                startIndex: 6,
                                                paragraphId: 'para_sheets_numfmt_rich_text',
                                                paragraphStyle: {
                                                    horizontalAlign: 0,
                                                },
                                            },
                                        ],
                                        customRanges: [],
                                        customDecorations: [],
                                        customBlocks: [],
                                    },
                                    drawings: {},
                                    drawingsOrder: [],
                                    settings: {
                                        zoomRatio: 1,
                                    },
                                },
                            },
                        },
                    },
                },
            },
            locale: LocaleType.ZH_CN,
            name: '',
            sheetOrder: [],
            styles: {},
        }, [
            [NumfmtEditorController],
            [SheetsNumfmtCellContentController],
            [IEditorBridgeService, { useClass: MockEditorBridgeService }],
        ]);

        univer = testBed.univer;
        get = testBed.get;
        commandService = get(ICommandService);
        testBed.get(NumfmtEditorController);
        testBed.get(SheetsNumfmtCellContentController);
        testBed.get(SheetInterceptorService);
    });

    it('when the cell has a numfmt should use both to get displayV', () => {
        const univerInstanceService = get(IUniverInstanceService);

        const workbook = univerInstanceService.getUnit<Workbook>('test');
        const worksheet = workbook?.getActiveSheet();
        if (worksheet) {
            const unitId = 'test';
            const subUnitId = worksheet.getSheetId();
            const params: ISetNumfmtMutationParams = {
                unitId,
                subUnitId,
                values: {
                    1: {
                        ranges: [{ startRow: 0, endRow: 0, startColumn: 0, endColumn: 0 }],
                    },
                },
                refMap: {
                    1: {
                        pattern: 'yyyy-mm-dd',
                    },
                },
            };
            commandService.syncExecuteCommand(SetNumfmtMutation.id, params);
            const matrix = worksheet.getMatrixWithMergedCells(0, 0, 0, 1, CellModeEnum.Both);
            const plain = getMatrixPlainText(matrix);
            expect(plain).toBe('2022-11-11\tuniver');
        }
    });
});