你可以通过调用 bind 和 unbind 方法来监听表单的某一个事件或取消监听表单的某一个事件。你可以调用 unbindAll 方法来取消监听表单所有的事件。表单提供了与 jquery 的事件监听和取消监听相类似的机制。例如:
如果你执行多个任务导致事件触发了很多次,而你并不想表单在每次任务执行时都触发相应的事件, 你可以调用 suspendEvent 方法来挂起事件触发机制。当所有任务执行完毕之后, 你可以调用 resumeEvent 方法来重新唤醒事件触发机制, 示例代码如下:
var spreadNS = GC.Spread.Sheets;
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {
sheetCount: 1
});
initSpread(spread);
var statusBar = new GC.Spread.Sheets.StatusBar.StatusBar(
document.getElementById('statusBar')
);
statusBar.bind(spread);
};
function initSpread(spread) {
var sheet = spread.getSheet(0);
sheet.suspendPaint();
setStatus(sheet);
sheet.setValue(2, 2, "点击我,然后用键盘输入一个字符!");
sheet.addSpan(2, 2, 1, 5);
sheet.getRange(2, 2, 1, 5).setBorder(new spreadNS.LineBorder("Black", spreadNS.LineStyle.thin), {
all: true
});
sheet.setValue(4, 2, "双击带有黑色边框的空单元格!");
sheet.addSpan(4, 2, 1, 5);
sheet.addSpan(5, 2, 1, 5);
sheet.getRange(5, 2, 1, 5).setBorder(new spreadNS.LineBorder("Black", spreadNS.LineStyle.thin), {
all: true
});
sheet.setValue(7, 2, "双击我!");
sheet.addSpan(7, 2, 1, 5);
sheet.getRange(7, 2, 1, 5).setBorder(new spreadNS.LineBorder("Black", spreadNS.LineStyle.thin), {
all: true
});
sheet.setValue(9, 2, "双击带有黑色边框的空单元格,然后再次点击它!");
sheet.addSpan(9, 2, 1, 7);
sheet.addSpan(10, 2, 1, 5);
sheet.getRange(10, 2, 1, 5).setBorder(new spreadNS.LineBorder("Black", spreadNS.LineStyle.thin), {
all: true
});
sheet.setValue(12, 2, "按F2键开始编辑带有黑色边框的空单元格,然后再次按F2键切换编辑器状态。");
sheet.addSpan(13, 2, 1, 5);
sheet.getRange(13, 2, 1, 5).setBorder(new spreadNS.LineBorder("Black", spreadNS.LineStyle.thin), {
all: true
});
sheet.resumePaint();
sheet.bind(GC.Spread.Sheets.Events.ColumnChanging, function (sender, info) {
//insert column
if (info.propertyName === "addColumns") {
_getElementById("status").innerHTML = "调用了 ColumnChanging 事件!";
_getElementById("showSheetEvents").innerHTML = "ColumnChanging:增加列(" + "列:" + info.col + ")";
}
//delete column
if (info.propertyName === "deleteColumns") {
_getElementById("status").innerHTML = "调用了 ColumnChanging 事件!";
_getElementById("showSheetEvents").innerHTML = "ColumnChanging:删除列(" + "列:" + info.col + ")";
}
//unhide column
if (info.propertyName === "isVisible" && info.newValue === true) {
_getElementById("status").innerHTML = "调用了 ColumnChanging 事件!";
_getElementById("showSheetEvents").innerHTML = "ColumnChanging:显示列(" + "列:" + info.col + ")";
}
//hide column
if (info.propertyName === "isVisible" && info.newValue === false) {
_getElementById("status").innerHTML = "调用了 ColumnChanging 事件!";
_getElementById("showSheetEvents").innerHTML = "ColumnChanging:隐藏列(" + "列:" + info.col + ")";
}
});
sheet.bind(GC.Spread.Sheets.Events.ColumnChanged, function (sender, info) {
//insert column
if (info.propertyName === "addColumns") {
_getElementById("status").innerHTML = "调用了 ColumnChanged 事件!";
_getElementById("showSheetEvents").innerHTML = "ColumnChanged:插入列(" + "列:" + info.col + ")";
}
//delete column
if (info.propertyName === "deleteColumns") {
_getElementById("status").innerHTML = "调用了 ColumnChanged 事件!";
_getElementById("showSheetEvents").innerHTML = "ColumnChanged:删除列(" + "列:" + info.col + ")";
}
//unhide column
if (info.propertyName === "isVisible" && info.newValue === true) {
_getElementById("status").innerHTML = "调用了 ColumnChanged 事件!";
_getElementById("showSheetEvents").innerHTML = "ColumnChanged:显示列(" + "列:" + info.col + ")";
}
//hide column
if (info.propertyName === "isVisible" && info.newValue === false) {
_getElementById("status").innerHTML = "调用了 ColumnChanged 事件!";
_getElementById("showSheetEvents").innerHTML = "ColumnChanged:隐藏列(" + "列:" + info.col + ")";
}
});
//RowChanging
sheet.bind(GC.Spread.Sheets.Events.RowChanging, function (sender, info) {
//insert row
if (info.propertyName === "addRows") {
_getElementById("status").innerHTML = "调用了 RowChanging 事件!";
_getElementById("showSheetEvents").innerHTML = "RowChanging:插入行(" + "行:" + info.row + ")";
}
//delete row
if (info.propertyName === "deleteRows") {
_getElementById("status").innerHTML = "调用了 RowChanging 事件!";
_getElementById("showSheetEvents").innerHTML = "RowChanging:删除行(" + "行:" + info.row + ")";
}
//unhide row
if (info.propertyName === "isVisible" && info.newValue === true) {
_getElementById("status").innerHTML = "调用了 RowChanging 事件!";
_getElementById("showSheetEvents").innerHTML = "RowChanging:显示行(" + "行:" + info.row + ")";
}
//hide row
if (info.propertyName === "isVisible" && info.newValue === false) {
_getElementById("status").innerHTML = "调用了 RowChanging 事件!";
_getElementById("showSheetEvents").innerHTML = "RowChanging:隐藏行(" + "行:" + info.row + ")";
}
});
sheet.bind(GC.Spread.Sheets.Events.RowChanged, function (sender, info) {
//insert row
if (info.propertyName === "addRows") {
_getElementById("status").innerHTML = "调用了 RowChanged 事件!";
_getElementById("showSheetEvents").innerHTML = "RowChanged:插入行(" + "行:" + info.row + ")";
}
//delete row
if (info.propertyName === "deleteRows") {
_getElementById("status").innerHTML = "调用了 RowChanged 事件!";
_getElementById("showSheetEvents").innerHTML = "RowChanged:删除行(" + "行:" + info.row + ")";
}
//unhide row
if (info.propertyName === "isVisible" && info.newValue === true) {
_getElementById("status").innerHTML = "调用了 RowChanged 事件!";
_getElementById("showSheetEvents").innerHTML = "RowChanged:显示行(" + "行:" + info.row + ")";
}
//hide row
if (info.propertyName === "isVisible" && info.newValue === false) {
_getElementById("status").innerHTML = "调用了 RowChanged 事件!";
_getElementById("showSheetEvents").innerHTML = "RowChanged:隐藏行(" + "行:" + info.row + ")";
}
});
sheet.bind(spreadNS.Events.EditorStatusChanged, function () {
setStatus(sheet);
});
sheet.bind(spreadNS.Events.SelectionChanging, function (e, info) {
_getElementById("status").innerHTML = "调用了 SelectionChanging 事件!";
_getElementById("showSheetEvents").innerHTML = "新的选区(" + "行: " + info.newSelections[0].row + ", " + "列: " + info.newSelections[0].col + ", " + "行数: " + info.newSelections[0].rowCount + ", " + "列数: " + info.newSelections[0].colCount + ")";
})
};
function setStatus(sheet) {
var statusNow = sheet.editorStatus();
if (statusNow === spreadNS.EditorStatus.ready) {
_getElementById("status").innerHTML = "编辑器状态:就绪!";
_getElementById("showSheetEvents").innerHTML = "单元格未处于编辑状态。";
} else if (statusNow === spreadNS.EditorStatus.enter) {
_getElementById("status").innerHTML = "编辑器状态:进入!";
_getElementById("showSheetEvents").innerHTML = "单元格正在编辑中,您可以按任意一个箭头键离开该单元格。";
} else if (statusNow === spreadNS.EditorStatus.edit) {
_getElementById("status").innerHTML = "编辑器状态:编辑!";
_getElementById("showSheetEvents").innerHTML = "单元格正在编辑中,您无法通过按任意一个箭头键离开该单元格。";
}
}
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>
<link rel="stylesheet" type="text/css" href="styles.css">
</head>
<body>
<div class="sample-tutorial">
<div id="ss" class="sample-spreadsheets"></div>
<div id="statusBar"></div>
<div class="options-container">
<div class="option-row">
<label style="color:rgb(226,107,29);display:inline-block;font-family:Arial, Helvetica, sans-serif;font-size: 18px;font-weight: normal;height:30px;line-height: 30px " id="status">Settings</label>
</div>
<div class="option-row">
<textarea id="showSheetEvents" cols="40" rows="5" style="width: 80%"></textarea>
</div>
</div>
</div>
</body>
</html>
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: calc(100% - 280px);
height: calc(100% - 25px);
overflow: hidden;
float: left;
}
#statusBar {
bottom: 0;
height: 25px;
width: 100%;
position: relative;
}
.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;
}