创建一个按钮单元格,请参照下面的代码:
你可以使用很多公用接口来自定制按钮单元格。如果你想控制按钮在单元格中的位置,使用以下方法。
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="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-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">
<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;
}