概述
本 Demo 展示了 SpreadJS 单元格的基础操作,包括设置单元格值和公式、切换引用样式(A1 和 R1C1)、设置文本对齐方式,以及控制单元格内容溢出。通过这个 Demo 可以了解 SpreadJS 单元格操作的核心功能。
实现思路
使用 setValue 方法设置单元格的值
使用 setFormula 方法设置单元格公式,并使用 FORMULATEXT 函数显示公式文本
通过下拉框切换 spread.options.referenceStyle 属性,在 A1 和 R1C1 两种引用样式之间切换
使用 getCell(row, col).hAlign(value) 方法设置单元格的文本水平对齐方式
通过复选框控制 sheet.options.allowCellOverflow 属性,控制内容是否溢出
代码解析
设置单元格值和公式
这段代码演示了如何设置单元格的值和公式。setValue 方法接受行索引、列索引和值三个参数。setFormula 方法用于设置公式,这里使用 FORMULATEXT 函数来显示 C7 单元格的公式文本。
切换引用样式
引用样式决定了公式中单元格引用的表示方式。SpreadJS 支持两种引用样式:
A1 样式:使用列字母和行号,如 SUM(A4:A8)
R1C1 样式:使用 R 和 C 加上行号和列号,如 SUM(R[4]C[1]:R[8]C[1])
通过修改 spread.options.referenceStyle 属性可以切换引用样式,切换后需要调用 recalcAll() 方法重新计算公式。
设置文本对齐方式
hAlign 方法用于设置单元格内容的水平对齐方式,参数可以是 HorizontalAlign 枚举值或数字:
0 或 left:左对齐
1 或 center:居中对齐
2 或 right:右对齐
3 或 general:常规(默认)
4 或 centerContinuous:跨列居中
不同的对齐方式会影响单元格内容溢出的方向。左对齐时内容向右溢出,右对齐时内容向左溢出,居中对齐时内容向两侧溢出。
控制内容溢出
allowCellOverflow 属性控制当单元格内容较长时是否允许溢出到相邻的空单元格。默认值为 true,表示允许溢出。设置为 false 时,单元格内容将被截断显示。
运行效果
左侧表格展示了单元格值和公式的设置,通过下拉框可以切换 A1 和 R1C1 引用样式,切换后公式会自动转换为对应的引用格式
右侧选项区域的复选框控制是否允许单元格内容溢出
表格中展示了四种不同的文本对齐方式及其对内容溢出的影响:
右对齐:内容向左溢出
居中对齐:内容向两侧溢出
左对齐:内容向右溢出
跨列居中:内容居中显示
取消勾选"允许单元格内容溢出"复选框后,长文本将被截断显示
API 参考
setValue 方法
row:行索引
col:列索引
value:要设置的值
sheetArea:可选,工作表区域
ignoreRecalc:可选,是否忽略重新计算
setFormula 方法
row:行索引
col:列索引
formula:公式字符串
sheetArea:可选,工作表区域
hAlign 方法
value:水平对齐方式,可以是 HorizontalAlign 枚举值或数字(0-4)
ReferenceStyle 枚举
指定公式引用样式:
a1:A1 引用样式
r1c1:R1C1 引用样式
allowCellOverflow 属性
类型:boolean
默认值:true
说明:指示数据是否可以溢出到相邻的空单元格中
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById('ss'));
var spreadNS = GC.Spread.Sheets;
var sheet = spread.getSheet(0);
sheet.suspendPaint();
sheet.defaults.colWidth = 120;
sheet.setColumnWidth(1, 200);
sheet.setValue(0, 0, 12);
sheet.setValue(1, 0, 13);
sheet.setValue(2, 0, 14);
sheet.setValue(3, 0, 15);
sheet.setValue(5, 0, '引用样式');
sheet.setValue(6, 0, 'A1');
sheet.setValue(5, 1, '公式:');
sheet.setFormula(6, 1, '=FORMULATEXT(C7)');
sheet.setValue(5, 2, '结果:');
sheet.setFormula(6, 2, 'SUM(A4:A8)');
sheet.setValue(9, 0, '文本对齐方式');
sheet.setValue(10, 0, '右对齐');
sheet.setValue(11, 0, '居中对齐');
sheet.setValue(12, 0, '左对齐');
sheet.setValue(13, 0, '跨列居中');
sheet.getCell(10, 2).value('单元格内容溢出到右侧单元格').hAlign(3);
sheet.getCell(11, 2).value('单元格内容向左右两侧溢出').hAlign(1);
sheet.getCell(12, 2).value('单元格内容溢出到左侧单元格').hAlign(2);
sheet.getCell(13, 2).value('单元格内容向左右两侧溢出').hAlign(4);
sheet.resumePaint();
document.getElementById('referenceStyle').addEventListener('change', function() {
var referenceStyleValue = this.value;
var referenceStyle = spreadNS.ReferenceStyle[referenceStyleValue];
spread.options.referenceStyle = referenceStyle;
spread.getActiveSheet().recalcAll();
sheet.getCell(6, 0).value(referenceStyleValue.toLocaleLowerCase() === 'a1' ? 'A1' : 'R1C1');
});
document.getElementById('chkAllowCellOverFlow').addEventListener('click', function() {
var sheet = spread.getActiveSheet();
if (sheet) {
sheet.options.allowCellOverflow = this.checked;
}
});
};
<!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">
<div class="position">
<p>引用样式定义为 'A1' 和 'R1C1',工作表上的行和列会进行编号。</p>
<p>如果更改引用样式,公式中的引用方式也会随之改变。</p>
<label for="referenceStyle">引用样式:</label>
<select id="referenceStyle" style="margin: 0 20px 0 6px">
<option value="a1">A1</option>
<option value="r1c1">R1C1</option>
</select>
</div>
<div style="padding-top: 16px">
<input type="checkbox" id="chkAllowCellOverFlow" checked="checked" />
<label for="chkAllowCellOverFlow">允许单元格内容溢出</label>
</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: 5px;
margin-top: 10px;
}
label {
margin-bottom: 6px;
}
input {
padding: 4px 6px;
}
input[type=button] {
margin-top: 6px;
display: block;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}