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
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?
Thanks
Posted 2 February 2021, 7:11 pm EST
Hi,
Apologies for the delayed response. For this, you need to disable the cut, Copy paste from the Global keys and also from the CellType. Please refer to the following code snippet and attached sample that demonstrates the same.
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>
sample: https://codesandbox.io/s/icy-framework-tf7vw?file=/src/index.js
Regards
Avinash
Posted 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.
Posted 14 February 2021, 11:46 pm EST
Hi,
As a better alternative, you could just remove shortcut bindings for copy and paste command. Please refer to the following code snippet and the sample demonstrating the same:
const commandManger = spread.commandManager();
// disable copy
commandManger.setShortcutKey(null, 67, true, false, false, false);
// disable cut
commandManger.setShortcutKey(null, 88, true, false, false, false);
https://stackblitz.com/edit/react-cfprvb?file=src%2FApp.js
Regards
Sharad
Posted 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.
Posted 16 February 2021, 9:52 pm EST
Hi,
Please try by registering an empty command as demonstrated in the following sample and let me know if the issue persists for you:
https://stackblitz.com/edit/react-hyuezv?file=src%2FApp.js
Posted 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.
Posted 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
);
sample: https://stackblitz.com/edit/react-efjjww?file=src/App.js
API reference:
https://www.grapecity.com/spreadjs/docs/v14/online/SpreadJS~GC.Spread.Commands.CommandManager~setShortcutKey.html
Posted 22 February 2021, 12:02 pm EST
Thank Sharad, I got it to work.
Is it possible to prevent the user from copying a value in spreadjs and pasting that value on a different website without disabling copy or cut?
The user should be able to use the copy and cut shortcut keys, but they cannot paste the value on a different website.
Posted 22 February 2021, 7:31 pm EST
Hi,
For this, you may set the contextMenu cut, copy, paste commands to the shortcut keys. Please refer to the following updated sample that demonstrates the same.
sample: https://stackblitz.com/edit/react-ghql5m?file=src/App.js
Regards
Avinash
Posted 23 February 2021, 4:16 pm EST
Hi,
This is the expected behavior from SJS. Inside the Edit mode, actions like copy, paste, cut, ctrl+a are not handled by SJS instead of that global events are performed. For handling this case you override the isReservedKey method. Please refer to the following code snippet and attached updated sample that demonstrates the same.
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);
}
};
sample: https://stackblitz.com/edit/react-1174ks?file=src/App.js
Regards
Avinash
Posted 25 February 2021, 3:18 am EST
Avinash, thank you so much for the detailed explanation, you save the day!
I have been trying to get the same thing to work.
I am glad my team chose spreadjs over other library. Experts from the support team like Avinash really make a huge difference.