Posted 26 October 2023, 5:41 pm EST
- Updated 26 October 2023, 5:46 pm EST
Hi Pooja,
Thanks for sharing the code snippet with us. As per my understanding, you don’t want to delete the values for some cells when the delete key is pressed based on some condition. To achieve this functionality, you need to make the following changes in the code:
- Disable the default Delete Key command. For this, you could pass “undefined” first parameter to the setShortCutKey() function. Refer to the following code snippet:
// Disable the default key command
spread
.commandManager()
.setShortcutKey(
undefined,
GC.Spread.Commands.Key.del,
false,
false,
false,
false
);
- Register a custom command with undo functionality that takes the parameters like “sheetName” and “selection” and then conditionally sets the value on the Cell. Please note that you obtain the “selection” parameter from “options” and not directly access inside the “execute” function of the command as this will break the undo functionality.
Also, kindly set the “canUndo” option of the command to true. Kindly refer to the following code snippet:
// Define the Custom Command
var command = {
canUndo: true,
execute: function (context, options, isUndo) {
var Commands = GC.Spread.Sheets.Commands;
options.cmd = "customDeleteCommand";
if (isUndo) {
Commands.undoTransaction(context, options);
return true;
} else {
Commands.startTransaction(context, options);
// Get the SheetName From options
let sheet = context.getSheetFromName(options.sheetName);
// Get the Selection from Options
let selection = options.selection;
for (let i = selection.row; i < selection.row + selection.rowCount; i++) {
for (let j = selection.col; j < selection.col + selection.colCount; j++) {
// Check for Custom Condition
if (j !== 6) {
sheet.setValue(i, j, null);
}
}
}
Commands.endTransaction(context, options);
return true;
}
}
};
// Get the Command Manager's instance
var commandManager = spread.commandManager();
// Register the Custom Delete Command
commandManager.register("customDeleteCommand", command);
- Register a command for the delete key and pass the parameters to the custom command created in step no.2. Refer to the following code snippet:
spread.commandManager().register('myCmdDel',
{
canUndo: false,
execute: (context, options) => {
let activeSheet = context.getActiveSheet();
let selection = activeSheet.getSelections()[0];
// Pass the sheetName and Selection as command options
spread.commandManager().execute({
cmd: "customDeleteCommand", sheetName: activeSheet.name()
, selection: selection
});
}
}, GC.Spread.Commands.Key.del, false, false, false, false);
Sample: https://jscodemine.grapecity.com/share/Fhmd3SdeoUCbCEyIwsZSRA/?IsEmbed=false&Theme=Unset&PreviewDirection=0&IsEditorShow=true&IsExplorerShow=true&IsPreviewShow=true&IsConsoleShow=true&IsRunBTNShow=false&IsResetBTNShow=false&IsOpenInCodemineBTNShow=false&PanelWidth=20&PanelWidth=50&PanelWidth=30&defaultOpen={"OpenedFileName"%3A["%2Fsrc%2Fapp.js"]%2C"ActiveFile"%3A"%2Fsrc%2Fapp.js"}
In the above sample, you cannot delete the contents of the column “G” using the delete key. Further, if you delete any other cell contents, you will be able to undo/redo the action.
If the issue still persists for you, kindly share us a minimal working sample along with your exact use case. This will help us in understanding your issue and to resolve it quickly.
You could also refer to the following demo that shows how you could achieve the undo/redo functionality for a custom command: https://www.grapecity.com/spreadjs/demos/features/worksheet/actions/custom-action/purejs
References:
register method: https://www.grapecity.com/spreadjs/api/v15/classes/GC.Spread.Commands.CommandManager#register
setShortcutKey method: https://www.grapecity.com/spreadjs/api/v15/classes/GC.Spread.Commands.CommandManager#setshortcutkey
Regards,
Ankit