你可以同时监听 Workbook 和 Worksheet 的事件。你可以通过调用bind 和 unbind 方法来监听某一个事件或取消监听某一个事件。 你可以调用 unbindAll 方法来取消所有监听的事件。SpreadJS 提供了与 jquery 的事件监听和事件取消监听相类似的机制。例如:
如果你不希望当前的 spread 或是表单在行为过程中触发任何的事件, 可以调用 suspendEvent 方法来挂起事件触发机制。当然也可以调用 resumeEvent 方法来重新唤醒事件触发机制。例如:
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(_getElementById("ss"), { sheetCount: 3 });
initSpread(spread);
};
function initSpread(spread) {
var spreadNS = GC.Spread.Sheets;
var sheet = spread.getActiveSheet();
spread.bind(spreadNS.Events.ActiveSheetChanged, function (e, args) {
_getElementById("showSpreadEvents").value=
'SpreadEvent: ' + GC.Spread.Sheets.Events.ActiveSheetChanged + ' event called' + '\n' +
'oldSheetName: ' + args.oldSheet.name() + '\n' +
'newSheetName: ' + args.newSheet.name();
});
spread.bind(spreadNS.Events.CellClick, function (e, args) {
var sheetArea = args.sheetArea === 0 ? 'sheetCorner' : args.sheetArea === 1 ? 'columnHeader' : args.sheetArea === 2 ? 'rowHeader' : 'viewPort';
_getElementById("showSpreadEvents").value=
'SpreadEvent: ' + GC.Spread.Sheets.Events.CellClick + ' event called' + '\n' +
'sheetArea: ' + sheetArea + '\n' +
'row: ' + args.row + '\n' +
'col: ' + args.col;
});
spread.bind(spreadNS.Events.SelectionChanging, function (e, args) {
var selection = args.newSelections.pop();
var sheetArea = args.sheetArea === 0 ? 'sheetCorner' : args.sheetArea === 1 ? 'columnHeader' : args.sheetArea === 2 ? 'rowHeader' : 'viewPort';
_getElementById("showSpreadEvents").value=
'SpreadEvent: ' + GC.Spread.Sheets.Events.SelectionChanging + ' event called' + '\n' +
'sheetArea: ' + sheetArea + '\n' +
'row: ' + selection.row + '\n' +
'column: ' + selection.col + '\n' +
'rowCount: ' + selection.rowCount + '\n' +
'colCount: ' + selection.colCount;
});
spread.bind(spreadNS.Events.SelectionChanged, function (e, args) {
var selection = args.newSelections.pop();
if(selection.rowCount > 1 && selection.colCount > 1){
var sheetArea = args.sheetArea === 0 ? 'sheetCorner' : args.sheetArea === 1 ? 'columnHeader' : args.sheetArea === 2 ? 'rowHeader' : 'viewPort';
_getElementById("showSpreadEvents").value=
'SpreadEvent: ' + GC.Spread.Sheets.Events.SelectionChanged + ' event called' + '\n' +
'sheetArea: ' + sheetArea + '\n' +
'row: ' + selection.row + '\n' +
'column: ' + selection.col + '\n' +
'rowCount: ' + selection.rowCount + '\n' +
'colCount: ' + selection.colCount;
}});
spread.bind(spreadNS.Events.EditStarting, function (e, args) {
_getElementById("showSpreadEvents").value=
'SpreadEvent: ' + GC.Spread.Sheets.Events.EditStarting + ' event called' + '\n' +
'row: ' + args.row + '\n' +
'column: ' + args.col;
});
spread.bind(spreadNS.Events.EditEnded, function (e, args) {
_getElementById("showSpreadEvents").value=
'SpreadEvent: ' + GC.Spread.Sheets.Events.EditEnded + ' event called' + '\n' +
'row: ' + args.row + '\n' +
'column: ' + args.col + '\n' +
'text: ' + args.editingText;
});
};
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$/spread/source/data/data.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 class="options-container">
<div class="option-row">
<label>Click the sheet tab to change the active sheet. Click the cell or select range to edit.</label>
</div>
<div class="option-row">
<textarea id="showSpreadEvents" cols="40" rows="8" style="width: 90%"></textarea>
</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;
}
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;
}