Selection

This sample shows how you can customize the selection behavior in the workbook.

<p>You can select cells, rows, columns, or multiple ranges in SpreadJS.</p> <p>Click one cell and drag the mouse to select a range. Then you will see the range selection.</p> <p>You can change the selection border color and background color by using the <strong>selectionBorderColor</strong> and <strong>selectionBackColor</strong> options, as shown in the following code:</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> sheet = spread.getActiveSheet(); sheet.options.selectionBorderColor = <span class="hljs-string">'red'</span>; sheet.options.selectionBackColor = <span class="hljs-string">'transparent'</span>; </code></pre> <p>You can set which items a user can select by using the <strong>selectionPolicy</strong> and <strong>selectionUnit</strong> options. The <strong>SelectionPolicy</strong> provides the following types:</p> <ul> <li><strong>single: Allows you to only select single items.</strong></li> <li><strong>range: Allows you to select single items and ranges of items, but not multiple ranges.</strong></li> <li><strong>mutliRange: Allows you to select single items and ranges of items, including multiple ranges.</strong></li> </ul> <p>The <strong>SelectionUnit</strong> enumeration contains the following types:</p> <ul> <li><strong>cell: Indicates that the smallest unit that can be selected is a cell.</strong></li> <li><strong>row: Indicates that the smallest unit that can be selected is a row.</strong></li> <li><strong>column: Indicates that the smallest unit that can be selected is a column.</strong></li> </ul> <p>You can use these two methods to control the select mode.</p> <p>Press the Ctrl key and select some ranges; you will select multiple ranges. Also you can use the <strong>addSelection</strong> method to add more selections, and then use the <strong>getSelections</strong> method to get all the selected ranges. Use the <strong>clearSelection</strong> method to clear the selections. These methods are used in the following code:</p> <pre><code class="hljs js language-js"> sheet.addSelection(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>); sheet.addSelection(<span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>); sheet.clearSelection(); <span class="hljs-keyword">var</span> selections = sheet.getSelections(); <span class="hljs-comment">// after clearSelection the selections.length is 0.</span> </code></pre> <p>The workbook <strong>allowUserDeselect</strong> option allow you to control whether enable to deselect current selection by mouse</p> <p>Besides using the mouse to select, you can use the <strong>setSelection</strong> method to select some cells and use the <strong>setActiveCell</strong> method to select one cell. The active cell is the first cell in the selection. Use the <strong>getActiveRowIndex</strong> and <strong>getActiveColumnIndex</strong> methods to get the active cell row and column indexes, as shown in the following code:</p> <pre><code class="hljs js language-js"> sheet.setSelection(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-number">3</span>, <span class="hljs-number">3</span>); sheet.setActiveCell(<span class="hljs-number">20</span>, <span class="hljs-number">3</span>, <span class="hljs-number">2</span>, <span class="hljs-number">2</span>); <span class="hljs-keyword">var</span> activeRowIndex = sheet.getActiveRowIndex(); <span class="hljs-keyword">var</span> activeColumnIndex = sheet.getActiveColumnIndex(); </code></pre> <p>After you set the active cell, if the active cell is not visible, you can use the <strong>showCell</strong>, <strong>showRow</strong>, and <strong>showColumn</strong> methods to make the active cell visible.</p> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> verticalPosition = GC.Spread.Sheets.VerticalPosition.top; <span class="hljs-keyword">var</span> horizontalPosition = GC.Spread.Sheets.HorizontalPosition.left; sheet.showCell(<span class="hljs-number">20</span>, <span class="hljs-number">3</span>, verticalPosition, horizontalPosition); <span class="hljs-comment">// up code and down code work out the same result.</span> sheet.showColumn(<span class="hljs-number">3</span>, horizontalPosition); sheet.showRow(<span class="hljs-number">20</span>, verticalPosition); </code></pre>
window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'), { sheetCount: 1 }); spread.suspendPaint(); initSpread(spread); spread.resumePaint(); }; 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); _getElementById('selectionPolicy').addEventListener('change', function() { var sheet = spread.getActiveSheet(); var policy = parseInt(this.value, 10); sheet.selectionPolicy(policy); }); _getElementById('selectionUnit').addEventListener('change', function() { var sheet = spread.getActiveSheet(); var unit = parseInt(this.value, 10); sheet.selectionUnit(unit); }); _getElementById('setBackColor').addEventListener('click', function() { var sheet = spread.getActiveSheet(); var backColor = _getElementById('backColor').value; sheet.options.selectionBackColor = backColor; }); _getElementById('setBorderColor').addEventListener('click', function() { var sheet = spread.getActiveSheet(); var borderColor = _getElementById('borderColor').value; sheet.options.selectionBorderColor = borderColor; }); _getElementById('tabStoptrue_Btn').addEventListener('click', function() { setIsTabStop(spread, true); }); _getElementById('tabStopfalse_Btn').addEventListener('click', function() { setIsTabStop(spread, false); }); _getElementById('chk_allowDeselect').addEventListener('click', function() { spread.options.allowUserDeselect = this.checked; }); } function setIsTabStop(spread, isTabStop) { var sheet = spread.getActiveSheet(); sheet.suspendPaint(); var sels = sheet.getSelections(); for (var index = 0; index < sels.length; index++) { var selRange = sels[index]; if (selRange.col >= 0 && selRange.row >= 0) { sheet.getRange(selRange.row, selRange.col, selRange.rowCount, selRange.colCount).tabStop(isTabStop); } else if (selRange.row >= 0) { sheet.getRange(selRange.row, -1, selRange.rowCount, -1).tabStop(isTabStop); } else if (selRange.col >= 0) { sheet.getRange(-1, selRange.col, -1, selRange.colCount).tabStop(isTabStop); } } sheet.resumePaint(); } function _getElementById(id) { return document.getElementById(id); }
<!doctype html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/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$/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"> Try changing the provided properties to see how they affect the selection. </div> <div class="option-row"> <label for="selectionPolicy" class="normal">SelectionPolicy:</label> </div> <div class="option-row"> <select id="selectionPolicy"> <option value="0">Single</option> <option value="1">Range</option> <option value="2" selected="selected">MultiRange</option> </select> </div> <div class="option-row"> <label for="selectionUnit" class="normal">SelectionUnit:</label> </div> <div class="option-row"> <select id="selectionUnit"> <option value="0" selected="selected">Cell</option> <option value="1">Row</option> <option value="2">Column</option> </select> </div> <div class="option-row"> <input type="checkbox" id="chk_allowDeselect" checked/> <label for="chk_allowDeselect">Allow User Deselect</label> </div> <div class="option-row"> <label for="backColor" class="wide">Selection BackColor:</label> </div> <div class="option-row"> <input type="text" id="backColor" value="rgba(204,255,51, 0.3)" /> <input type="button" value="Set" id="setBackColor" class="narrow" /> </div> <div class="option-row"> <label for="borderColor" class="wide">Selection BorderColor:</label> </div> <div class="option-row"> <input type="text" id="borderColor" value="Accent 3 -40" /> <input type="button" value="Set" id="setBorderColor" class="narrow" /> </div> <div class="option-row"> <input type="button" id="tabStoptrue_Btn" value="SetTabStop True" class="wide" style="padding-top: 4px"/> <input type="button" id="tabStopfalse_Btn" value="SetTabStop False" class="wide" style="padding-top: 4px"/> <label style="display: block;padding-top: 10px;">Set this to control whether the user can set focus to a selection using the Tab key.</label> </div> </div> </div> </body> </html>
input[type="text"] { width: 160px; } label.normal { display: inline-block; width: 120px; } select { width: 120px; height: 35px; } label.wide { display: inline-block; width: 160px; } input[type="button"].narrow { width: 60px; } input[type="button"].wide { width: 160px; } .colorLabel { background-color: lavender; } .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; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }