概述
本 Demo 展示了如何在 SpreadJS 中添加和管理形状。Demo 初始化时添加了一个矩形和一个直线连接器,并通过下拉菜单提供交互式功能,用户可以选择不同的形状类型动态添加到工作表中。
实现思路
加载 SpreadJS Workbook 实例和形状插件
使用 fillShapeTypeList 函数填充下拉菜单,列出所有可用的基础形状和连接器形状类型
初始化时通过 shapes.add() 添加一个矩形基础形状
初始化时通过 shapes.addConnector() 添加一个直线连接器,并设置线条样式
为"添加形状"和"添加连接形状"按钮绑定点击事件,实现动态添加形状功能
代码解析
填充形状类型下拉菜单
这段代码遍历形状类型枚举(AutoShapeType 或 ConnectorType),过滤无效选项后按名称排序,填充到下拉菜单中供用户选择。
添加基础形状
使用 shapes.add() 方法添加基础形状,参数依次为形状名称、形状类型、左侧位置、顶部位置、宽度和高度。
添加连接器形状并设置样式
使用 shapes.addConnector() 方法添加连接器形状,参数依次为形状名称、连接器类型、起点 X、起点 Y、终点 X 和终点 Y。通过 style() 方法获取样式对象,修改线条宽度后再设置回去。
动态添加形状
点击"添加"按钮时,根据当前形状数量计算新形状的位置(自动排列布局),从下拉菜单获取选中的形状类型并添加到工作表中。hAlign = 1 设置形状的水平对齐方式为居中。
运行效果
页面加载后,工作表中已显示一个矩形和一个粗直线连接器
从"添加形状"下拉菜单中选择形状类型(如心形、箭头等),点击"添加"按钮,该形状会被添加到工作表中
从"添加连接形状"下拉菜单中选择连接器类型(如直线、弯头线等),点击"添加"按钮,连接器会被添加到工作表中
新添加的形状会自动按网格布局排列,奇偶行错开显示
API 参考
shapes.add() 方法
name:形状名称
autoShapeTypeOrModel:形状类型枚举值或模型对象
left?:左侧位置(可选)
top?:顶部位置(可选)
width?:宽度(可选)
height?:高度(可选)
返回添加的形状对象。
shapes.addConnector() 方法
name:连接器名称
connectorType:连接器类型枚举值
beginX?:起点 X 坐标(可选)
beginY?:起点 Y 坐标(可选)
endX?:终点 X 坐标(可选)
endY?:终点 Y 坐标(可选)
返回添加的连接器形状对象。
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
initSpread(spread);
initEvent(spread);
};
function fillShapeTypeList(type, dom) {
var names = [];
for (var name in type) {
if(name === "none" || (parseInt(name, 10)) == name) {
continue;
}
names.push({name: name, value: type[name]});
}
names.sort(function (a, b) {
return a.name > b.name ? 1 : -1
});
var html = "";
names.forEach(function (item) {
html += '<option value="' + item.value + '">' + item.name + '</option>';
});
dom.innerHTML= html;
}
function getActiveConnectorShape(sheet) {
return sheet.shapes.all().filter(function(sp){
return sp.isSelected() && sp instanceof GC.Spread.Sheets.Shapes.ConnectorShape;
});
}
function initSpread(spread) {
fillShapeTypeList(GC.Spread.Sheets.Shapes.AutoShapeType, _getElementById("autoShapeType"));
fillShapeTypeList(GC.Spread.Sheets.Shapes.ConnectorType, _getElementById("connectShapeType"));
spread.getActiveSheet().shapes.add("rectangle", GC.Spread.Sheets.Shapes.AutoShapeType.rectangle, 40, 20, 150, 150);
var lineShape = spread.getActiveSheet().shapes.addConnector("line", GC.Spread.Sheets.Shapes.ConnectorType.straight, 290, 20, 420, 170);
var lineShapeStyle = lineShape.style();
lineShapeStyle.line.width = 8;
lineShape.style(lineShapeStyle);
}
function initEvent(spread) {
_getElementById("insertShape").addEventListener('click', function () {
var sheet = spread.getActiveSheet(), shapes = sheet.shapes, total = shapes.all().length;
var x = 40 + (total % 2) * 250, y = parseInt(total / 2) * 200 + 20;
var shape = shapes.add('', _getElementById("autoShapeType").value, x, y);
shape.style().hAlign = 1;
});
_getElementById("insertConnectShape").addEventListener('click', function() {
var sheet = spread.getActiveSheet(), shapes = sheet.shapes, total = shapes.all().length;
var x = 40 + (total % 2) * 250, y = parseInt(total / 2) * 200 + 20;
shapes.addConnector('', parseInt(_getElementById("connectShapeType").value), x, y, x + 200, y + 200);
});
}
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-shapes/dist/gc.spread.sheets.shapes.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 class="options-container">
<div class="option-row">
请从任意下拉菜单中选择一个形状,然后点击“添加”按钮,即可将该形状添加到 Spread 实例中。
</div>
<div class="option-row">
<label for='autoShapeType'>添加形状:</label>
<select id='autoShapeType' class="shapeSelect"></select>
<input type='button' id='insertShape' value="添加" />
<label for='connectShapeType'>添加连接形状:</label>
<select id='connectShapeType' class="shapeSelect"></select>
<input type='button' id='insertConnectShape' 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-left: 5px;
}
.divide-line {
width: 100%;
height: 1px;
background: #cbcbcb;
margin-top: 10px;
margin-bottom: 3px;
}
.title {
text-align: center;
font-weight: bold;
}
label {
display: block;
margin-top: 15px;
margin-bottom: 5px;
}
p {
padding: 2px 10px;
background-color: #F4F8EB;
}
input {
width: 160px;
margin-left: 10px;
display: inline;
}
input[type=button] {
width: 50px;
margin-left: 1px;
}
select {
width: 160px;
margin-left: 10px;
display: inline;
}
textarea {
width: 160px;
margin-left: 10px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}