SpreadJS自定义右键菜单-根据sheet显示新增菜单项
背景:客户需求,spread指定sheet才显示新增的菜单项,其他sheet不显示。
步骤:
1、创建命令
var newMenuItem = {
canUndo: true,
name: "newMenuItem",
execute: function (context, options, isUndo) {
var Commands = GC.Spread.Sheets.Commands;
if (isUndo) {
Commands.undoTransaction(context, options);
return true;
} else {
Commands.startTransaction(context, options);
var sheet = context.getSheetFromName(options.sheetName);
alert("添加的新右键菜单项");
Commands.endTransaction(context, options);
return true;
}
}
};2、注册命令
spread.commandManager().register("newMenuItem", newMenuItem);3、重写右键菜单
function MyContextMenu() { }
MyContextMenu.prototype = new GC.Spread.Sheets.ContextMenu.ContextMenu(spread);
MyContextMenu.prototype.onOpenMenu = function (menuData, itemsDataForShown, hitInfo, spread) {
var sheetIndex = spread.getActiveSheetIndex()
// 如果为sheet1,则添加新项
if (sheetIndex == 0) {
itemsDataForShown.push({
text: "新项",
name: "newItem",
command: "newMenuItem",
workArea: "viewportcolHeaderrowHeaderslicercorner"
});
}
};
var contextMenu = new MyContextMenu();
spread.contextMenu = contextMenu;测试结果如下图:
完整代码请参考附件demo。
页:
[1]