Spread.Sheets支持一下三种形状类型
基础形状: 基础形状可以单独使用。
线条形状: 线条形状即可以单独使用也可以链接其他基础形状。
分组形状: 分组形状并不是一个真实的形状,但是被用作一个形状的管理者,以便方便快捷的操作一组形状。
使用形状功能需要添加如下的JS文件到文档的Head部分:
你可以使用ShapeCollection类上的API来管理所有的形状:
你可以通过sheet.shapes.add方法创建一个基础形状:
sheet.shapes.add('autoShape', GC.Spread.Sheets.Shapes.AutoShapeType.heart, 100, 50, 100, 150);
你可以通过sheet.shapes.addConnector方法创建一个线条形状:
sheet.shapes.addConnector('connectorShape', GC.Spread.Sheets.Shapes.ConnectorType.elbow, 200, 50, 300, 200);
你可以根据形状的名字来获取或删除一个形状:
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">
Try selecting a shape from either of the drop-down menus and click the ‘Add’ button to add that shape to
the Spread instance.
</div>
<div class="option-row">
<label for='autoShapeType'>Add Shape: </label>
<select id='autoShapeType' class="shapeSelect"></select>
<input type='button' id='insertShape' value="Add" />
<label for='connectShapeType'>Add Connect Shape: </label>
<select id='connectShapeType' class="shapeSelect"></select>
<input type='button' id='insertConnectShape' value="Add" />
</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;
}