概述
本 Demo 展示了 SpreadJS 中公式的核心功能,包括为单元格设置公式、使用自定义名称、设置数组公式,以及控制公式的显示方式。Demo 中创建了一个销售数据表,通过公式计算销售额和总计,并演示了自定义名称的实际应用场景。
实现思路
创建销售数据表格,包含产品名称、单价、数量等列
使用 formula 方法为单元格区域设置相对引用公式,自动计算每行的销售额
使用 addCustomName 方法为价格和数量列创建自定义名称
在公式中引用自定义名称,计算总计
添加单元格按钮,用于切换公式的显示状态(显示公式文本或计算结果)
提供交互式界面,允许用户动态设置单个单元格公式或数组公式
代码解析
为区域设置相对引用公式
这段代码为 D2:D6 区域设置公式,第二个参数 true 表示自动调整相对引用。SpreadJS 会根据每个单元格的位置自动调整公式中的单元格引用:D2 使用 B2C2,D3 使用 B3C3,以此类推。
添加自定义名称
使用 addCustomName 方法为单元格区域创建有意义的名称:
customName1 引用价格列($B$2:$B$6)
customName2 引用数量列($C$2:$C$6)
参数 (0, 0) 指定公式的基准行和基准列
在公式中使用自定义名称
使用 setFormula 方法在公式中引用自定义名称,计算价格和数量的总计。自定义名称使公式更易读且易于维护。
控制公式显示
通过设置 sheet.options.showFormulas 属性,可以切换显示公式文本或计算结果。这在调试或导出公式时非常有用。
动态设置公式
Demo 提供了交互式界面,用户可以输入公式、行索引、列索引等信息,动态设置单个单元格公式或数组公式。
运行效果
表格自动计算每行的销售额(D 列)
底部显示价格总计和数量总计,使用自定义名称简化公式
点击 "Show Formulas" 按钮可以在显示公式文本和计算结果之间切换
在右侧面板中输入参数,点击 "设置公式" 按钮可以为指定单元格设置公式
输入行数和列数时,会设置数组公式;否则设置单个单元格公式
API 参考
setFormula 方法
row:行索引
col:列索引
value:公式字符串(以 = 开头)
sheetArea:可选,指定工作表区域
formula 方法
value:可选,要设置的公式字符串
autoAdjustReference:可选,为 true 时自动调整相对引用
addCustomName 方法
name:自定义名称
formula:名称引用的公式或区域
baseRow、baseCol:基准位置
comment:可选,注释
isReadOnly:可选,是否只读
setArrayFormula 方法
row、col:起始位置
rowCount、colCount:区域大小
value:数组公式
sheetArea:可选,工作表区域
showFormulas 属性
布尔值,控制是否显示公式文本而不是计算结果。
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(_getElementById("ss"));
var sheet = spread.getSheet(0);
sheet.suspendPaint();
sheet.setArray(0, 0, [
["Product", "Item Price", "Quantity", "Sales"],
['Kraft Real Mayo', 5.71, 1],
['Smartfood Popcorn', 2.5, 4],
['Teddy Grahams Crackers', 35, 5],
['Parmesan Cheese', 14.89, 9],
['Planter Deluxe Whole Cashew', 8.52, 3],
['Total']
]);
sheet.setColumnWidth(0, 190);
sheet.setColumnWidth(1, 80);
sheet.setColumnWidth(2, 80);
sheet.getRange(1, 3, 5, 1).formula("B2*C2", true);
sheet.addCustomName('customName1', '=$B$2:$B$6', 0, 0);
sheet.addCustomName('customName2', '=$C$2:$C$6', 0, 0);
sheet.setFormula(6, 1, "=SUM(customName1)");
sheet.setFormula(6, 2, "=SUM(customName2)");
sheet.getRange(6, 0, 1, 4).foreColor('red');
sheet.setFormula(6, 3, "B7*C7");
var style = sheet.getStyle(4, 7) || new GC.Spread.Sheets.Style();;
style.cellButtons= [{
useButtonStyle: true,
caption: "Show Formulas",
width: 120,
command: function() {
sheet.options.showFormulas = !sheet.options.showFormulas;
if (sheet.options.showFormulas) {
style.cellButtons[0].imageType = GC.Spread.Sheets.ButtonImageType.ok;
} else {
style.cellButtons[0].imageType = GC.Spread.Sheets.ButtonImageType.none;
}
sheet.setStyle(4, 7, style);
},
}];
sheet.setStyle(4, 7, style);
sheet.setColumnWidth(7, 122);
sheet.resumePaint();
_getElementById('btnSetFormula').addEventListener('click', function() {
var sheet = spread.getActiveSheet();
if (_getElementById("formula").value) {
var formula = _getElementById("formula").value;
if (_getElementById("rowIndex").value && _getElementById("columnIndex").value) {
var rowIndex = parseInt(_getElementById("rowIndex").value);
var columnIndex = parseInt(_getElementById("columnIndex").value);
if (_getElementById("rowCount").value && _getElementById("columnCount").value) {
var rowCount = Math.max(parseInt(_getElementById("rowCount").value), 1);
var columnCount = Math.max(parseInt(_getElementById("columnCount").value), 1);
sheet.setArrayFormula(rowIndex, columnIndex, rowCount, columnCount, formula);
} else {
sheet.setFormula(rowIndex, columnIndex, formula);
}
}
}
});
};
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">
<p style="font-size:16px;padding:1px 0px">通过代码设置公式</p>
<p style="padding:2px 10px">指定公式单元格的行索引和列索引。设置行数和列数以指定要添加公式的行数和列数。将公式添加到“公式”框中,然后单击“设置公式”以应用该公式。</p>
<div class="option-row">
<label for="rowIndex">行索引:</label>
<input type="text" id="rowIndex" />
</div>
<div class="option-row">
<label for="columnIndex">列索引:</label>
<input type="text" id="columnIndex" />
</div>
<div class="option-row">
<label for="rowCount">行数:</label>
<input type="text" id="rowCount" />
</div>
<div class="option-row">
<label for="columnCount">列数:</label>
<input type="text" id="columnCount" />
</div>
<div class="option-row">
<label for="formula">公式:</label>
<input type="text" id="formula" value="=SUM(B2,B6)" />
<input type="button" id="btnSetFormula" value="设置公式" />
</div>
</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: 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 {
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;
}