概述
本 Demo 展示了如何为单元格设置默认值和默认公式。Demo 加载了一个预算分配表,为月度分配单元格设置了默认公式,同时提供了交互界面让用户设置和查看单元格的默认值。
实现思路
加载预设的 .sjs 文件作为基础表格
为月度分配单元格设置默认公式 =$B3/12 和 =$B4/12,实现自动分配年度预算
绑定选择改变事件,当用户选中单元格时显示其默认值
提供 UI 界面让用户输入默认值或公式并应用到选中的单元格
代码解析
设置默认公式
这段代码为 12 个月份列设置默认公式。defaultValue() 方法接受公式字符串作为参数,当单元格为空时,会自动计算并显示公式的结果。这里将年度预算除以 12 得到月度分配。
设置默认值
setDefaultValue(row, col, value) 方法为指定单元格设置默认值。如果传入的是公式字符串(以等号开头),则作为默认公式处理;否则作为默认值。convertValue() 函数会将用户输入转换为合适的数据类型(数字、布尔值、日期或字符串)。
获取默认值
getDefaultValue(row, col) 方法返回指定单元格的默认值或默认公式字符串。当用户选中单个单元格时,界面会显示其默认值。
运行效果
表格加载后,月度分配行(第 3、4 行)的单元格显示根据年度预算自动计算的值
用户可以编辑这些单元格,输入的数据不会覆盖默认公式
在右侧面板输入值或公式,点击"设置默认值"按钮应用到选中单元格
选中单元格时,"获取默认值"文本框会显示该单元格的默认值或公式
当单元格被清空时,会自动恢复显示默认值或重新计算公式结果
API 参考
setDefaultValue 方法
row:行索引
col:列索引
value:默认值或公式字符串(如 =A1*2)
getDefaultValue 方法
row:行索引
col:列索引
返回值:单元格的默认值或公式字符串
defaultValue 方法
value:可选,设置默认值或公式
返回值:不传参数时返回当前默认值,传参数时返回单元格对象(支持链式调用)
window.onload = function () {
let spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 1 });
spread.suspendPaint();
let sheet = spread.getActiveSheet();
let fbx = new GC.Spread.Sheets.FormulaTextBox.FormulaTextBox(document.getElementById('formula'), { rangeSelectMode: true });
fbx.workbook(spread);
initSpread(spread, fbx);
bindEvents(spread);
spread.resumePaint();
};
function bindEvents(spread) {
spread.bind(GC.Spread.Sheets.Events.SelectionChanged, function () {
getDefault(spread.getActiveSheet());
});
spread.bind(GC.Spread.Sheets.Events.ActiveSheetChanged, function () {
self.getDefault(spread.getActiveSheet());
});
}
function getDefault(sheet) {
let selections = sheet.getSelections(), showDefault = document.getElementById('showDefault');
if (!selections || selections.length === 0) {
showDefault.value = '';
return;
}
let sel = selections[0], row = sel.row, col = sel.col;
if (sel.rowCount === 1 && sel.colCount === 1) {
showDefault.value = sheet.getDefaultValue(row, col);
} else {
showDefault.value = '';
}
}
function convertValue(value) {
if (!value) {
return value;
}
// try to convert to number
let num = Number(value);
if (!isNaN(num)) {
return num;
}
// try to convert to boolean
if (value.toLowerCase() === "true" || value.toLowerCase() === "false") {
return value.toLowerCase() === "true";
}
// try to convert to date
let date = new Date(value);
if (!isNaN(date.getTime())) {
return date;
}
return value;
}
function initSpread(spread, fbx) {
fetch(getFileUrl()).then(res => res.blob()).then(file => {
spread.open(file, () => {
let sheet = spread.getActiveSheet();
sheet.suspendPaint();
sheet.suspendCalcService();
for (let i = 0; i < 12; i++) {
sheet.getCell(2, 3 + i, 3).defaultValue('=$B3/12');
sheet.getCell(3, 3 + i, 3).defaultValue('=$B4/12');
}
sheet.resumeCalcService();
sheet.resumePaint();
getDefault(spread.getActiveSheet());
});
});
document.getElementById('btnSetDefault').onclick = function () {
let value = document.getElementById('value').value;
let formula = fbx.text();
sheet = spread.getActiveSheet();
let selections = sheet.getSelections();
if (!selections || selections.length === 0) {
return;
}
sheet.suspendPaint();
let length = selections.length;
for (let i = 0; i < length; i++) {
let sel = selections[i],
rowIndex = sel.row,
colIndex = sel.col,
rowCount = sel.rowCount,
colCount = sel.colCount,
maxRow = rowIndex + rowCount,
maxColumn = colIndex + colCount,
r, c;
if (rowIndex !== -1 && colIndex !== -1) {
for (r = rowIndex; r < maxRow; r++) {
for (c = colIndex; c < maxColumn; c++) {
sheet.setDefaultValue(r, c, formula || convertValue(value));
}
}
}
}
sheet.resumePaint();
getDefault(sheet);
};
}
function getFileUrl() {
return '$DEMOROOT$/spread/source/data/defaultValue.sjs';
}
<!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$/zh/purejs/node_modules/@grapecity-software/spread-sheets-io/dist/gc.spread.sheets.io.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="option-row">
<p>
<span>您可以设置和获取活动单元格的默认值和公式。</span>
</p>
</div>
<div class="option-row">
<label for="value">默认值: </label>
<input id="value" />
</div>
<div class="option-row">
<label for="formula">默认公式: </label>
<div id="formula" spellcheck="false"
style="border: 1px solid #808080;width:100%;height:21px;overflow:hidden;"></div>
</div>
<div class="option-row">
<input type="button" id="btnSetDefault" value="设置默认值"
title="为选定项设置默认值和公式" />
</div>
<div class="option-row">
<label for="showDefault">获取默认值: </label>
<textarea id="showDefault" cols="31" readonly></textarea>
</div>
</div>
</div>
</body>
</html>
.sample {
position: relative;
height: 100%;
overflow: auto;
}
.sample::after {
display: block;
content: "";
clear: both;
}
.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: inline-block;
margin-bottom: 6px;
}
input {
padding: 4px;
width: 100%;
margin: 0 4px 4px 0;
box-sizing: border-box;
}
input[type=button] {
width: 30%;
}
p {
background-color: #F4F8EB;
padding: 4px;
}
p span {
display: block;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}