Initialization

The first step to work with SpreadJS is to add a workbook object to the web page and get an instance variable through which you can customize all aspects of the control and dynamically change the contents of the loaded workbook with JavaScript code.

You can add a SpreadJS workbook to a web page using the following steps: Add two link elements to the head section of the web page: one for the gc.spread.sheets.x.x.x.css stylesheet and the other for the gc.spread.sheets.all.x.x.x.min.js SpreadJS library. Create a div element with an id attribute in the body of the page to host the SpreadJS component. In the onload event of the web page define a javascript variable and assign it a workbook object by calling the Workbook constructor and providing it with the DOM object of the div element you created in the previous step. The Workbook object also takes an optional JSON object that specifies different properties for the workbook that can be set during instantiation. Alternatively, you can specify these options after the workbook is created. Once the object is created, you can use the workbook variable to load a SpreadJS SSJSON file, import an Excel file, change the visible elements of the control like the scrollbars, tabstrip, formula box, and dynamically modify the workbook's sheets, tables, and charts, etc. The other feature demos will demonstrate many of these properties. Finally, you can also access any workbook on the page using the GC.Spread.Sheets.findControl method and provide it with a DOM element on the page that hosts a workbook. If you use JQuery, instantiate the workbook as follows:
window.onload = function() { // host the workbook control in a DIV element with id "ss" var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'), { sheetCount: 1 }); /* * retrieve the spread workbook object from the host element using findControl static method. * var spread = GC.Spread.Sheets.findControl(document.getElementById('ss')); */ 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); }
<!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"> <!-- Workbook host element --> <div id="ss" style="width:100%;height:100%"></div> </div> </body> </html>
.sample-tutorial { position: relative; height: 100%; overflow: hidden; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }