Spreadsheet View

This demo show how you can manipulate the sheets in a workbook by dynamically adding and removing them with JavaScript code. Try using the buttons below to see this functionality in action.

<p>To work with the worksheets of the SpreadJS workbook you need a reference to one of the worksheets. You can access any worksheet by its index, which is its position in the workbook, using the <strong>getSheet</strong> method of the Workbook object or using its name, which is the sheet's tab name, using the <strong>getSheetFromName</strong> worbkook method. For example:</p> <pre><code class="hljs js language-js"> <span class="hljs-comment">// Initialize a workbook</span> <span class="hljs-keyword">var</span> workbook = GC.workbook.Sheets.findControl(<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">'ss'</span>)); <span class="hljs-comment">// access a sheet by index</span> <span class="hljs-keyword">var</span> sheet = workbook.getSheet(<span class="hljs-number">0</span>); <span class="hljs-comment">// or by tab name</span> <span class="hljs-keyword">var</span> sheet1 = workbook.getSheetFromName(<span class="hljs-string">'Sheet1'</span>); </code></pre> <p>Use the Workbook <strong>getActiveSheet</strong> method to get an instance of the currently active worksheet.</p> <pre><code class="hljs js language-js"> <span class="hljs-comment">// Get the active sheet</span> <span class="hljs-keyword">var</span> sheet = workbook.getActiveSheet(); </code></pre> <p>To change the active worksheet use the <strong>setActiveSheetIndex</strong> method and provide the index of the desired sheet. For example:</p> <pre><code class="hljs js language-js"> <span class="hljs-comment">// Change the active Sheet</span> workbook.setActiveSheetIndex(<span class="hljs-number">1</span>); </code></pre> <p>Once you have a reference to a worksheet, you can use it to customize it as illustrated by the demos in the Worksheet section.</p> <p>Other useful methods to work with the workbook sheets:</p> <ul> <li><strong>addSheet</strong>: add a named sheet at a specific position in the workbook.</li> </ul> <pre><code class="hljs js language-js"> <span class="hljs-keyword">var</span> sheet = <span class="hljs-keyword">new</span> GC.workbook.Sheets.Worksheet(<span class="hljs-string">'New Sheet'</span>); workbook.addSheet(<span class="hljs-number">0</span>, sheet); </code></pre> <ul> <li><strong>getSheetCount</strong>: get the number of sheets in the workbook and use it to append a sheet to the workbook.</li> </ul> <pre><code class="hljs js language-js"> workbook.addSheet(workbook.getSheetCount()); </code></pre> <ul> <li><strong>removeSheet</strong> and <strong>clearSheets</strong>: remove a sheet by index or clear all the workbook sheets.</li> </ul> <pre><code class="hljs js language-js"> workbook.removeSheet(<span class="hljs-number">0</span>); workbook.clearSheets(); </code></pre> <ul> <li><strong>getActiveSheetIndex</strong>: get the index of the ActiveSheet.</li> </ul> <pre><code class="hljs js language-js"> <span class="hljs-comment">// get the index of the active sheet</span> <span class="hljs-keyword">var</span> activeSheetIndex = workbook.getActiveSheetIndex(); </code></pre>
window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss')); var spreadNS = GC.Spread.Sheets; spread.setSheetCount(3); initSpread(spread); spread.bind(spreadNS.Events.ActiveSheetChanged, function(e, args) { _getElementById('activeSheetIndex').value = spread.getActiveSheetIndex(); }); _getElementById('btnAddSheet').addEventListener('click',function() { spread.addSheet(spread.getSheetCount()); }); _getElementById('btnRemoveSheet').addEventListener('click',function() { var activeIndex = spread.getActiveSheetIndex(); if (activeIndex >= 0) { spread.removeSheet(activeIndex); } }); _getElementById('btnClearSheets').addEventListener('click',function() { spread.clearSheets(); }); _getElementById('btnSetActiveSheetIndex').addEventListener('click',function() { var index = _getElementById('activeSheetIndex').value; if (!isNaN(index)) { index = parseInt(index); if (0 <= index && index < spread.getSheetCount()) { spread.setActiveSheetIndex(index); } } }); }; 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); } 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"> <input type="button" value="Add Sheet" id="btnAddSheet"/> <input type="button" value="Remove Sheet" id="btnRemoveSheet"/> <input type="button" value="Clear Sheets" id="btnClearSheets"/> </div> <div class="option-row"> <label>ActiveSheetIndex:</label> <input type="text" id="activeSheetIndex" value="0"/> <input type="button" id="btnSetActiveSheetIndex" value="Set"/> </div> <div class="option-row"> <label>This switches the currently active sheet to the sheet at the specified index.</label> </div> </div> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } .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; }