概述
本 Demo 展示了如何使用 SpreadJS 表格 API 对表格进行行列操作,包括插入行、删除行、插入列、删除列,以及将表格转换为普通区域。通过界面按钮和输入框,用户可以交互式地调整表格结构,实时查看操作效果。
实现思路
加载预设的表格数据(使用 fromJSON 加载包含表格的工作表)
通过 sheet.tables.findByName() 获取表格对象实例
为每个操作按钮绑定点击事件处理函数
在事件处理函数中,获取用户输入的行/列索引和数量
将工作表坐标转换为表格相对坐标,调用相应的表格 API 执行操作
实现表格转区域功能,移除表格结构但保留数据和样式
代码解析
获取表格实例
通过 findByName 方法获取名为 'Table1' 的表格实例,后续所有表格操作都基于此实例进行。
插入行
这段代码从输入框获取要插入的行索引和数量,以及是否在当前行之后插入的标志。table.dataRange().row 获取表格数据区域的起始行索引,用于将工作表行索引转换为表格相对行索引。insertRows 方法接受三个参数:表格内的相对行索引、插入的行数、是否在指定行之后插入。
删除行
删除行的实现与插入行类似,同样需要将工作表行索引转换为表格相对行索引。deleteRows 方法接受两个参数:要删除的起始行索引和删除的行数。
插入列和删除列
列操作与行操作方式相同,使用 insertColumns 和 deleteColumns 方法。同样需要将工作表列索引转换为表格相对列索引。
表格转区域
tables.remove 方法用于将表格转换为普通区域。第二个参数使用 TableRemoveOptions 枚举指定移除选项:keepData 表示保留单元格数据,keepStyle 表示保留样式。使用位运算符 | 可以同时指定多个选项。
运行效果
在界面左侧的输入框中输入行索引和数量,点击"插入行"或"删除行"按钮,表格会相应地插入或删除指定数量的行
勾选"之后插入"复选框后,新行会插入到指定行之后;否则插入到指定行位置
在界面右侧的输入框中输入列索引和数量,点击"插入列"或"删除列"按钮,表格会相应地插入或删除指定数量的列
点击"表格转区域"按钮后,表格结构会被移除,但单元格数据和样式会保留
所有操作都会实时反映在表格上
API 参考
Table.insertRows 方法
index:表格内的相对行索引
count:插入的行数
isAfter:布尔值,是否在指定行之后插入
Table.deleteRows 方法
index:表格内的相对行索引
count:删除的行数
Table.insertColumns / deleteColumns 方法
参数含义与行操作方法相同,作用于列
tables.remove 方法
table:要移除的表格实例
removeOptions:TableRemoveOptions 枚举,支持 keepData 和 keepStyle,可通过 | 组合使用
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;
}