拡張コンテキストメニュー

SpreadJSの組み込みコンテキストメニューには、カスタムメニューオプションを追加または削除できます。個々のオプションは、JSONスキーマの定義済みメニューデータに準拠しています。

menuDataプロパティを、次のように定義できます。 カスタムメニューオプションを追加または削除するには、spread.contextMenu.menuDataプロパティを使用します。
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 2}); initSpread(spread); }; function initSpread(spread) { initEvents(spread); } function initEvents(spread) { document.getElementById('addFormatCells').onchange = function () { var 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; } var commandManager = spread.commandManager(); var markWithRedBg = { text: "Format Cells", name: "markWithRedBg", command: "markWithRedBg", workArea: "viewport" }; spread.contextMenu.menuData.push(markWithRedBg); var markWithRedBgCommand = { canUndo: false, execute: function () { var style = new GC.Spread.Sheets.Style(); style.name = 'style1'; style.backColor = 'red'; var sheet = spread.getActiveSheet(); sheet.suspendPaint(); var selections = sheet.getSelections(); var selectionIndex = 0, selectionCount = selections.length; for (; selectionIndex < selectionCount; selectionIndex++) { var selection = selections[selectionIndex]; for (var i = selection.row; i < (selection.row + selection.rowCount); i++) { for (var 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); }; document.getElementById('addOpenDialog').onchange = function () { var 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; } var openDialog = { text: "Open Dialog", name: "openDialog", command: showLoginDialog, workArea: "viewport" }; spread.contextMenu.menuData.push(openDialog); }; } 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'; } }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta name="spreadjs culture" content="ja-jp" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets-resources-ja/dist/gc.spread.sheets.resources.ja.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/js/license.js" type="text/javascript"></script> <script src="app.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="styles.css"> </head> <body> <div class="sample-tutorial"> <div id="ss" class="sample-spreadsheets"></div> <div class="options-container"> <p>任意のセルを右クリックすると、固有のコンテキストメニューが表示されます。このコンテキストメニューに対し、以下のいずれかのオプションを追加または削除できます。</p> <div id="settingsDiv"> <div class="option-row"> <input id="addFormatCells" type="checkbox" /> <label for="addFormatCells">[セルの書式設定]メニュー項目を追加する</label> </div> <div class="option-row"> <input id="addOpenDialog" type="checkbox" /> <label for="addOpenDialog">[ダイアログを開く]メニュー項目を追加する</label> </div> </div> </div> </div></body> </html>
.option-row { font-size: 14px; padding: 5px; margin-top: 10px; } .buttonStyle{ width:240px; height:30px; } .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; } .button{ width: 30%; margin: 60px 10%; text-align: center; } .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; } .option-row { font-size: 14px; margin-top: 10px; } label { margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; } p{ padding:2px 10px; background-color:#F4F8EB; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }