アクション

SpreadJSでは、多くのキーボードアクションがサポートされます。たとえば、左矢印キーの押下による左方向への移動、[Del]キーの押下による値のクリアなどです。キーアクションでは、キーマップを使用してキーとアクションとをリンクします。

<p>独自のキーアクションを追加するには、<strong>register</strong>メソッドを使用して、キーダウンイベントと任意のアクションとをリンクします。次に、この例を示します。</p> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> spread = GC.Spread.Sheets.findControl(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'ss'</span>)); <span class="hljs-keyword">var</span> commandManager = spread.commandManager(); commandManager.register(<span class="hljs-string">'customNavigationUp'</span>, GC.Spread.Sheets.Commands.navigationUp, <span class="hljs-string">'W'</span>.charCodeAt(<span class="hljs-number">0</span>), <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>); commandManager.register(<span class="hljs-string">'customNavigationLeft'</span>, GC.Spread.Sheets.Commands.navigationLeft, <span class="hljs-string">'A'</span>.charCodeAt(<span class="hljs-number">0</span>), <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>); commandManager.register(<span class="hljs-string">'customNavigationDown'</span>, GC.Spread.Sheets.Commands.navigationDown, <span class="hljs-string">'S'</span>.charCodeAt(<span class="hljs-number">0</span>), <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>) commandManager.register(<span class="hljs-string">'customNavigationRight'</span>, GC.Spread.Sheets.Commands.navigationRight, <span class="hljs-string">'D'</span>.charCodeAt(<span class="hljs-number">0</span>), <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>, <span class="hljs-literal">false</span>); </code></pre> <p>これで、[W]、[A]、[S]、[D]キーを押すと、上下左右の矢印キーと同様に各方向に移動できます。</p>
window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'), { sheetCount: 1 }); initSpread(spread); }; function initSpread(spread) { var sd = dataSource; var sheet = spread.getActiveSheet(); if (sd.length > 0) { sheet.setDataSource(sd); } sheet.setColumnWidth(0, 160); sheet.setColumnWidth(1, 70); sheet.setColumnWidth(2, 90); sheet.setColumnWidth(3, 110); sheet.setColumnWidth(4, 80); sheet.setColumnWidth(6, 110); document.getElementById('addCToKeyMap').addEventListener('click', function() { var spreadNS = GC.Spread.Sheets.Commands; var commandManager = spread.commandManager(); commandManager.register( 'customNavigationUp', spreadNS.navigationUp, 'W'.charCodeAt(0), false, false, false, false ); commandManager.register( 'customNavigationLeft', spreadNS.navigationLeft, 'A'.charCodeAt(0), false, false, false, false ); commandManager.register( 'customNavigationDown', spreadNS.navigationDown, 'S'.charCodeAt(0), false, false, false, false ); commandManager.register( 'customNavigationRight', spreadNS.navigationRight, 'D'.charCodeAt(0), false, false, false, false ); }); }
<!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/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/data/data.js" type="text/javascript"></script> <script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/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"> <div class="option-row"> <input type="button" id="addCToKeyMap" value="キーボードナビゲーションを設定" /> </div> <div class="option-row"> <label>Key ‘W’ = 上に移動</label> <label>Key ‘A’ = 左に移動</label> <label>Key ‘S’ = 下に移動</label> <label>Key ‘D’ = 右に移動</label> </div> </div> </div> </body> </html>
.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; padding: 5px; margin-top: 10px; } label { display: block; margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; display: block; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }