概述
本 Demo 展示了 SpreadJS 中用户可以通过交互行为改变单元格值的功能,包括剪贴板操作(复制、剪切、粘贴)和拖放操作。Demo 提供了交互式的控制选项,可以实时调整拖放行为、提示信息和剪切/复制指示器的显示样式。
实现思路
使用 fromJSON 方法加载预设的表格数据
为多个配置选项绑定事件监听器,实时控制用户交互行为
通过 allowUserDragDrop 和 showDragDropTip 控制拖放功能和提示显示
通过 cutCopyIndicatorVisible 和 cutCopyIndicatorBorderColor 控制剪切/复制指示器的可见性和样式
通过 isProtected 属性演示工作表保护对交互行为的影响
代码解析
控制拖放功能
allowUserDragDrop 属性控制用户是否可以拖动和放置单元格区域。当设置为 true 时,用户可以通过拖动单元格边缘来移动或复制数据(按住 Ctrl 键为复制)。
控制拖放提示
showDragDropTip 属性控制在拖放操作过程中是否显示提示信息。提示会以 A1 或 R1C1 引用样式显示目标单元格位置。
控制剪切/复制指示器
cutCopyIndicatorVisible 控制在复制或剪切单元格时是否显示虚线边框指示器。cutCopyIndicatorBorderColor 可以自定义指示器的边框颜色,接受颜色名称(如 "red"、"blue")或十六进制值。
工作表保护
isProtected 属性设置工作表为受保护状态,限制用户的编辑行为。受保护的工作表中,被锁定的单元格不可编辑。
运行效果
拖放操作:选中单元格区域,将鼠标移至边缘并拖动,可移动数据;按住 Ctrl 键拖动可复制数据
拖放提示:拖动过程中会显示目标位置的提示信息(如 "A1:C3")
剪切/复制指示器:按 Ctrl+C 或 Ctrl+X 后,选中的区域会显示虚线边框
自定义指示器颜色:在输入框中输入颜色名称(如 "red"),点击设置按钮可更改虚线边框颜色
工作表保护:勾选"已保护"后,被锁定的单元格无法编辑,也无法进行拖放操作
API 参考
allowUserDragDrop
是否允许用户拖放范围数据。默认值为 true。
showDragDropTip
是否显示拖放提示。默认值为 true。
cutCopyIndicatorVisible
是否显示复制或剪切选定项时的指示器。默认值为 true。
cutCopyIndicatorBorderColor
复制或剪切选定项时显示的指示器的边框颜色。支持颜色名称(如 "red")或十六进制值。
isProtected
指示此工作表中标记为受保护的单元格是否不可编辑。
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss'), { sheetCount: 1 });
initSpread(spread);
};
function initSpread(spread) {
var sheet = spread.getActiveSheet();
spread.fromJSON(data[0]);
_getElementById('chkShowDragDropTip').addEventListener('click', function() {
spread.options.showDragDropTip = _getElementById('chkShowDragDropTip').checked;
});
_getElementById('chkAllowUserDragDrop').addEventListener('click', function() {
spread.options.allowUserDragDrop = _getElementById('chkAllowUserDragDrop').checked;
});
_getElementById('cutCopyIndicatorVisible').addEventListener('change', function() {
var value = _getElementById('cutCopyIndicatorVisible').checked;
spread.options.cutCopyIndicatorVisible = value;
});
_getElementById('setCutCopyIndicatorBorderColor').addEventListener('click', function() {
var value = _getElementById('cutCopyIndicatorBorderColor').value;
spread.options.cutCopyIndicatorBorderColor = value;
});
_getElementById('setIsProtected').addEventListener('click', function() {
var sheet = spread.getActiveSheet();
var value = _getElementById('setIsProtected').checked;
sheet.options.isProtected = value;
});
}
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>
<script src="data.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">
<input id="chkAllowUserDragDrop" type="checkbox" checked="checked" />
<label for="chkAllowUserDragDrop" class="sizedLabel">允许用户拖放</label>
</div>
<div class="option-row">
<input id="chkShowDragDropTip" type="checkbox" checked="checked" />
<label for="chkShowDragDropTip" class="sizedLabel">显示拖放提示</label>
</div>
<div class="option-row">
<input type="checkbox" id="cutCopyIndicatorVisible" checked="checked" />
<label for="cutCopyIndicatorVisible" class="sizedLabel">显示剪切/复制指示器</label>
</div>
<div class="option-row">
<input type="checkbox" id="setIsProtected" />
<label for="setIsProtected" class="sizedLabel">已保护</label>
</div>
<hr>
<div class="option-row">
<label>输入颜色名称,如 "red" 或 "blue",以更改剪切/复制指示器边框的颜色。</label>
</div>
<label>剪切/复制指示器边框颜色:</label>
<div class="option-row">
<input type="text" style="margin:0 8px; width: 150px;" id="cutCopyIndicatorBorderColor" />
<input type="button" value="设置" id="setCutCopyIndicatorBorderColor" />
</div>
</div>
</div>
</body>
</html>
var data = [{
"version":"12.0.0",
"tabStripRatio":0.6,
"sheetCount":1,
"sheets":{
"Sheet1":{
"name":"Sheet1",
"rowCount":114,
"columnCount":21,
"activeRow":2,
"activeCol":2,
"data":{
"dataTable":{
"0":{
"0":{
"value":"Salesperson",
"style":
{
"foreColor" : "#FFFFFF",
"backColor" : "#808080",
"hAlign" : 1
}
},
"1":{
"value":"Region",
"style": {
"foreColor" : "#FFFFFF",
"backColor" : "#808080",
"hAlign" : 1
}
},
"2":{
"value":"Sales",
"style": {
"foreColor" : "#FFFFFF",
"backColor" : "#808080",
"hAlign" : 1
}
}
},
"1":{
"0":{
"value":"Ally",
"style":{"hAlign" : 1}
},
"1":{
"value":"North",
"style":{"hAlign" : 1}
},
"2":{
"value":24234324,
"style":{"hAlign" : 1, "formatter": "$#,##0"}
}
},
"2":{
"0":{
"value":"Tom",
"style":{"hAlign" : 1}
},
"1":{
"value":"South",
"style":{"hAlign" : 1}
},
"2":{
"value":2342342,
"style":{"hAlign" : 1, "formatter": "$#,##0"}
}
},
"3":{
"0":{
"value":"Jack",
"style":{"hAlign" : 1}
},
"1":{
"value":"South",
"style":{"hAlign" : 1}
},
"2":{
"value":324234,
"style":{"hAlign" : 1, "formatter": "$#,##0"}
}
},
"4":{
"0":{
"value":"John",
"style":{"hAlign" : 1}
},
"1":{
"value":"West",
"style":{"hAlign" : 1}
},
"2":{
"value":2342443,
"style":{"hAlign" : 1, "formatter": "$#,##0"}
}
},
"5":{
"0":{
"value":"Lily",
"style":{"hAlign" : 1}
},
"1":{
"value":"North",
"style":{"hAlign" : 1}
},
"2":{
"value":2342342,
"style":{"hAlign" : 1, "formatter": "$#,##0"}
}
},
"6":{
"0":{
"value":"Linda",
"style":{"hAlign" : 1}
},
"1":{
"value":"East",
"style":{"hAlign" : 1}
},
"2":{
"value":5857858,
"style":{"hAlign" : 1, "formatter": "$#,##0"}
}
},
"7":{
"0":{
"value":"Will",
"style":{"hAlign" : 1}
},
"1":{
"value":"North",
"style":{"hAlign" : 1}
},
"2":{
"value":437587965,
"style":{"hAlign" : 1, "formatter": "$#,##0"}
}
}
}
},
"defaults": {"colHeaderRowHeight": 20, "colWidth": 120, "rowHeaderColWidth": 40, "rowHeight": 26},
}
}
}];
label {
display:inline-block;
}
.sizedLabel {
width: 180px;
}
hr {
border-color: #fff;
opacity: .2;
margin-top: 20px;
}
.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 {
margin-bottom: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}