表格操作

你可以通过API和右键菜单来进行表格操作。

在表格某行之前或之后插入行或区域。 删除行或区域: 在表格某列之前或之后插入列或区域。 删除列或区域: 通过API来将表格转换为普通区域,并且保持数据和样式。
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss")); spread.fromJSON(data); 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("行, 数量必须为正整数"); } } 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("行, 数量必须为正整数"); } } 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("列, 数量必须为正整数"); } } 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("列, 数量必须为正整数"); } } 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 name="spreadjs culture" content="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.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> <script src="$DEMOROOT$/spread/source/data/table-operator.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>尝试使用以下字段和按钮插入和删除行与列。只需输入特定的行或列索引,然后输入要添加的行或列数量。</p> <div class="option-group"> <label for="insertRowIndex">行:</label> <input type="text" id="insertRowIndex" /> <label for="insertRowCount">数量:</label> <input type="text" id="insertRowCount" /> <input style="width: 20px;float: left;" type="checkbox" id="insertAfterRow" /> <label for="insertAfterRow">在当前行索引后插入</label> <input type="button" id="insertRows" value="插入行" /> </div> <div class="option-group"> <label for="deleteRowIndex">行:</label> <input type="text" id="deleteRowIndex" /> <label for="deleteRowCount">数量:</label> <input type="text" id="deleteRowCount" /> <input type="button" id="deleteRows" value="删除行" /> </div> <div class="option-group"> <label for="insertColIndex">列:</label> <input type="text" id="insertColIndex" /> <label for="insertColCount">数量:</label> <input type="text" id="insertColCount" /> <input style="width: 20px;float: left;" type="checkbox" id="insertAfterCol" /> <label for="insertAfterCol">在当前列索引后插入</label> <input type="button" id="insertColumns" value="插入列" /> </div> <div class="option-group"> <label for="deleteColIndex">列:</label> <input type="text" id="deleteColIndex" /> <label for="deleteColCount">数量:</label> <input type="text" id="deleteColCount" /> <input type="button" id="deleteColumns" value="删除列" /> </div> <p>点击此按钮可将表格转换为应用了相同样式的单元格区域。</p> <div class="option-group"> <input type="button" id="tableToRange" value="表格转区域" /> </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; }