Undo not working when using shortcut key

Posted by: ratmym on 19 March 2020, 11:25 am EST

    • Post Options:
    • Link

    Posted 19 March 2020, 11:25 am EST

    I’ve made some custom context menus. When I execute a command from context menus, undo works well. But, when I execute it by shortcut key, undo does not work.

    Here is my code snippet.

    var insertDateCommand = {		
    	canUndo: true,
    	execute: function (context, options, isUndo) {
    		var commands = GC.Spread.Sheets.Commands;
    		
    		if (isUndo) {
    			commands.undoTransaction(context, options);
    			return true;
    		} else {
    			commands.startTransaction(context, options);
    			setDateToSelectedCells(context);
    			commands.endTransaction(context, options);
    			return true;
    		}
    	}
    };
    // alt + 1
    commandManager.register("insertDate", insertDateCommand, 49, false, false, true, false);
    

    Thank you in advance.

  • Posted 19 March 2020, 3:20 pm EST

    
    	var insertDateCommand = {		
    		canUndo: true,
    		execute: function (context, options, isUndo) {
    			var commands = GC.Spread.Sheets.Commands;
    			
    			if (isUndo) {
    				commands.undoTransaction(context, options);
    				return true;
    			} else {
    				commands.startTransaction(context, options);
    				setDateToSelectedCells(context);
    				commands.endTransaction(context, options);
    				return true;
    			}
    		}
    	};
    	// alt + 1
    	commandManager.register("insertDate", insertDateCommand, 49, false, false, true, false);	
    	//.
    	
    	
    	function setDateToSelectedCells(context, dateObj, format) {
    		var sheet = context.getActiveSheet();
    		
    		
    		if (!sheet.options.isProtected) {
    			var date;
    			if (dateObj) {
    				date = dateObj;
    			} else {
    				date = new Date();
    			}
    				
    			var selections = sheet.getSelections();
    			var selectionIndex = 0, selectionCount = selections.length;
    			
    			sheet.suspendPaint();
    			for (; selectionIndex < selectionCount; selectionIndex++) {
    				var selection = selections[selectionIndex];
    				for (var i = selection.row; i < (selection.row + selection.rowCount); i++) {
    					for (var j = selection.col; j < (selection.col + selection.colCount); j++) {
    						
    						sheet.setValue(i, j, date);
    						sheet.setFormatter(i, j, format ? format : 'yyyy/MM/dd');
    					}
    				}
    			}
    			sheet.resumePaint();
    		}
    	}
    
    
  • Posted 19 March 2020, 11:45 pm EST

    Hi,

    We are able to replicate the issue at our end and hence we have forwarded it to the concerned team for further investigation of the issue. The internal tracking ID for the issue is SJS-3913. We will let you know about any updates regarding the issue.

    Regards

    Sharad

  • Posted 30 March 2020, 3:50 pm EST

    Hi,

    Dev team has informed us that to be able to have undo functionality for a custom command, we need to set the cmd option too. Please refer to the following code snippet and the sample demonstrating the same:

    var changeColorCommand = {
        canUndo: true,
        execute: function(context, options, isUndo) {
          var commands = GC.Spread.Sheets.Commands;
          if (isUndo) {
            commands.undoTransaction(context, options);
            return true;
          } else {
            [b]options.cmd = "changeColor";[/b]
            commands.startTransaction(context, options);
            // command
            commands.endTransaction(context, options);
            return true;
          }
        }
      };
    

    https://codesandbox.io/s/spread-js-starter-d27me

    Regards

    Sharad

  • Posted 30 March 2020, 4:00 pm EST

    Dear Sharad,

    It works well.

    Thank you for your support.

Need extra support?

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

Learn More

Forum Channels