SpreadJSには、次の3種類のシェイプがあります。
AutoShape: オートシェイプは単独で使用できます。
ConnectorShape: コネクタシェイプは単独で使用することも、他のシェイプと接続して使用することもできます。
GroupShape: グループシェイプ自体は実質的なシェイプではなく、複数のシェイプを簡単かつ短時間でグループ処理するための管理機能として使用します。
シェイプ機能を使用するには、文書のheadセクションに、jsファイルへのリンクを追加します。
ShapeCollection APIを使用することで、シート内のすべてのシェイプを管理できます。
オートシェイプを作成するには、次に示すように、sheet.shapes.addメソッドを使用します。
コネクタシェイプを作成するには、次に示すように、sheet.shapes.addConnectorメソッドを使用します。
名前を指定してシェイプを取得または設定するには、次のコードを使用します。
ShapeBase APIを使用することで、すべてのシェイプの各プロパティをカスタマイズできます。
allowMove: シェイプを移動可能にするかどうかを取得または設定します。
allowResize: シェイプをサイズ変更可能にするかどうかを取得または設定します。
allowRotate: シェイプを回転可能にするかどうかを取得または設定します。
showHandle: シェイプのハンドルを表示するかどうかを取得または設定します。
isSelected: このシェイプが選択されているかどうかを取得または設定します。
width/height: シェイプの幅または高さを取得または設定します。
x/y: シェイプの水平方向または垂直方向の位置を取得または設定します。
Shape APIを使用することで、自動シェイプの各プロパティをカスタマイズできます。
rotate: シェイプの回転角度を取得または設定します(度単位)。
text: シェイプのテキストを取得または設定します。
style: シェイプのスタイルを取得または設定します。
ConnectorShape APIを使用することで、コネクタシェイプの各プロパティをカスタマイズできます。
style: コネクタシェイプのスタイルを取得または設定します。
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
initSpread(spread);
initEvent(spread);
};
var lineCapStyle = {
flat: 2,
square: 1,
round: 0
};
var lineJoinStyle = {
round: 0,
miter: 1,
bevel: 2
};
var lineDashStyle = {
solid: 0,
squareDot: 1,
dash: 2,
longDash: 3,
dashDot: 4,
longDashDot: 5,
longDashDotDot: 6,
sysDash: 7,
sysDot: 8,
sysDashDot: 9,
dashDotDot: 10
};
var arrowheadLength = {
"short" : 0,
"medium" : 1,
"long" : 2
};
var arrowheadWidth = {
"narrow" : 0,
"medium" : 1,
"wide" : 2
};
var horizontalAlign = {
left: 0,
center: 1,
right: 2
};
var verticalAlign = {
top: 0,
center: 1,
bottom: 2
};
var activeShape;
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) {
setShapePropVisibility("none");
setConnectorPropVisibility("none");
setBorderPropVisibility("none");
fillShapeTypeList(GC.Spread.Sheets.Shapes.AutoShapeType, _getElementById("autoShapeType"));
fillShapeTypeList(GC.Spread.Sheets.Shapes.ConnectorType, _getElementById("connectShapeType"));
fillShapeTypeList(lineDashStyle, _getElementById("borderLineStyle"));
fillShapeTypeList(lineCapStyle, _getElementById("borderCapLineStyle"));
fillShapeTypeList(lineJoinStyle, _getElementById("borderJoinLineStyle"));
fillShapeTypeList(horizontalAlign, _getElementById("txtHAlign"));
fillShapeTypeList(verticalAlign, _getElementById("txtVAlign"));
fillShapeTypeList(GC.Spread.Sheets.Shapes.ArrowheadStyle, _getElementById("beginArrowheadStyle"));
fillShapeTypeList(GC.Spread.Sheets.Shapes.ArrowheadStyle, _getElementById("endArrowheadStyle"));
fillShapeTypeList(arrowheadLength, _getElementById("beginArrowheadLength"));
fillShapeTypeList(arrowheadLength, _getElementById("endArrowheadLength"));
fillShapeTypeList(arrowheadWidth, _getElementById("beginArrowheadWidth"));
fillShapeTypeList(arrowheadWidth, _getElementById("endArrowheadWidth"));
_getElementById("txtText").value = "abcdefgHIJKLMN\n" + "12356789\n" + "SpreadJSSpreadJS";
spread.getActiveSheet().shapes.add("heart", GC.Spread.Sheets.Shapes.AutoShapeType.heart, 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 setShapePropVisibility(display) {
var shapeFillProp = _getElementById("shapeFillProp");
var shapeTxtProp = _getElementById("shapeTxtProp");
shapeFillProp.style.display = display;
shapeTxtProp.style.display = display;
}
function setConnectorPropVisibility(display) {
var connectorProp = _getElementById("connectorProp");
connectorProp.style.display = display;
}
function setBorderPropVisibility(display) {
var borderProp = _getElementById("borderProp");
borderProp.style.display = display;
}
function initEvent(spread) {
var spreadNS = GC.Spread.Sheets;
var sheet = spread.getActiveSheet();
sheet.bind(spreadNS.Events.ShapeSelectionChanged, function () {
var selectedShape = sheet.shapes.all().filter(function(sp){
return sp.isSelected();
});
var isShapeSelected = false, isConnectorSelected = false;
if (selectedShape.length > 0) {
selectedShape.forEach(function (shape) {
if (!isShapeSelected && shape instanceof spreadNS.Shapes.Shape) {
isShapeSelected = true;
} else if (!isConnectorSelected && shape instanceof spreadNS.Shapes.ConnectorShape) {
isConnectorSelected = true;
}
});
if (isShapeSelected) {
setShapePropVisibility("block");
} else {
setShapePropVisibility("none");
}
if (isConnectorSelected) {
setConnectorPropVisibility("block");
} else {
setConnectorPropVisibility("none");
}
setBorderPropVisibility("block");
} else {
setShapePropVisibility("none");
setConnectorPropVisibility("none");
setBorderPropVisibility("none");
}
});
_getElementById("rotateActionBtn").addEventListener('click', function() {
_handleShapeStyleSetting(spread, 'rotate', _getElementById("txtRotate"));
});
_getElementById("backgroundActionBtn").addEventListener('click', function() {
_handleShapeStyleSetting(spread, 'background', _getElementById("txtFillColor"));
});
_getElementById("transparencyActionBtn").addEventListener('click', function() {
_handleShapeStyleSetting(spread, 'transparency', _getElementById("txtFillTransparency"));
});
_getElementById("textActionBtn").addEventListener('click', function() {
_handleShapeStyleSetting(spread, 'text', _getElementById("txtText"));
});
_getElementById("colorActionBtn").addEventListener('click', function() {
_handleShapeStyleSetting(spread, 'color', _getElementById("txtTextColor"));
});
_getElementById("transparencyTxtActionBtn").addEventListener('click', function() {
_handleShapeStyleSetting(spread, 'transparencyTxt', _getElementById("txtTextTransparency"));
});
_getElementById("fontActionBtn").addEventListener('click', function() {
_handleShapeStyleSetting(spread, 'font', _getElementById("txtFont"));
});
_getElementById("hAlignActionBtn").addEventListener('click', function() {
_handleShapeStyleSetting(spread, 'hAlign', _getElementById("txtHAlign"));
});
_getElementById("vAlignActionBtn").addEventListener('click', function() {
_handleShapeStyleSetting(spread, 'vAlign', _getElementById("txtVAlign"));
});
_getElementById("borderColorActionBtn").addEventListener('click', function() {
_handleShapeBorderAction(spread, 'color', _getElementById("borderColorStyle"));
});
_getElementById("borderTransparencyActionBtn").addEventListener('click', function() {
_handleShapeBorderAction(spread, 'transparency', _getElementById("borderTransparencyStyle"));
});
_getElementById("borderWidthActionBtn").addEventListener('click', function() {
_handleShapeBorderAction(spread, 'width', _getElementById("borderWidthStyle"));
});
_getElementById("borderLineStyleActionBtn").addEventListener('click', function() {
_handleShapeBorderAction(spread, 'lineStyle', _getElementById("borderLineStyle"));
});
_getElementById("borderCapLineActionBtn").addEventListener('click', function() {
_handleShapeBorderAction(spread, 'capType', _getElementById("borderCapLineStyle"));
});
_getElementById("borderJoinLineActionBtn").addEventListener('click', function() {
_handleShapeBorderAction(spread, 'joinType', _getElementById("borderJoinLineStyle"));
});
_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);
_setShapeStyle(shape, '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);
});
_getElementById("beginArrowheadStyleActionBtn").addEventListener('click', function() {
_handleConnectorShapeStyleSetting(spread, 'beginStyle', _getElementById("beginArrowheadStyle"));
});
_getElementById("beginArrowheadWidthActionBtn").addEventListener('click', function() {
_handleConnectorShapeStyleSetting(spread, 'beginWidth', _getElementById("beginArrowheadWidth"));
});
_getElementById("beginArrowheadLengthActionBtn").addEventListener('click', function() {
_handleConnectorShapeStyleSetting(spread, 'beginLength', _getElementById("beginArrowheadLength"));
});
_getElementById("endArrowheadStyleActionBtn").addEventListener('click', function() {
_handleConnectorShapeStyleSetting(spread, 'endStyle', _getElementById("endArrowheadStyle"));
});
_getElementById("endArrowheadWidthActionBtn").addEventListener('click', function() {
_handleConnectorShapeStyleSetting(spread, 'endWidth', _getElementById("endArrowheadWidth"));
});
_getElementById("endArrowheadLengthActionBtn").addEventListener('click', function() {
_handleConnectorShapeStyleSetting(spread, 'endLength', _getElementById("endArrowheadLength"));
});
}
function _handleShapeStyleSetting(spread, action, valueDom) {
var sheet = spread.getActiveSheet();
activeShape = sheet.shapes.all().filter(function(sp){
return sp.isSelected();
});
if (activeShape.length > 0) {
activeShape.forEach(function (shape) {
if (shape instanceof GC.Spread.Sheets.Shapes.Shape) {
_setShapeStyle(shape, action, valueDom.value);
}
});
sheet.repaint();
}
}
function _setShapeStyle(shape, action, value) {
if (action === 'rotate') {
shape.rotate(value);
} else if (action === 'text') {
shape.text(value);
} else {
var shapeStyle = shape.style();
if (action === 'background') {
shapeStyle.fill.color = value;
} else if (action === 'transparency') {
shapeStyle.fill.transparency = value;
} else if (action ==='color') {
shapeStyle.textEffect.color = value;
} else if (action === 'transparencyTxt') {
shapeStyle.textEffect.transparency = value;
} else if (action === 'font') {
shapeStyle.textEffect.font = value;
} else if (action === 'hAlign') {
shapeStyle.textFrame.hAlign = parseInt(value);
} else if (action === 'vAlign') {
shapeStyle.textFrame.vAlign = parseInt(value);
}
shape.style(shapeStyle);
}
}
function _handleShapeBorderAction(spread, action, valueDom) {
var sheet = spread.getActiveSheet();
activeShape = sheet.shapes.all().filter(function (sp) {
return sp.isSelected();
});
if (activeShape) {
activeShape.forEach(function (shape) {
var shapeStyle = shape.style();
shapeStyle.line[action] = valueDom.value;
shape.style(shapeStyle);
});
sheet.repaint();
}
}
function _handleConnectorShapeStyleSetting(spread, action, valueDom) {
var sheet = spread.getActiveSheet();
activeShape = getActiveConnectorShape(sheet);
if(activeShape.length > 0) {
activeShape.forEach(function (shape) {
_setConnectorShapeStyle(shape, action, parseInt(valueDom.value));
});
sheet.repaint();
}
}
function _setConnectorShapeStyle(shape, action, value) {
var shapeStyle = shape.style();
var shapeStyleLine = shapeStyle.line;
switch (action) {
case "beginStyle": {
shapeStyleLine.beginArrowheadStyle = value;
break;
}
case "beginWidth": {
shapeStyleLine.beginArrowheadWidth = value;
break;
}
case "beginLength": {
shapeStyleLine.beginArrowheadLength = value;
break;
}
case "endStyle": {
shapeStyleLine.endArrowheadStyle = value;
break;
}
case "endWidth": {
shapeStyleLine.endArrowheadWidth = value;
break;
}
case "endLength": {
shapeStyleLine.endArrowheadLength = value;
break;
}
}
shape.style(shapeStyle);
}
function _getElementById(id){
return document.getElementById(id);
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta name="spreadjs culture" content="ja-jp" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets-resources-ja/dist/gc.spread.sheets.resources.ja.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">
ドロップダウンメニューからシェイプを選択し、「追加」ボタンをクリックしてスプレッドシートに追加してください。
</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 id="divideLine" class="divide-line"></div>
<div id="borderProp" class="option-row">
<label class="title">シェイプの枠線</label>
<label>枠線の色:</label>
<input id="borderColorStyle" data-attr="color" type="color" value="#00A2E8"/>
<input type="button" id='borderColorActionBtn' value="設定"/>
<label>枠線の透過性:</label>
<input id="borderTransparencyStyle" data-attr="transparency" type="text" value="0.5"/>
<input type="button" id='borderTransparencyActionBtn' value="設定"/>
<label>枠線の幅:</label>
<input id="borderWidthStyle" data-attr="width" type="number" value="1"/>
<input type="button" id='borderWidthActionBtn' value="設定"/>
<label>枠線の線種:</label>
<select id="borderLineStyle" data-attr="lineStyle"></select>
<input type="button" id='borderLineStyleActionBtn' value="設定"/>
<label>枠線の先端のスタイル:</label>
<select id="borderCapLineStyle" data-attr="capType"></select>
<input type="button" id='borderCapLineActionBtn' value="設定"/>
<label>枠線の結合点のスタイル:</label>
<select id="borderJoinLineStyle" data-attr="joinType"></select>
<input type="button" id='borderJoinLineActionBtn' value="設定"/>
</div>
<div id="shapeFillProp" class="option-row">
<div class="divide-line"></div>
<label class="title">シェイプの塗りつぶしと回転</label>
<label>塗りつぶしの色:</label>
<input id='txtFillColor' type="color" value="#00A2E8"/>
<input type="button" id='backgroundActionBtn' value="設定"/>
<label>塗りつぶしの透過性:</label>
<input id='txtFillTransparency' type="text" value="0.5"/>
<input type="button" id='transparencyActionBtn' value="設定"/>
<label for='txtRotate'>回転角度:</label>
<input id='txtRotate' type="number" value="30" min="0" max="360"/>
<input type="button" id='rotateActionBtn' value="設定"/>
</div>
<div id="shapeTxtProp" class="option-row">
<div class="divide-line"></div>
<label class="title">シェイプのテキスト</label>
<label>テキスト:</label>
<textarea id='txtText' type="text" placeholder="Shape text" rows="3"></textarea>
<input type="button" id='textActionBtn' value="設定"/>
<label>テキストの色:</label>
<input id='txtTextColor' type="color" value="#FFFF00"/>
<input type="button" id='colorActionBtn' value="設定"/>
<label>テキストの透過性:</label>
<input id='txtTextTransparency' type="text" value="0.5"/>
<input type="button" id='transparencyTxtActionBtn' value="設定"/>
<label>テキストのフォント:</label>
<input id='txtFont' type="text" value="bold 15px Georgia"/>
<input type="button" id='fontActionBtn' value="設定"/>
<label>水平方向の配置:</label>
<select id="txtHAlign"></select>
<input type="button" id='hAlignActionBtn' value="設定"/>
<label>垂直方向の配置:</label>
<select id="txtVAlign"></select>
<input type="button" id='vAlignActionBtn' value="設定"/>
</div>
<div id="connectorProp" class="option-row">
<div class="divide-line"></div>
<label class="title">シェイプの矢印</label>
<label for="beginArrowheadStyle">始点矢印のスタイル:</label>
<select id='beginArrowheadStyle' class="shapeSelect"></select>
<input id="beginArrowheadStyleActionBtn" type="button" data-attr="style" data-isBegin=true class='value arrow-action-button' value="設定"/>
<label>始点矢印の幅:</label>
<select id="beginArrowheadWidth" class="shapeSelect"></select>
<input id="beginArrowheadWidthActionBtn" type="button" data-attr="width" data-isBegin=true class='value arrow-action-button' value="設定"/>
<label>始点矢印の長さ:</label>
<select id="beginArrowheadLength" class="shapeSelect"></select>
<input id="beginArrowheadLengthActionBtn" type="button" data-attr="length" data-isBegin=true class='value arrow-action-button' value="設定"/>
<label for="endArrowheadStyle">終点矢印のスタイル:</label>
<select id='endArrowheadStyle' class="shapeSelect"></select>
<input id="endArrowheadStyleActionBtn" type="button" data-attr="style" data-isBegin=false
class='value arrow-action-button' value="設定"/>
<label>終点矢印の幅:</label>
<select id="endArrowheadWidth" class="shapeSelect"></select>
<input id="endArrowheadWidthActionBtn" type="button" data-attr="width" data-isBegin=false class='value arrow-action-button' value="設定"/>
<label>終点矢印の長さ:</label>
<select id="endArrowheadLength" class="shapeSelect"></select>
<input id="endArrowheadLengthActionBtn" type="button" data-attr="length" data-isBegin=false class='value arrow-action-button' 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: lavender;
}
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;
}