本帖最后由 DerrickJiao 于 2021-2-5 15:19 编辑
在实际应用中,出于对网页数据的保护或为了避免外部数据的污染,有的小伙伴有这样的需求:在表单内可以进行复制粘贴,将外部数据的复制粘贴全部禁止,让SpreadJS只能在表单之间复制粘贴。
这篇文章教大家如何通过重写Ctrl C和Ctrl V实现上述的功能。有细心的朋友已经知道了这一功能在原生的SpreadJS的右键菜单的复制粘贴中已经实现。那么我们要做的就是将右键菜单的命令与Ctrl C绑定。SpreadJS提供了copy和paste命令。
重写复制快捷键
- //重写复制快捷键
- var command = {
- canUndo: false,
- execute: function (context, options, isUndo) {
- var Commands = GC.Spread.Sheets.Commands;
- if (isUndo) {
- Commands.undoTransaction(context, options);
- return true;
- } else {
- Commands.startTransaction(context, options);
- let spread = context;
- let sheetName = options.sheetName;
- let command = {};
- command.cmd = "copy";
- command.sheetName = sheetName;
- command.ignoreClipboard = true;
- command.callback = function () {
- options.callback(command.copyData.copyText, command.copyData.copyHtml);
- };
- spread.commandManager().execute(command);
-
- Commands.endTransaction(context, options);
- return true;
- }
- }
- };
- spread.commandManager().register("myCopy", command);
- spread.commandManager().setShortcutKey("myCopy", GC.Spread.Commands.Key.c, true, false, false, false);
复制代码
重写粘贴快捷键
- //重写粘贴
- var commandV = {
- canUndo: false,
- execute: function (context, options, isUndo) {
- var Commands = GC.Spread.Sheets.Commands;
- if (isUndo) {
- Commands.undoTransaction(context, options);
- return true;
- } else {
- Commands.startTransaction(context, options);
- let spread = context;
- let sheetName = options.sheetName;
- let command = {};
- command.cmd = "paste";
- command.sheetName = sheetName;
-
- spread.commandManager().execute(command);
-
- Commands.endTransaction(context, options);
- return true;
- }
- }
- };
- spread.commandManager().register("myPaste", commandV);
- spread.commandManager().setShortcutKey("myPaste", GC.Spread.Commands.Key.v, true, false, false, false);
复制代码
下载附件即可查看完整的的demo
|
|