Extended Context Menu

The SpreadJS built-in context menu supports adding/removing custom menu options. Each option follows the defined menu data in the JSON schema.

Description
app.vue
index.html
Copy to CodeMine

You can define the menuData property as shown here:

    var openDialog = {
        text: 'Open Dialog',
        name: 'openDialog',
        command: showLoginDialog,
        workArea: 'viewport'
    };
    spread.contextMenu.menuData.push(openDialog);
    function showLoginDialog() {
        //pop up login dialog
    }

You can add/remove the custom menu options by using the spread.contextMenu.menuData property:

    $.each(spread.contextMenu.menuData, function (p, v) {
        if (v.name === 'openDialog') { //openDialog is a command's name
            spread.contextMenu.menuData.splice(p, 1)
        }
    });
You can define the menuData property as shown here: You can add/remove the custom menu options by using the spread.contextMenu.menuData property:
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> <gc-worksheet> </gc-worksheet> </gc-spread-sheets> <div class="options-container"> <div class="options-row"> <p>Right click any cell to bring up its context menu. You can then add or remove one of the demo options below: </p> </div> <div class="options-row"> <input type="checkbox" id="addFormatCell" v-model="addFormatCell"> <label for="addFormatCell">Add "Format Cells" menu item</label> </div> <div class="options-row"> <input type="checkbox" id="addOpenDialog" v-model="addOpenDialog"> <label for="addOpenDialog">Add "Open Dialog" menu item</label> </div> </div> </div> </template> <script setup> import { ref, watch } from "vue"; import "@mescius/spread-sheets-vue"; import GC from '@mescius/spread-sheets'; const spreadRef = ref(null); const addFormatCell = ref(false); const addOpenDialog = ref(false); watch(addFormatCell, (value) => { let spread = spreadRef.value; let commandRemoved = false; spread.contextMenu.menuData.forEach(function (item, index) { if (item && item.name === "markWithRedBg") { spread.contextMenu.menuData.splice(index, 1); commandRemoved = true; } }); if (commandRemoved) { return; } let commandManager = spread.commandManager(); let markWithRedBg = { text: "Format Cells", name: "markWithRedBg", command: "markWithRedBg", workArea: "viewport" }; spread.contextMenu.menuData.push(markWithRedBg); let markWithRedBgCommand = { canUndo: false, execute: function () { let style = new GC.Spread.Sheets.Style(); style.name = 'style1'; style.backColor = 'red'; let sheet = spread.getActiveSheet(); sheet.suspendPaint(); let selections = sheet.getSelections(); let selectionIndex = 0, selectionCount = selections.length; for (; selectionIndex < selectionCount; selectionIndex++) { let selection = selections[selectionIndex]; for (let i = selection.row; i < (selection.row + selection.rowCount); i++) { for (let j = selection.col; j < (selection.col + selection.colCount); j++) { sheet.setStyle(i, j, style, GC.Spread.Sheets.SheetArea.viewport); } } } sheet.resumePaint(); } }; commandManager.register("markWithRedBg", markWithRedBgCommand, null, false, false, false, false); }); watch(addOpenDialog, (value) => { let spread = spreadRef.value; let commandRemoved = false; spread.contextMenu.menuData.forEach(function (item, index) { if (item && item.name === "openDialog") { spread.contextMenu.menuData.splice(index, 1); commandRemoved = true; } }); if (commandRemoved) { return; } let openDialog = { text: "Open Dialog", name: "openDialog", command: showLoginDialog, workArea: "viewport" }; spread.contextMenu.menuData.push(openDialog); }); let initSpread = function (spread) { spreadRef.value = spread; } function showLoginDialog() { if (document.getElementsByClassName('loginBox').length === 0) { var loginBox = document.createElement('div'); loginBox.className = 'loginBox'; loginBox.innerHTML = '<label for="Dialog" class="loginBoxLabel">Dialog:</label>' + '<input type="button" id="okBtn" class="btn-primary button" value="OK">' + '<input type="button" id="cancelBtn" class="btn-primary button" value="Cancel">'; document.body.appendChild(loginBox); document.getElementById('okBtn').onclick = function () { loginBox.style.display = 'none'; }; document.getElementById('cancelBtn').onclick = function () { loginBox.style.display = 'none'; }; } var loginBoxEle = document.getElementsByClassName('loginBox')[0]; if (loginBoxEle.style.display === 'none') { loginBoxEle.style.display = 'block'; } } </script> <style scoped> #app { height: 100%; } .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-spreadsheets { width: calc(100% - 280px); height: 100%; overflow: hidden; float: left; } .options-container { float: right; width: 280px; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .options-row { font-size: 14px; padding: 5px; margin-top: 10px; } .button { width: 30%; margin: 60px 10%; text-align: center; } input { display: inline-block; } input[type="text"] { width: 200px; } input[type="button"] { margin-top: 6px; } label { margin-bottom: 6px; } p { padding: 2px 10px; background-color: #F4F8EB; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .desc { padding: 2px 10px; background-color: #F4F8EB; } .loginBox { position: absolute; left: 35%; top: 30%; background-color: white; padding: 20px; border: 1px solid #c6c6c6; box-shadow: rgba(0, 0, 0, 0.4) 1px 2px 5px; z-index: 2000; height: 100px; width: 200px; } .loginBoxLabel { display: inline-block; width: 200px; text-align: center; } </style>
<!DOCTYPE html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>SpreadJS VUE</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/vue3/node_modules/systemjs/dist/system.src.js"></script> <script src="./systemjs.config.js"></script> <script src="./compiler.js" type="module"></script> <script src="$DEMOROOT$/spread/source/data/sorting.js" type="text/javascript"></script> <script> var System = SystemJS; System.import("./src/app.js"); System.import('$DEMOROOT$/en/lib/vue3/license.js'); </script> </head> <body> <div id="app"></div> </body> </html>
(function (global) { SystemJS.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, packageConfigPaths: [ './node_modules/*/package.json', "./node_modules/@mescius/*/package.json", "./node_modules/@babel/*/package.json", "./node_modules/@vue/*/package.json" ], map: { 'vue': "npm:vue/dist/vue.esm-browser.js", 'tiny-emitter': 'npm:tiny-emitter/index.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', "systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js", '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-en': 'npm:@mescius/spread-sheets-resources-en/index.js', '@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js' }, meta: { '*.css': { loader: 'systemjs-plugin-css' }, '*.vue': { loader: "../plugin-vue/index.js" } } }); })(this);