您好,需要遍历菜单栏中所有的节点,如下代码进行递归遍历:
- let defaultConfig = GC.Spread.Sheets.Designer.DefaultConfig
- let ribbonNode = defaultConfig.ribbon
- let commands = []
- findNode(ribbonNode)
- function findNode(node) {
- if (node instanceof Array) {
- for (let i = 0; i < node.length; i++) {
- findNode(node[i])
- }
- } else if (node.buttonGroups) {
- findNode(node.buttonGroups)
- } else if (node.commandGroup) {
- findNode(node.commandGroup)
- } else if (node.children) {
- if (node.command) {
- commands.push(node.command)
- }
- findNode(node.children)
- } else if (node.commands) {
- commands.push(...node.commands)
- } else {
- if (!node.type) {
- commands.push(node)
- }
- }
- }
复制代码 获取到菜单栏中所有的commandName后,获取指令,修改enableContext。注意有的命令enableContext为null ,所以需要做个判断,如下代码:
- let commandMap = {}
- commands.forEach(commandName => {
- let command = GC.Spread.Sheets.Designer.getCommand(commandName)
- if (command) {
- command.enableContext = command.enableContext == null ? "!controlByMyself" : command.enableContext + "&& !controlByMyself";
- commandMap[commandName] = command
- }
- })
复制代码 在上述代码中,在enableContext中增加了变量controlByMyself ,可以通过controlByMyself控制是否全部disabled.
之后重新设置config
- defaultConfig.commandMap = commandMap
- designer.setConfig(defaultConfig)
复制代码
最后我们增加两个按钮,用来控制开关
- <button id="btn1">菜单禁止点击</button>
- <button id="btn2">菜单恢复点击</button>
复制代码- document.getElementById('btn1').addEventListener('click', function () {
- designer.setData("controlByMyself", true)
- })
- document.getElementById('btn2').addEventListener('click', function () {
- designer.setData("controlByMyself", false)
- })
复制代码 完整代码参考附件:
|
|