概述
本 Demo 展示了如何在工作表中添加标签表单控件并设置文本内容。标签控件是表单控件的一种类型,用于显示静态文本,常用于表单标题、字段说明或操作提示。
实现思路
创建工作簿实例并暂停渲染
通过 sheet.shapes.addFormControl() 方法添加标签控件
使用 text() 方法设置标签显示的文本
代码解析
添加标签控件
这行代码在工作表中添加了一个标签控件。addFormControl 方法的参数依次为:
"label":控件的名称
GC.Spread.Sheets.Shapes.FormControlType.label:控件类型,指定为标签类型
50, 50:控件在工作表中的位置坐标 (x, y)
160, 30:控件的宽度和高度 (单位: 像素)
设置标签文本
使用 text() 方法设置标签显示的文本内容。该方法支持获取或设置文本,不传参数时返回当前文本,传入字符串参数时设置新文本。
运行效果
运行 Demo 后,工作表中会显示一个标签控件,位置在坐标 (50, 50) 处,尺寸为 160×30 像素,显示文本"我是一个标签"。标签控件默认具有标准样式,可以通过样式属性进一步自定义外观。
API 参考
addFormControl 方法
name:控件名称 (string)
formControlType:表单控件类型,标签使用 FormControlType.label
left、top:控件位置坐标 (可选)
width、height:控件尺寸 (可选)
text 方法
value:要设置的文本内容 (可选)
返回值: 不传参数时返回当前文本,传参数时设置文本
window.onload = function () {
var workbook = new GC.Spread.Sheets.Workbook(document.getElementById("ss"));
workbook.suspendPaint();
initSheet1(workbook.getSheet(0));
workbook.resumePaint();
};
function initSheet1(sheet) {
var label = sheet.shapes.addFormControl("label", GC.Spread.Sheets.Shapes.FormControlType.label, 50, 50, 160, 30);
label.text("我是一个标签");
}
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="spreadjs culture" content="zh-cn" />
<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$/zh/purejs/node_modules/@grapecity-software/spread-sheets-shapes/dist/gc.spread.sheets.shapes.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>
</body>
</html>
input[type="text"] {
width: 200px;
margin-right: 20px;
}
label {
display: inline-block;
width: 110px;
}
.sample-tutorial {
position: relative;
height: 100%;
overflow: hidden;
}
.sample-spreadsheets {
width: 100%;
height: 100%;
overflow: hidden;
float: left;
}
label {
display: block;
margin-bottom: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
width:216px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
code {
border: 1px solid #000;
}