Multiple Sheets Selection

Users can select multiple sheets for batch operations using the Shift/Ctrl/Cmd key and clicking to select and deselect sheet tabs. This is the default SpreadJS behavior which matches Excel. In addition, Worksheet.isSelected method is used to select and deselect worksheets.

<p>You can get or set selected state of a worksheet using <strong>isSelected</strong> method, for example:</p> <pre><code class="hljs javascript language-javascript"><span class="hljs-comment">// get the selected state of a sheet</span> <span class="hljs-keyword">var</span> selectedState = sheet.isSelected(); <span class="hljs-comment">// set the selected state of a sheet</span> sheet.isSelected(<span class="hljs-literal">true</span>); sheet.isSelected(<span class="hljs-literal">false</span>); </code></pre> <p>The active sheet is always selected and cannot be set to unselected. If you set active sheet as unselected using the <strong>isSelected</strong> method, the active but not selected sheet will have another sheet tab view.</p> <p>You can also change the selected state for a sheet or sheets by clicking with shift/ctrl key (in MacOS you need to use shift/command key instead).</p> <p>Spread also provides two events when changing the selected state of a sheet.</p> <pre><code class="hljs javascript language-javascript"><span class="hljs-keyword">var</span> sheetSelectedStateChanging = <span class="hljs-literal">false</span>; <span class="hljs-keyword">var</span> sheetSelectedStateChanged = <span class="hljs-literal">false</span>; <span class="hljs-comment">// If the selected state doesn't change, the two events won't be triggered.</span> spread.bind(GC.Spread.Sheets.Events.SheetChanging, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e, args</span>) </span>{ <span class="hljs-keyword">if</span> (sheetSelectedStateChanging === <span class="hljs-literal">false</span>) { sheetSelectedStateChanging = <span class="hljs-literal">true</span>; args.cancel = <span class="hljs-literal">true</span>; } }); <span class="hljs-comment">// If the data.cancel is true, the SheetSelectedStateChanged won't be triggered.</span> spread.bind(GC.Spread.Sheets.Events.SheetChanged, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">e, args</span>) </span>{ sheetSelectedStateChanged = <span class="hljs-literal">true</span>; }); <span class="hljs-comment">// sheetSelectedStateChanging is true now;</span> <span class="hljs-comment">// sheetSelectedStateChanged is false now;</span> </code></pre> <p>Spread supports batch delete and hide functions of multiple selected sheets.</p>
window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss'), { sheetCount: 5 }); initCheckBoxesEvent(spread); initSpread(spread); }; function initCheckBoxesEvent (spread) { var sheetsNames = ["Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5"]; sheetsNames.forEach(function (name) { document.getElementById(name).addEventListener('mouseup', function (e) { var sheet = spread.getSheetFromName(name); sheet.isSelected(!this.checked); // This time, the checkbox's checked state hasn't changed. spread.repaint(); }); }); } function initSpread(spread) { var sheet1 = spread.getSheetFromName("Sheet1"); var sheet2 = spread.getSheetFromName("Sheet2"); var sheet3 = spread.getSheetFromName("Sheet3"); sheet1.isSelected(true); sheet2.isSelected(true); sheet3.isSelected(true); spread.bind(GC.Spread.Sheets.Events.SheetChanged, function (e, args) { var checkboxElement = document.getElementById(args.sheetName); checkboxElement.checked = !checkboxElement.checked; }); spread.repaint(); } 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/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"> <p>Click the checkbox to select or deselect the sheet.</p> <p>Hold down the Shift/Ctrl/Cmd key and click the sheet tab to select/deselect multiple sheets. The sheet checkbox will update correspondingly.</p> </div> <div class="sheets-checkboxes-container"> <div class="option-row"> <input type="checkbox" id="Sheet1" checked/> <label for="sheet1" id="labelOfSheet1">Sheet1</label> </div> <div class="option-row"> <input type="checkbox" id="Sheet2" checked/> <label for="sheet2" id="labelOfSheet2">Sheet2</label> </div> <div class="option-row"> <input type="checkbox" id="Sheet3" checked/> <label for="sheet3" id="labelOfSheet3">Sheet3</label> </div> <div class="option-row"> <input type="checkbox" id="Sheet4" /> <label for="sheet4" id="labelOfSheet4">Sheet4</label> </div> <div class="option-row"> <input type="checkbox" id="Sheet5" /> <label for="sheet5" id="labelOfSheet5">Sheet5</label> </div> </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; } .sheets-checkboxes-container { padding-left: 10px; } label { margin-bottom: 6px; } input { padding: 4px 6px; } hr { border-color: #fff; opacity: .2; margin-top: 20px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }