概述
本 Demo 展示了如何创建、管理和使用自定义名称。Demo 预置了两个自定义名称示例,并提供了交互式界面,允许用户选择单元格区域来添加新的自定义名称,设置名称、注释和只读属性,同时可以查看所有已定义的自定义名称。
实现思路
初始化工作表并设置基础数据和格式
添加两个预置的自定义名称,分别引用单元格和区域
在单元格中使用自定义名称作为公式引用
显示所有已定义的自定义名称列表
绑定单元格点击事件,当点击已定义名称的区域时自动填充该名称信息
提供 UI 界面,允许用户选择区域并添加新的自定义名称
代码解析
添加自定义名称
addCustomName 方法用于添加自定义名称,参数包括:
name:自定义名称
formula:引用的公式或区域(如 =$B$3 或 =$C$3:$D$4)
baseRow、baseCol:基准行和列
comment(可选):注释信息
isReadOnly(可选):是否只读
在公式中使用自定义名称
自定义名称可以直接在公式中引用,就像使用单元格引用一样。
获取所有自定义名称
getCustomNames() 返回所有自定义名称信息的数组,每个 NameInfo 对象包含名称、表达式、注释等信息。
动态添加自定义名称
用户选择区域后,将选区转换为公式字符串,然后添加为自定义名称。
自动填充已定义名称信息
当点击单元格时,检查该区域是否已被定义为自定义名称,如果是则自动填充名称信息。
运行效果
工作表预置了两个自定义名称:customName1(引用单元格 B3)和 customName2(引用区域 C3:D4)
单元格 B6 使用公式 =customName1,显示 B3 的值
单元格 C6 使用公式 =sum(customName2),计算 C3:D4 区域的和
右侧面板显示所有已定义的自定义名称及其引用
选择任意区域后,可以输入名称、注释和勾选只读属性,点击"添加自定义名称"按钮创建新名称
点击已定义名称的区域时,自动填充该名称的信息到输入框
API 参考
addCustomName 方法
name:自定义名称
formula:公式或区域引用
baseRow、baseCol:基准行列位置
comment(可选):注释
isReadOnly(可选):是否只读
getCustomNames 方法
返回 NameInfo[] 数组,包含所有自定义名称信息。
NameInfo 对象常用方法
getName():获取名称
getComment():获取注释
isReadOnly():获取或设置只读状态
getExpression():获取表达式对象
window.onload = initFunction;
function initFunction() {
var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 1});
spread.suspendPaint();
var sheet = spread.getActiveSheet();
sheet.setColumnWidth(1, 100);
sheet.setValue(2, 1, "示例范围:");
sheet.setValue(2, 2, 1);
sheet.setValue(2, 3, 2);
sheet.setValue(3, 2, 3);
sheet.setValue(3, 3, 4);
// add custom name
sheet.addCustomName("customName1", "=$B$3", 0, 0, "这是一个自定义名称!");
sheet.setFormula(5, 1, "=customName1");
sheet.addCustomName("customName2", "=$C$3:$D$4", 0, 0, "这是另一个自定义名称!");
sheet.setFormula(5, 2, "=sum(customName2)");
printNames(sheet);
document.getElementById('setNameInfo').onclick = function () {
var sheet = spread.getActiveSheet();
var name = document.getElementById('setname').value;
if (name === '') {
alert("名称不能为空");
return;
}
var comment = document.getElementById('setComment').value;
var isReadOnly = document.getElementById('setIsReadOnly').checked;
var formula = GC.Spread.Sheets.CalcEngine.rangeToFormula(sheet.getSelections()[0], 0, 0, GC.Spread.Sheets.CalcEngine.RangeReferenceRelative.allAbsolute);
sheet.addCustomName(name, formula, 0, 0, comment, isReadOnly);
printNames(sheet);
};
function printNames(sheet) {
var namesInfo = sheet.getCustomNames();
var str = "名称\t\t引用\n";
namesInfo.forEach(function (nameInfo) {
var name = nameInfo.getName();
var reference = GC.Spread.Sheets.CalcEngine.rangeToFormula(nameInfo.getExpression().getRange());
str += name + " " + reference + '\n';
});
document.getElementById('allNameInfo').value = str;
}
sheet.bind(GC.Spread.Sheets.Events.CellClick, function (sender, args) {
var sheet = args.sheet;
var selection = sheet.getSelections()[0];
document.getElementById('setname').value = '';
document.getElementById('setComment').value = '';
document.getElementById('setIsReadOnly').checked = false;
document.getElementById('reference').value = GC.Spread.Sheets.CalcEngine.rangeToFormula(selection);
var customNames = sheet.getCustomNames();
for (var i = 0; i < customNames.length; i++) {
var customName = customNames[i];
var range = customName.getExpression().getRange();
if (range.row === selection.row && range.col === selection.col
&& range.rowCount === selection.rowCount && range.colCount === selection.colCount) {
var name = customName.getName();
var comment = customName.getComment();
var isReadOnly = customName.isReadOnly();
document.getElementById('setname').value = name;
document.getElementById('setComment').value = comment;
document.getElementById('setIsReadOnly').checked = !!isReadOnly;
break;
}
}
});
spread.resumePaint();
}
<!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">
<strong class="sp-demo-HeaderTitle">设置:</strong>
<p style="padding:2px 10px">选择一个区域以添加自定义名称。</p>
<div id="settingsDiv" style="margin-top: 10px">
<div class="option-row">
<label for="setName">名称:</label>
<input type="text" id="setname" placeholder="名称不能为空" />
</div>
<div class="option-row">
<label for="setComment">注释:</label>
<input type="text" id="setComment" />
</div>
<div class="option-row">
<label for="setIsReadOnly">只读:</label>
<input type="checkbox" id="setIsReadOnly" />
</div>
<div class="option-row">
<label for="reference">引用:</label>
<input type="text" id="reference" />
</div>
<div class="option-row">
<input type="button" id="setNameInfo" value="添加自定义名称"
style="width:125px; margin-left: 10px" />
</div>
<div class="option-row">
<label for="reference">所有自定义名称:</label>
<textarea rows="5" cols="30" id="allNameInfo"></textarea>
</div>
</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;
}
input {
display:block;
width: 100%;
margin: 8px 0;
box-sizing: border-box;
}
input[type=checkbox] {
display: inline-block;
width: initial;
margin: 0;
vertical-align: middle;
}
label, input {
padding: 4px 6px;
}
body {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}