Replicate Excel Ctrl+Enter behavior

Posted by: frederik.beaudry on 13 July 2022, 10:50 am EST

    • Post Options:
    • Link

    Posted 13 July 2022, 10:50 am EST

    In Excel, when you select a range, type a value and then Ctrl+Enter, it copies the value in every cell of the selected range.

    In SpreadJs, it acts like Alt+Enter and inserts a new line in cell being edited.

    How can I replicate this Excel feature?

  • Posted 13 July 2022, 10:52 am EST

    I tried the following code, but it doesn’t work when a cell is being edited

    
    window.workbook.commandManager().register("excelCtrlEnter", () => {
        console.log("test");
    }, GC.Spread.Commands.Key.enter, true, false, false, false)
    
    
  • Posted 14 July 2022, 9:30 am EST

    Work in progress, but this is what I have so far

    
    onEditorStatusChanged: function (event, args) {
            let copyCellValues = (selections, activeRowIndex, activeColumnIndex) => {
                let activeCellValue = workbook.getActiveSheet().getCell(activeRowIndex, activeColumnIndex).value();
    
                workbook.suspendPaint();
                workbook.suspendEvent();
    
                selections.forEach((selection) => {
                    let valuesArray = Array.from({ length: selection.rowCount }).fill(Array.from({ length: selection.colCount }).fill(activeCellValue));
    
                    console.log(selection.row, selection.col, valuesArray);
    
                    workbook.getActiveSheet().setArray(selection.row, selection.col, valuesArray, false);
                });
    
                workbook.resumeEvent();
                workbook.resumePaint();
            };
    
            if (args.newStatus == GC.Spread.Sheets.EditorStatus.enter || GC.Spread.Sheets.EditorStatus.edit) {
                let editor = workbook.getHost().querySelector('[gcuielement="gcEditingInput"]');
    
                if (editor) {
                    let selections = workbook.getActiveSheet().getSelections();
                    let activeRowIndex = workbook.getActiveSheet().getActiveRowIndex();
                    let activeColumnIndex = workbook.getActiveSheet().getActiveColumnIndex();
    
                    editor.addEventListener('keydown', function (e) {
                        if (e.keyCode == 13 && e.ctrlKey) {
                            e.preventDefault();
    
                            var kb = new KeyboardEvent('keydown', {
                                ctrlKey: false,
                                keyCode: 13
                            });
                            editor.dispatchEvent(kb);
    
                            copyCellValues(selections, activeRowIndex, activeColumnIndex);
                        }
                    }, true);
                }
            }
        }
    
    
  • Posted 15 July 2022, 9:30 am EST - Updated 3 October 2022, 9:11 am EST

    Hi Frederik,

    You can create a custom command to replicate the Excel behavior and bind the custom command to shortcut key. Please refer to the following sample that I have created for you: https://jscodemine.grapecity.com/share/KtlSDHGTckKfJgHZcAC2iQ/

    API Docs:

    activateEditor method: https://www.grapecity.com/spreadjs/docs/latest/online/SpreadJS~GC.Spread.Sheets.CellTypes.Base~activateEditor.html

    isReservedKey method: https://www.grapecity.com/spreadjs/docs/latest/online/SpreadJS~GC.Spread.Sheets.CellTypes.Base~isReservedKey.html

    register method: https://www.grapecity.com/spreadjs/docs/latest/online/SpreadJS~GC.Spread.Commands.CommandManager~register.html

    setShortcutKey method: https://www.grapecity.com/spreadjs/docs/latest/online/SpreadJS~GC.Spread.Commands.CommandManager~setShortcutKey.html

    endEdit method: https://www.grapecity.com/spreadjs/docs/latest/online/SpreadJS~GC.Spread.Sheets.Worksheet~endEdit.html

    Please let us know if you need further assistance with this query. We would be happy to help you.

    Regards

    Ankit

  • Posted 15 July 2022, 1:02 pm EST

    Thanks for the reply. I was on the right track :slight_smile:

Need extra support?

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

Learn More

Forum Channels