버튼 셀을 만들려면 다음 예를 따르십시오:
제공되는 많은 API를 사용하여 Button을 사용자 정의할 수 있습니다. 셀의 버튼 위치를 제어하려면 다음 메서드를 사용합니다.
marginTop: 버튼의 위쪽 여백을 셀에 상대적인 픽셀 수로 가져오거나 설정합니다.
marginRight: 버튼의 오른쪽 여백을 셀에 상대적인 픽셀 수로 가져오거나 설정합니다.
marginBottom: 버튼의 아래쪽 여백을 셀에 상대적인 픽셀 수로 가져오거나 설정합니다.
marginLeft: 버튼의 왼쪽 여백을 셀에 상대적인 픽셀 수로 가져오거나 설정합니다.
text 메서드를 사용하여 버튼의 내용을 가져오고 설정할 수 있습니다. buttonBackColor 메서드를 사용하여 버튼의 배경색을 가져오고 설정합니다. 예:
window.onload = function () {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
initSpread(spread);
};
function initSpread(spread) {
var spreadNS = GC.Spread.Sheets;
var sheet = spread.getSheet(0);
sheet.bind(spreadNS.Events.SelectionChanged, function () {
propertyChange(false);
});
sheet.suspendPaint();
sheet.setColumnWidth(2, 120);
sheet.setColumnWidth(1, 120);
sheet.setRowHeight(1, 50);
var b0 = new spreadNS.CellTypes.Button();
b0.text("Margin");
b0.marginLeft(15);
b0.marginTop(7);
b0.marginRight(15);
b0.marginBottom(7);
sheet.setCellType(1, 2, b0, spreadNS.SheetArea.viewport);
sheet.setValue(1, 1, "ButtonCellType");
sheet.resumePaint();
_getElementById("changeProperty").addEventListener('click',function () {
propertyChange(true);
});
function propertyChange(isSet) {
var sheet = spread.getActiveSheet();
var sels = sheet.getSelections();
if (sels && sels.length > 0) {
var sel = getActualRange(sels[0], sheet.getRowCount(), sheet.getColumnCount());
var buttonCellType = sheet.getCellType(sel.row, sel.col);
if (!(buttonCellType instanceof spreadNS.CellTypes.Button)) {
_getElementById("changeProperty").setAttribute("disabled",'disabled');
return;
}
if (!isSet) {
_getElementById("changeProperty").removeAttribute("disabled");
_getElementById("txtButtonCellMarginLeft").value=buttonCellType.marginLeft();
_getElementById("txtButtonCellMarginTop").value=buttonCellType.marginTop();
_getElementById("txtButtonCellMarginRight").value=buttonCellType.marginRight();
_getElementById("txtButtonCellMarginBottom").value=buttonCellType.marginBottom();
_getElementById("txtButtonCellText").value=buttonCellType.text();
_getElementById("txtButtonCellBackColor").value=buttonCellType.buttonBackColor();
} else {
buttonCellType.marginLeft(parseInt(_getElementById("txtButtonCellMarginLeft").value));
buttonCellType.marginTop(parseInt(_getElementById("txtButtonCellMarginTop").value));
buttonCellType.marginRight(parseInt(_getElementById("txtButtonCellMarginRight").value));
buttonCellType.marginBottom(parseInt(_getElementById("txtButtonCellMarginBottom").value));
buttonCellType.text(_getElementById("txtButtonCellText").value);
buttonCellType.buttonBackColor(_getElementById("txtButtonCellBackColor").value);
}
}
sheet.repaint();
}
function getActualRange(range, maxRowCount, maxColCount) {
var row = range.row < 0 ? 0 : range.row;
var col = range.col < 0 ? 0 : range.col;
var rowCount = range.rowCount < 0 ? maxRowCount : range.rowCount;
var colCount = range.colCount < 0 ? maxColCount : range.colCount;
return new spreadNS.Range(row, col, rowCount, colCount);
}
}
function _getElementById(id){
return document.getElementById(id);
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta name="spreadjs culture" content="ko-kr"/>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-resources-ko/dist/gc.spread.sheets.resources.ko.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">
<label>Select the button cell in Spread and edit its options with these text boxes.</label>
<div class="option-row">
<label>margin-left:</label>
<input type="text" id="txtButtonCellMarginLeft" />
<label>margin-top:</label>
<input type="text" id="txtButtonCellMarginTop" />
</div>
<div class="option-row">
<label>margin-right:</label>
<input type="text" id="txtButtonCellMarginRight" />
<label>margin-bottom:</label>
<input type="text" id="txtButtonCellMarginBottom" />
</div>
<div class="option-row">
<label>text:</label>
<input type="text" id="txtButtonCellText" />
<label>backColor:</label>
<input type="text" id="txtButtonCellBackColor" />
</div>
<div class="option-row">
<label></label>
<input type="button" id="changeProperty" value="Update"/>
</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;
overflow: auto;
padding: 12px;
height: 100%;
box-sizing: border-box;
background: #fbfbfb;
}
.option-row {
padding-bottom: 8px;
}
label {
padding-bottom: 4px;
display: block;
}
input {
width: 100%;
padding: 4px 8px;
box-sizing: border-box;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}