No data loss when clicking another cell

Posted by: thanh.minh.le on 2 March 2023, 5:07 pm EST

    • Post Options:
    • Link

    Posted 2 March 2023, 5:07 pm EST

    I thought I would describe my case thoroughly because the title wouldn’t explain enough.

    Currently, we block users from entering text into a cell with a default formula like this:

    GC.Spread.Sheets.CellTypes.Text.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
          const { sheet, row, col } = context
          speadJsHelper.registerSingleEventListener(editorContext, htmlDomEventNames.keydown, (event) => {
            if (!event.ctrlKey && //for allowing ctr action such as paste
            !spreadJSHelper.isValidKeyCodeForNumericInput(event, event.key, separator) || // for validate key press
            ( && event.key == separator) || // for one separator validation
            ( && event.key == keyCodesString.MINUS_SIGN) || // for one minus-sign validation
            !sapTestingSheetSpeadJsHelper.isValidDecimalPatternFormatAtKeyPress(event, separator, lastedKeyPressed) // custom validate follow format decimal(18,2)
            ) {

    It works and prevents the user enter text into the cell.

    The problem is that when the user cannot enter text in that cell and they click on another cell, the default formula in the cell is lost. I think this is the design of SpreadJS like Excel.

    May I ask is there a way for the user to click on another cell and still keep the formula? Like pressing the ESC key



  • Posted 5 March 2023, 7:36 pm EST

    Hello Thanh,

    As I can understand, you are allowing the users to enter the text in the cell but if they enter invalid text, then you want to retain the default formula of the cell. For this use case, you can execute the cancelInput command which is used to exit from the edit mode of the cell and restores the previous value of the cell. You can execute this command with execute method of the command manager.

    Please note that if you do not want the user to edit a cell at all, you may set the cell to locked cell.

    Please refer to the code snippet and attached sample

    let oldActivateEditor = GC.Spread.Sheets.CellTypes.Text.prototype.activateEditor;
    GC.Spread.Sheets.CellTypes.Text.prototype.activateEditor = function (editorContext, cellStyle, cellRect, context) {
        oldActivateEditor.apply(this, arguments);
        const { sheet, row, col } = context;
        editorContext.addEventListener('keydown', (e) => {
            // here, you can validate the user input
            if (row === 1 && col === 1 && e.key === 'x') {
                    cmd: 'cancelInput', 


    Please let us know if you still face any issue or you have a different use case.

    Doc references:

    cancelInput command:


    Text class:



Need extra support?

Upgrade your support plan and get personal unlimited phone support with our customer engagement team

Learn More

Forum Channels