工作簿搜索功能

SpreadJS 提供了强大的单元格搜索功能,支持在工作表或整个工作簿范围内查找指定的文本、数字、公式、批注或单元格标签。通过灵活的搜索条件设置,可以实现区分大小写、完全匹配、使用通配符等多种搜索模式,并支持按行或按列的搜索顺序。

概述 本 Demo 展示了如何使用 SpreadJS 的搜索功能在工作表或工作簿中查找内容。Demo 提供了完整的搜索界面,支持设置搜索范围、查找目标、搜索顺序和匹配选项,实现了"查找下一个"功能,能够自动定位并滚动到搜索结果单元格。 实现思路 创建 Workbook 和 Worksheet,初始化示例数据(包括值、公式、批注和单元格标签) 构建搜索界面,提供搜索条件选项(查找内容、范围、查找范围、搜索顺序等) 点击"查找下一个"时,根据用户输入构建 SearchCondition 搜索条件对象 根据搜索范围(工作表或工作簿)执行不同的搜索逻辑,支持环绕搜索 找到结果后,激活对应工作表,定位到找到的单元格并滚动到视图中心 代码解析 构建搜索条件 这段代码根据用户的选择构建 SearchCondition 对象。searchOrder 决定搜索顺序(按行或按列),searchTarget 指定搜索目标类型,searchFlags 控制匹配方式。 在工作表中搜索 这段代码实现在当前工作表中从活动单元格位置开始向后搜索。通过设置 startSheetIndex 和 endSheetIndex 限定搜索范围为当前工作表,并从活动单元格的下一位置开始搜索。 处理搜索结果 找到结果后,通过 setActiveSheetIndex 激活对应工作表,使用 setActiveCell 定位到找到的单元格,最后调用 showCell 将单元格滚动到视图中心位置。 运行效果 在"查找内容"输入框中输入要搜索的文本或数字 选择搜索范围:工作表(仅当前工作表)或工作簿(所有工作表) 选择查找范围:值、公式、批注或单元格标签 可勾选"区分大小写"、"完全匹配"、"使用通配符"选项 点击"查找下一个"按钮,会自动定位到匹配的单元格并滚动到视图中心 如果搜索到工作表末尾未找到,会自动从头开始搜索(环绕搜索) 如果整个工作簿都未找到,会弹出"Not Found"提示 API 参考 SearchCondition 类 用于定义搜索条件的类,主要属性包括: searchString:要搜索的字符串 startSheetIndex、endSheetIndex:搜索的工作表索引范围 rowStart、columnStart、rowEnd、columnEnd:搜索的单元格范围 searchOrder:搜索顺序(SearchOrder.zOrder 按行,SearchOrder.nOrder 按列) searchTarget:搜索目标(SearchFoundFlags.cellText、cellFormula、cellComment、cellTag) searchFlags:搜索标志(ignoreCase、exactMatch、useWildCards、blockRange) search 方法 返回 SearchResult 对象,包含找到的工作表索引、行索引、列索引和匹配的字符串。 SearchResult 类 搜索结果对象,主要属性包括: searchFoundFlag:搜索结果标志,指示是否找到内容 foundSheetIndex:找到的工作表索引 foundRowIndex:找到的行索引 foundColumnIndex:找到的列索引 foundString:找到的字符串
var spreadNS = GC.Spread.Sheets, spread; window.onload = function () { spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 3}); spread.suspendPaint(); initSpread(spread); spread.resumePaint(); }; function initSheet(sheet) { sheet.setColumnWidth(0, 100); sheet.setColumnWidth(1, 100); sheet.setValue(0, 0, 'Value'); sheet.setValue(1, 0, 1); sheet.setValue(2, 0, 2); sheet.setValue(3, 0, 3); sheet.addSpan(0, 1, 1, 2); sheet.setValue(0, 1, 'Formula Result'); sheet.setValue(1, 1, 'SUM(A2:A3)'); sheet.setFormula(1, 2, '=SUM(A2:A3)'); var comment = null sheet.setTag(6, 2, "tag: C7"); comment = new GC.Spread.Sheets.Comments.Comment(); comment.text("tag: C7"); comment.backColor("yellow"); comment.foreColor("green"); comment.displayMode(GC.Spread.Sheets.Comments.DisplayMode.alwaysShown); comment.autoSize(true); sheet.getCell(6, 2).comment(comment); sheet.setTag(16, 2, "tag: C17"); comment = new GC.Spread.Sheets.Comments.Comment(); comment.text("tag: C17"); comment.backColor("yellow"); comment.foreColor("green"); comment.displayMode(GC.Spread.Sheets.Comments.DisplayMode.alwaysShown); comment.autoSize(true); sheet.getCell(16, 2).comment(comment); sheet.setTag(6, 8, "tag: I7"); comment = new GC.Spread.Sheets.Comments.Comment(); comment.text("tag: I7"); comment.backColor("yellow"); comment.foreColor("green"); comment.displayMode(GC.Spread.Sheets.Comments.DisplayMode.alwaysShown); comment.autoSize(true); sheet.getCell(6, 8).comment(comment); sheet.setTag(11, 8, "tag: I12"); comment = new GC.Spread.Sheets.Comments.Comment(); comment.text("tag: I12"); comment.backColor("yellow"); comment.foreColor("green"); comment.displayMode(GC.Spread.Sheets.Comments.DisplayMode.alwaysShown); comment.autoSize(true); sheet.getCell(11, 8).comment(comment); } function initSpread(spread) { var sheet1 = spread.getSheet(0); initSheet(sheet1); var sheet2 = spread.getSheet(1); initSheet(sheet2); document.getElementById('btnFindNext').onclick = function () { var sheet = spread.getActiveSheet(); var searchCondition = getSearchCondition(); var within = document.getElementById('searchWithin').value; var searchResult = null; if (within == "sheet") { var sels = sheet.getSelections(); if (sels.length > 1) { searchCondition.searchFlags |= spreadNS.Search.SearchFlags.blockRange; } else if (sels.length == 1) { var spanInfo = getSpanInfo(sheet, sels[0].row, sels[0].col); if (sels[0].rowCount != spanInfo.rowSpan && sels[0].colCount != spanInfo.colSpan) { searchCondition.searchFlags |= spreadNS.Search.SearchFlags.blockRange; } } searchResult = getResultSearchinSheetEnd(searchCondition); if (searchResult == null || searchResult.searchFoundFlag == spreadNS.Search.SearchFoundFlags.none) { searchResult = getResultSearchinSheetBefore(searchCondition); } } else if (within == "workbook") { searchResult = getResultSearchinSheetEnd(searchCondition); if (searchResult == null || searchResult.searchFoundFlag == spreadNS.Search.SearchFoundFlags.none) { searchResult = getResultSearchinWorkbookEnd(searchCondition); } if (searchResult == null || searchResult.searchFoundFlag == spreadNS.Search.SearchFoundFlags.none) { searchResult = getResultSearchinWorkbookBefore(searchCondition); } if (searchResult == null || searchResult.searchFoundFlag == spreadNS.Search.SearchFoundFlags.none) { searchResult = getResultSearchinSheetBefore(searchCondition); } } if (searchResult != null && searchResult.searchFoundFlag != spreadNS.Search.SearchFoundFlags.none) { spread.setActiveSheetIndex(searchResult.foundSheetIndex); var sheet = spread.getActiveSheet(); sheet.setActiveCell(searchResult.foundRowIndex, searchResult.foundColumnIndex); if ((searchCondition.searchFlags & spreadNS.Search.SearchFlags.blockRange) == 0) { sheet.setActiveCell(searchResult.foundRowIndex, searchResult.foundColumnIndex, 1, 1); } //scrolling if (searchResult.foundRowIndex < sheet.getViewportTopRow(1) || searchResult.foundRowIndex > sheet.getViewportBottomRow(1) || searchResult.foundColumnIndex < sheet.getViewportLeftColumn(1) || searchResult.foundColumnIndex > sheet.getViewportRightColumn(1) ) { sheet.showCell(searchResult.foundRowIndex, searchResult.foundColumnIndex, spreadNS.VerticalPosition.center, spreadNS.HorizontalPosition.center); } else { sheet.repaint(); } } else { //Not Found alert('Not Found'); } }; } function getSpanInfo(sheet, row, col) { var span = sheet.getSpans(new spreadNS.Range(row, col, 1, 1)); if (span.length > 0) { return {rowSpan: span[0].rowCount, colSpan: span[0].colCount}; } else { return {rowSpan: 1, colSpan: 1}; } } function getResultSearchinSheetEnd(searchCondition) { var sheet = spread.getActiveSheet(); searchCondition.startSheetIndex = spread.getActiveSheetIndex(); searchCondition.endSheetIndex = spread.getActiveSheetIndex(); if (searchCondition.searchOrder == spreadNS.Search.SearchOrder.zOrder) { searchCondition.rowStart = sheet.getActiveRowIndex(); searchCondition.columnStart = sheet.getActiveColumnIndex() + 1; } else if (searchCondition.searchOrder == spreadNS.Search.SearchOrder.nOrder) { searchCondition.rowStart = sheet.getActiveRowIndex() + 1; searchCondition.columnStart = sheet.getActiveColumnIndex(); } if ((searchCondition.searchFlags & spreadNS.Search.SearchFlags.blockRange) > 0) { var sel = sheet.getSelections()[0]; searchCondition.rowStart = sel.row; searchCondition.columnStart = sel.col; searchCondition.rowEnd = sel.row + sel.rowCount - 1; searchCondition.columnEnd = sel.col + sel.colCount - 1; } var searchResult = spread.search(searchCondition); return searchResult; } function getResultSearchinSheetBefore(searchCondition) { var sheet = spread.getActiveSheet(); searchCondition.startSheetIndex = spread.getActiveSheetIndex(); searchCondition.endSheetIndex = spread.getActiveSheetIndex(); if ((searchCondition.searchFlags & spreadNS.Search.SearchFlags.blockRange) > 0) { var sel = sheet.getSelections()[0]; searchCondition.rowStart = sel.row; searchCondition.columnStart = sel.col; searchCondition.rowEnd = sel.row + sel.rowCount - 1; searchCondition.columnEnd = sel.col + sel.colCount - 1; } else { searchCondition.rowStart = -1; searchCondition.columnStart = -1; searchCondition.rowEnd = sheet.getActiveRowIndex(); searchCondition.columnEnd = sheet.getActiveColumnIndex(); } var searchResult = spread.search(searchCondition); return searchResult; } function getResultSearchinWorkbookEnd(searchCondition) { searchCondition.rowStart = -1; searchCondition.columnStart = -1; searchCondition.rowEnd = -1; searchCondition.columnEnd = -1; searchCondition.startSheetIndex = spread.getActiveSheetIndex() + 1; searchCondition.endSheetIndex = -1; var searchResult = spread.search(searchCondition); return searchResult; } function getResultSearchinWorkbookBefore(searchCondition) { searchCondition.rowStart = -1; searchCondition.columnStart = -1; searchCondition.rowEnd = -1; searchCondition.columnEnd = -1; searchCondition.startSheetIndex = -1 searchCondition.endSheetIndex = spread.getActiveSheetIndex() - 1; var searchResult = spread.search(searchCondition); return searchResult; } function getSearchCondition() { var searchCondition = new spreadNS.Search.SearchCondition(); var findWhat = document.getElementById('txtSearchWhat').value; var within = document.getElementById('searchWithin').value; var order = document.getElementById('searchOrder').value; var lookin = document.getElementById('searchLookin').value; var matchCase = document.getElementById('chkSearchMachCase').checked; var matchEntire = document.getElementById('chkSearchMachEntire').checked; var useWildCards = document.getElementById('chkSearchUseWildCards').checked; searchCondition.searchString = findWhat; if (within == "sheet") { searchCondition.startSheetIndex = spread.getActiveSheetIndex(); searchCondition.endSheetIndex = spread.getActiveSheetIndex(); } if (order == "byRows") { searchCondition.searchOrder = spreadNS.Search.SearchOrder.zOrder; } else { searchCondition.searchOrder = spreadNS.Search.SearchOrder.nOrder; } if (lookin == "formula") { searchCondition.searchTarget = spreadNS.Search.SearchFoundFlags.cellFormula; } else if (lookin == "comment") { searchCondition.searchTarget = spreadNS.Search.SearchFoundFlags.cellComment; } else if (lookin == "tag") { searchCondition.searchTarget = spreadNS.Search.SearchFoundFlags.cellTag; } else { searchCondition.searchTarget = spreadNS.Search.SearchFoundFlags.cellText; } if (!matchCase) { searchCondition.searchFlags |= spreadNS.Search.SearchFlags.ignoreCase; } if (matchEntire) { searchCondition.searchFlags |= spreadNS.Search.SearchFlags.exactMatch; } if (useWildCards) { searchCondition.searchFlags |= spreadNS.Search.SearchFlags.useWildCards; } return searchCondition; }
<!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>使用这些选项指定在电子表格中搜索的内容。</p> <div> <label>查找内容:</label> <input id="txtSearchWhat" /> </div> <div> <label>范围:</label> <select id="searchWithin"> <option value="sheet" selected>工作表</option> <option value="workbook">工作簿</option> </select> <input id="chkSearchMachCase" type="checkbox" /> <label for="chkSearchMachCase">区分大小写</label> </div> <div> <label>查找范围:</label> <select id="searchLookin"> <option value="value" selected>值</option> <option value="formula">公式</option> <option value="tag">单元格标签</option> <option value="comment">批注</option> </select> <input id="chkSearchMachEntire" type="checkbox" /> <label for="chkSearchMachEntire">完全匹配</label> </div> <div> <label>搜索顺序:</label> <select id="searchOrder"> <option value="byRows" selected>按行</option> <option value="byColumns">按列</option> </select> <div> <input id="chkSearchUseWildCards" type="checkbox" /> <label for="chkSearchUseWildCards">使用通配符</label> </div> </div> <div> <label></label> <input id="btnFindNext" type="button" value="查找下一个" /> </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; } label { display: inline-block; margin: 8px 0 6px; } input[type="checkbox"] { margin: 6px 0; width: auto; } input, select { padding: 4px 6px; width: 100%; box-sizing: border-box; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }