搜索标签

如果单元格的标签中存储的是字符串,那么它就是可以被搜索的。搜索的顺序可以设置成按行搜索或者按列搜索。

首先预备一个 SearchCondition ,然后将 searchTarget 设置成 SearchFoundFlags.cellTag 。然后就可以搜索标签了。 通过 sheet.search(searchCondition) 方法来完成搜索。 搜索结果的 foundRowIndex 和 foundColumnIndex 参数说明了第一个匹配的单元格的位置;如果没有找到那么结果都是-1。示例代码如下:
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 1}); initSpread(spread); }; function initSpread(spread) { var spreadNS = GC.Spread.Sheets, sheet = spread.sheets[0]; sheet.suspendPaint(); // add tags for demo use sheet.getCell(0, 0).tag("A1 tag"); sheet.getCell(1, 6).tag("demo tag for G2"); sheet.setTag(3, 1, "B4 tag demo"); sheet.getCell(0, 0).backColor("#E3E3E3"); sheet.getCell(1, 6).backColor("#E3E3E3"); sheet.getCell(3, 1).backColor("#E3E3E3"); var r, c; for (r = 7; r <= 10; r++) { for (c = 0; c <= 2; c++) { sheet.setTag(r, c, "Cell tag " + String.fromCharCode(65 + c) + (r + 1)); sheet.getCell(r, c).backColor("#E3E3E3"); } } for (r = 5; r <= 8; r++) { for (c = 5; c <= 7; c++) { sheet.setTag(r, c, "demo tag " + String.fromCharCode(65 + c) + (r + 1)); sheet.getCell(r, c).backColor("#E3E3E3"); } } sheet.bind(spreadNS.Events.EnterCell, function () { var tag = sheet.getTag(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex()); if (tag == null) { document.getElementById('txtTag').value = ''; } else if (typeof tag === "string") { document.getElementById('txtTag').value = tag; } else { document.getElementById('txtTag').value = JSON.stringify(tag); } }); document.getElementById('btnSearchTag').onclick = function () { var searchOrder = parseInt(document.getElementById('searchOrder').value, 10); if (isNaN(searchOrder)) { return; } var condition = new spreadNS.Search.SearchCondition(); condition.searchTarget = spreadNS.Search.SearchFoundFlags.cellTag; condition.searchString = document.getElementById('txtSearchTag').value; condition.findBeginRow = sheet.getActiveRowIndex(); condition.findBeginColumn = sheet.getActiveColumnIndex(); condition.searchOrder = searchOrder; if (searchOrder === 0) { condition.findBeginColumn++; } else { condition.findBeginRow++; } var result = sheet.search(condition); if (result.foundRowIndex < 0 && result.foundColumnIndex < 0) { condition.findBeginRow = 0; condition.findBeginColumn = 0; result = sheet.search(condition); } var row = result.foundRowIndex, col = result.foundColumnIndex; if (row < 0 && col < 0) { document.getElementById('txtTag').value = 'Not found'; } else { sheet.setActiveCell(row, col); document.getElementById('txtTag').value = sheet.getTag(row, col); } }; sheet.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"> <div class="option-row"> <label for="txtSearchTag">Search tag equals.</label> <label>For example enter: B4 tag demo (case sensitive):</label> <input type="text" id="txtSearchTag" placeholder="tag name" /> </div> <div class="option-row"> <label for="searchOrder">Search Order:</label> <select id="searchOrder"> <option value="0">Column First (ZOrder)</option> <option value="1">Row First (NOrder)</option> </select> <input type="button" id="btnSearchTag" value="Find Tag" title="Search cell with tag contains specified content and active the cell" /> </div> <div class="option-row"> <label for="txtTag" style="width:210px">Selected cell tag:</label> <input type="text" id="txtTag" /> </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; } .wide { width: 100%; box-sizing: border-box; } label { display: block; margin-bottom: 6px; } input, select { padding: 4px; margin: 0 4px 4px 0; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }