Posted 29 January 2021, 8:44 am EST
How to prevent a user from copying a value on spreadjs and pasting outside of spreadjs?Thanks
Forums Home / Spread / SpreadJS Topics
Posted by: roywebweb123 on 29 January 2021, 8:44 am EST
Posted 29 January 2021, 8:44 am EST
How to prevent a user from copying a value on spreadjs and pasting outside of spreadjs?Replied 2 February 2021, 7:11 pm EST
Hi,
GC.Spread.Sheets.CellTypes.Text.prototype.isReservedKey = function (
e,
context
) {
//cell type handle ctrl c,v,x key by itself(doesnt call the copy paste cut actions)
return (
(e.keyCode === 67 || e.keyCode === 88 || e.keyCode === 86) &&
e.ctrlKey &&
!e.shiftKey &&
!e.altKey
);
};
//html
<div
oncopy="return false"
oncut="return false"
onpaste="return false"
id="ss"
></div>
Replied 13 February 2021, 8:47 am EST
Hi Avinash, thanks for the response. I try to convert your solution into React.js, but I unable to get it to work.Replied 14 February 2021, 11:46 pm EST
Hi,const commandManger = spread.commandManager();
// disable copy
commandManger.setShortcutKey(null, 67, true, false, false, false);
// disable cut
commandManger.setShortcutKey(null, 88, true, false, false, false);
Replied 16 February 2021, 1:31 pm EST
Hi Sharad, thanks for the response. I try your code snippet and sample on stackblitz.com, but it not working. I can still copy and cut.Replied 16 February 2021, 9:52 pm EST
Hi,Replied 17 February 2021, 11:59 am EST
It still not working for me. I think it because I using Mac. I try it on a window pc and it work.Replied 17 February 2021, 4:37 pm EST
For Mac, you need to pass the meta parameter as true in setShortcutKey. Please refer to the following code snippet and attached updated sample that demonstrates the same.
// disable copy
commandManger.setShortcutKey("emptyCommand", 67, false, false, false, true);
// disable cut
commandManger.setShortcutKey(
"emptyCommand2",
88,
false,
false,
false,
true
);
Replied 22 February 2021, 12:02 pm EST
Thank Sharad, I got it to work.Replied 22 February 2021, 7:31 pm EST
Hi,Replied 23 February 2021, 12:26 pm EST
Thanks AvinashReplied 23 February 2021, 4:16 pm EST
Hi,
let old = GC.Spread.Sheets.CellTypes.Text.prototype.isReservedKey;
GC.Spread.Sheets.CellTypes.Text.prototype.isReservedKey = function(e, context) {
//cell type handle ctrl+c,v,x key by itself
if (context.isEditing) {
return (
(e.keyCode === GC.Spread.Commands.Key.c ||
e.keyCode === GC.Spread.Commands.Key.v ||
e.keyCode === GC.Spread.Commands.Key.x) &&
e.metakey &&
(!e.shiftKey && !e.altKey)
);
} else {
old.apply(this, arguments);
}
};
Marked as Answer
Replied 25 February 2021, 3:18 am EST
Avinash, thank you so much for the detailed explanation, you save the day!