Table Operations

This sample shows how you can configure the table using the built-in JavaScript APIs as well as the context menu operations within a workbook.

Insert table rows or ranges before or after defined row: Delete table rows or ranges: Insert table columns or ranges before or after defined column: Delete table columns or ranges: Convert table to ranges through the existing API with options to keepData and keepStyle.
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss")); spread.fromJSON({"version":"12.0.0","customList":[],"sheetCount":2,"sheets":{"Sheet1":{"name":"Sheet1","activeRow":14,"activeCol":10,"theme":"Office","data":{"dataTable":{"3":{"2":{"value":"Order","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"Item","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":"Units","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":"Cost","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"4":{"2":{"value":"/OADate(41426)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"pencil","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":95,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":1.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"5":{"2":{"value":"/OADate(41367)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"binder","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":45,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":4.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"6":{"2":{"value":"/OADate(41520)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"pent set","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":60,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":15.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"7":{"2":{"value":"/OADate(41531.666666666664)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"pencil","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":16,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":1.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"8":{"2":{"value":"/OADate(41578.666666666664)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"binder","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":95,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":4.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"9":{"2":{"value":"/OADate(41625.666666666664)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"pent set","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":60,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":15.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"10":{"2":{"value":"/OADate(41672.666666666664)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"pencil","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":12,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":1.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"11":{"2":{"value":"/OADate(41719.666666666664)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"binder","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":65,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":4.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"12":{"2":{"value":"/OADate(41766.666666666664)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"pent set","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":45,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":15.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"13":{"2":{"value":"/OADate(41813.666666666664)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"pencil","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":14,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":1.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"14":{"2":{"value":"/OADate(41860.666666666664)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"binder","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":9,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":4.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}},"15":{"2":{"value":"/OADate(41907.666666666664)/","style":{"autoFormatter":{"formatCached":"M/d/yyyy"},"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"3":{"value":"pent set","style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"4":{"value":33,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}},"5":{"value":15.99,"style":{"hAlign":3,"vAlign":0,"themeFont":"Body","imeMode":1}}}},"defaultDataNode":{"style":{"themeFont":"Body"}}},"rowHeaderData":{"defaultDataNode":{"style":{"themeFont":"Body"}}},"colHeaderData":{"defaultDataNode":{"style":{"themeFont":"Body"}}},"columns":[null,null,{"size":97},null,null,{"size":77}],"leftCellIndex":0,"topCellIndex":0,"selections":{"0":{"row":14,"rowCount":1,"col":10,"colCount":1},"length":1},"cellStates":{},"outlineColumnOptions":{},"autoMergeRangeInfos":[],"tables":[{"name":"Table1","row":3,"col":2,"rowCount":13,"colCount":4,"showResizeHandle":true,"style":{"buildInName":"Medium2"},"rowFilter":{"range":{"row":4,"rowCount":12,"col":2,"colCount":4},"typeName":"HideRowFilter","dialogVisibleInfo":{},"filterButtonVisibleInfo":{"0":true,"1":true,"2":true,"3":true},"showFilterButton":true},"columns":[{"id":1,"name":"Order"},{"id":2,"name":"Item"},{"id":3,"name":"Units"},{"id":4,"name":"Cost"}]}],"index":0},"Sheet2":{"name":"Sheet2","theme":"Office","data":{},"rowHeaderData":{},"colHeaderData":{},"leftCellIndex":0,"topCellIndex":0,"selections":{"0":{"row":0,"rowCount":1,"col":0,"colCount":1},"length":1},"cellStates":{},"outlineColumnOptions":{},"autoMergeRangeInfos":[],"index":1}}}); initSpread(spread); }; function initSpread(spread) { var sheet = spread.getActiveSheet(); var table = sheet.tables.findByName('Table1'); document.getElementById("insertRows").onclick = function () { if (table) { try { var row = parseInt(document.getElementById("insertRowIndex").value); var count = parseInt(document.getElementById("insertRowCount").value); var isAfter = document.getElementById("insertAfterRow").checked; if (!isNaN(row) && !isNaN(count)) { table.insertRows(row - table.dataRange().row, count, isAfter); } else { alert("Row, Count must be a number"); } } catch (ex) { alert(!!ex.message ? ex.message : ex); } } }; document.getElementById("deleteRows").onclick = function () { if (table) { try { var row = parseInt(document.getElementById("deleteRowIndex").value); var count = parseInt(document.getElementById("deleteRowCount").value); if (!isNaN(row) && !isNaN(count)) { table.deleteRows(row - table.dataRange().row, count); } else { alert("Row, Count must be a number"); } } catch (ex) { alert(!!ex.message ? ex.message : ex); } } }; document.getElementById("insertColumns").onclick = function () { if (table) { try { var col = parseInt(document.getElementById("insertColIndex").value); var count = parseInt(document.getElementById("insertColCount").value); var isAfter = document.getElementById("insertAfterCol").checked; if (!isNaN(col) && !isNaN(count)) { table.insertColumns(col - table.dataRange().col, count, isAfter); } else { alert("Row, Count must be a number"); } } catch (ex) { alert(!!ex.message ? ex.message : ex); } } }; document.getElementById("deleteColumns").onclick = function () { if (table) { try { var col = parseInt(document.getElementById("deleteColIndex").value); var count = parseInt(document.getElementById("deleteColCount").value); if (!isNaN(col) && !isNaN(count)) { table.deleteColumns(col - table.dataRange().col, count); } else { alert("Row, Count must be a number"); } } catch (ex) { alert(!!ex.message ? ex.message : ex); } } }; document.getElementById("tableToRange").onclick = function () { try { if (table) { sheet.tables.remove(table, GC.Spread.Sheets.Tables.TableRemoveOptions.keepData | GC.Spread.Sheets.Tables.TableRemoveOptions.keepStyle); } } catch (ex) { alert(!!ex.message ? ex.message : ex); } }; } 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"> <p>Try inserting and remove rows and columns with the following fields and buttons. Simply enter a specific row or column index, and then the number of rows or columns to add.</p> <div class="option-group"> <label for="insertRowIndex">Row:</label> <input type="text" id="insertRowIndex"/> <label for="insertRowCount">Count:</label> <input type="text" id="insertRowCount"/> <input style="width: 20px;float: left;" type="checkbox" id="insertAfterRow"/> <label for="insertAfterRow">Insert after Current RowIndex</label> <input type="button" id="insertRows" value="Insert Rows"/> </div> <div class="option-group"> <label for="deleteRowIndex">Row:</label> <input type="text" id="deleteRowIndex"/> <label for="deleteRowCount">Count:</label> <input type="text" id="deleteRowCount"/> <input type="button" id="deleteRows" value="Delete Rows"/> </div> <div class="option-group"> <label for="insertColIndex">Column:</label> <input type="text" id="insertColIndex"/> <label for="insertColCount">Count:</label> <input type="text" id="insertColCount"/> <input style="width: 20px;float: left;" type="checkbox" id="insertAfterCol"/> <label for="insertAfterCol">Insert after Current ColIndex</label> <input type="button" id="insertColumns" value="Insert Columns"/> </div> <div class="option-group"> <label for="deleteColIndex">Column:</label> <input type="text" id="deleteColIndex"/> <label for="deleteColCount">Count:</label> <input type="text" id="deleteColCount"/> <input type="button" id="deleteColumns" value="Delete Columns"/> </div> <p>Click on this button to convert the table to a range of cells with the same styles applied.</p> <div class="option-group"> <input type="button" id="tableToRange" value="Table to Range"/> </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; } .option-group { margin-bottom: 6px; } label { display: block; margin-bottom: 6px; } input { margin-bottom: 5px; padding: 2px 4px; width: 100%; box-sizing: border-box; } input[type=button] { margin-bottom: 6px; } hr { border-color: #fff; opacity: .2; margin: 5px 0; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }