概述
本 Demo 演示 FILTERXML 函数的基本用法。工作表中先放入一段 XML 书目数据,再用多条公式提取标题、作者、价格和分类;底部还提供一个可编辑 XPath 查询,用来观察公式结果如何随查询条件变化。
实现思路
创建 Workbook,并开启动态数组支持。
暂停绘制和计算服务,批量写入 XML、标签、样式和公式。
把 XML 文本放在 B2,并设置自动换行和醒目的输入底色。
使用固定 XPath 提取各类节点,再用 B10 中的 XPath 作为自定义查询条件。
恢复计算和绘制,让所有 FILTERXML 结果一次性计算并显示。
代码解析
动态数组支持会让 FILTERXML 的多结果输出自动溢出到相邻单元格,适合展示多个书名、作者或分类。
XML 源数据被写入 B2,后续所有公式都引用这个单元格作为查询源。
前四条公式展示固定 XPath 的提取结果,最后一条公式读取用户输入的 XPath,因此可以用来测试不同查询条件。
运行效果
工作表中会显示一份 XML 书目样例。
标题、作者、价格和分类会分别溢出显示到结果区。
修改 B10 的 XPath 后,底部查询结果会重新计算。
示例中高亮输入区域和表头样式,便于区分源数据、查询条件和输出结果。
API 参考
spread.options.allowDynamicArray:允许公式结果以动态数组方式溢出。
sheet.setFormula(...):向单元格写入 FILTERXML 公式。
FILTERXML(xml, xpath):根据 XPath 从 XML 文本中提取匹配内容。
spread.suspendCalcService() / spread.resumeCalcService():批量写入公式时暂停和恢复计算服务。
window.onload = function() {
var spread = new GC.Spread.Sheets.Workbook(_getElementById("ss"));
spread.options.allowDynamicArray = true;
initSpread(spread);
};
function initSpread(spread) {
spread.setSheetCount(1);
spread.suspendPaint();
spread.suspendCalcService();
initSheet1(spread.getSheet(0));
spread.resumeCalcService();
spread.resumePaint();
}
function initSheet1(sheet) {
var xmlData = [
'<catalog>',
'<book category="小说"><title>失落王国</title><author>艾娃·斯通</author><price>18.50</price></book>',
'<book category="科学"><title>数据模式</title><author>李欧·金</author><price>24.00</price></book>',
'<book category="历史"><title>王者之城</title><author>米娅·陈</author><price>21.75</price></book>',
'</catalog>'
].join('');
sheet.suspendPaint();
sheet.name('FILTERXML');
sheet.setColumnWidth(0, 108);
sheet.setColumnWidth(1, 520);
sheet.setColumnWidth(2, 160);
sheet.setColumnWidth(3, 100);
sheet.setColumnWidth(4, 120);
sheet.setRowHeight(1, 90);
sheet.setValue(0, 1, '使用 XPath 从 XML 中提取值');
sheet.setValue(1, 0, 'XML 源');
sheet.setValue(1, 1, xmlData);
sheet.setValue(3, 1, '标题');
sheet.setValue(3, 2, '作者');
sheet.setValue(3, 3, '价格');
sheet.setValue(3, 4, '分类');
sheet.setValue(9, 0, 'XPath 查询');
sheet.setValue(9, 1, '//book[price>20]/title');
sheet.setValue(10, 0, '试一试');
sheet.setValue(10, 1, '//book[price>20 and @category!="科学"]/title');
sheet.setValue(11, 1, '查询结果');
var titleStyle = new GC.Spread.Sheets.Style();
titleStyle.font = 'bold 14pt Calibri';
sheet.setStyle(0, 1, titleStyle);
var labelStyle = new GC.Spread.Sheets.Style();
labelStyle.font = 'bold 11pt Calibri';
sheet.setStyle(1, 0, labelStyle);
sheet.setStyle(9, 0, labelStyle);
sheet.setStyle(10, 0, labelStyle);
var xmlInputStyle = new GC.Spread.Sheets.Style();
xmlInputStyle.backColor = '#FEF3CD';
sheet.setStyle(1, 1, xmlInputStyle);
sheet.getCell(1, 1).wordWrap(true);
var queryInputStyle = new GC.Spread.Sheets.Style();
queryInputStyle.backColor = '#FEF3CD';
sheet.setStyle(9, 1, queryInputStyle);
var headerStyle = new GC.Spread.Sheets.Style();
headerStyle.backColor = 'rgb(222,235,246)';
headerStyle.font = 'bold 11pt Calibri';
headerStyle.hAlign = 1;
var borderBottom = new GC.Spread.Sheets.LineBorder();
borderBottom.color = 'black';
borderBottom.style = GC.Spread.Sheets.LineStyle.thin;
headerStyle.borderBottom = borderBottom;
sheet.setStyle(3, 1, headerStyle);
sheet.setStyle(3, 2, headerStyle);
sheet.setStyle(3, 3, headerStyle);
sheet.setStyle(3, 4, headerStyle);
sheet.setStyle(11, 1, headerStyle);
sheet.setFormula(4, 1, '=FILTERXML($B$2,"//book/title")');
sheet.setFormula(4, 2, '=FILTERXML($B$2,"//book/author")');
sheet.setFormula(4, 3, '=FILTERXML($B$2,"//book/price")');
sheet.setFormula(4, 4, '=FILTERXML($B$2,"//book/@category")');
sheet.setFormula(12, 1, '=FILTERXML($B$2,$B$10)');
sheet.resumePaint();
}
function _getElementById(id) {
return document.getElementById(id);
}
<!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$/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;
}