Overview

SpreadJS allows searching for content within the workbook, such as a particular number or text string.

Description
app.vue
index.html
Copy to CodeMine

To search for a specific text string or number, use the search method to search the text in cells with the specified criteria, as shown in the following example:

    sheet.setText(6, 10, 'testSearch');
    var searchCondition = new GC.Spread.Sheets.Search.SearchCondition();
    searchCondition.searchString = 'testSearch';
    var searchresult = sheet.search(searchCondition);

Use the SearchCondition to customize the search parameters.

    this.sheet.getCell(3, 3).text('Demo string for search');
    var condition = new GC.Spread.Sheets.Search.SearchCondition();
    condition.searchString = 'for';
    condition.startSheetIndex = 0;
    condition.endSheetIndex = 0;
    condition.searchFlags = GC.Spread.Sheets.Search.SearchFlags.ignoreCase | GC.Spread.Sheets.Search.SearchFlags.blockRange;
    condition.searchOrder = GC.Spread.Sheets.Search.SearchOrder.nOrder;
    condition.searchTarget = GC.Spread.Sheets.Search.SearchFoundFlags.cellText;
    condition.sheetArea = GC.Spread.Sheets.SheetArea.viewport;
    condition.rowStart = 0;
    condition.columnStart = 0;
    condition.rowEnd = 10;
    condition.columnEnd = 10;
    var result = this.spread.search(condition);

The search result is an object with the following properties:

To search for a specific text string or number, use the search method to search the text in cells with the specified criteria, as shown in the following example: Use the SearchCondition to customize the search parameters. The search result is an object with the following properties: searchFoundFlag: An enumeration that specifies what is matched. foundSheetIndex: The index of the sheet in which a match is found. foundRowIndex: The index of the row at which a match is found. foundColumnIndex: The index of the column at which a match is found. foundString: The found string.
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> <gc-worksheet></gc-worksheet> <gc-worksheet></gc-worksheet> <gc-worksheet></gc-worksheet> </gc-spread-sheets> <div class="options-container"> <p>Use these options to specify what to search for in Spread.</p> <div> <label>Find what:</label> <input id="txtSearchWhat" v-model="txtSearchWhat" /> </div> <div> <label>Within:</label> <select id="searchWithin" v-model="searchWithin"> <option value="sheet" selected>Sheet</option> <option value="workbook">Workbook</option> </select> <input id="chkSearchMachCase" type="checkbox" v-model="chkSearchMachCase" /> <label for="chkSearchMachCase">Match case</label> </div> <div> <label>Look in:</label> <select id="searchLookin" v-model="searchLookIn"> <option value="value" selected>Values</option> <option value="formula">Formulas</option> <option value="tag">Cell Tag</option> <option value="comment">Comments</option> </select> <input id="chkSearchMachEntire" type="checkbox" v-model="chkSearchMachEntire" /> <label for="chkSearchMachEntire">Match exactly</label> </div> <div> <label>Search:</label> <select id="searchOrder" v-model="searchOrder"> <option value="byRows" selected>By Rows</option> <option value="byColumns">By Columns</option> </select> <div> <input id="chkSearchUseWildCards" type="checkbox" v-model="chkSearchUseWildCards" /> <label for="chkSearchUseWildCards">Use wildcards</label> </div> </div> <div> <label></label> <input id="btnFindNext" type="button" value="Find Next" @click="findNext" /> </div> </div> </div> </template> <script setup> import GC from "@mescius/spread-sheets"; import { ref } from "vue"; import "@mescius/spread-sheets-vue"; let spread = null; const txtSearchWhat = ref(''); const searchWithin = ref('sheet'); const searchOrder = ref('byRows'); const searchLookIn = ref('value'); const chkSearchMachCase = ref(false); const chkSearchMachEntire = ref(false); const chkSearchUseWildCards = ref(false); const initSpread = (s) => { spread = s; spread.suspendPaint(); const sheet1 = spread.getSheet(0); initSheet(sheet1); const sheet2 = spread.getSheet(1); sheet2.setColumnWidth(0, 100); initSheet(sheet2); spread.resumePaint(); } const 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)'); sheet.setTag(6, 2, "tag: C7"); let 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); } const findNext = (e) => { let sheet = spread.getActiveSheet(); let searchCondition = getSearchCondition(spread); let within = searchWithin.value; let searchResult = null; if (within == "sheet") { let selections = sheet.getSelections(); if (selections.length > 1) { searchCondition.searchFlags |= GC.Spread.Sheets.Search.SearchFlags.blockRange; } else if (selections.length == 1) { let spanInfo = getSpanInfo(sheet, selections[0].row, selections[0].col); if (selections[0].rowCount != spanInfo.rowSpan && selections[0].colCount != spanInfo.colSpan) { searchCondition.searchFlags |= GC.Spread.Sheets.Search.SearchFlags.blockRange; } } searchResult = getResultSearchInSheetEnd(spread, searchCondition); if (searchResult == null || searchResult.searchFoundFlag == GC.Spread.Sheets.Search.SearchFoundFlags.none) { searchResult = getResultSearchInSheetBefore(spread, searchCondition); } } else if (within == "workbook") { searchResult = getResultSearchInSheetEnd(spread, searchCondition); if (searchResult == null || searchResult.searchFoundFlag == GC.Spread.Sheets.Search.SearchFoundFlags.none) { searchResult = getResultSearchInWorkbookEnd(spread, searchCondition); } if (searchResult == null || searchResult.searchFoundFlag == GC.Spread.Sheets.Search.SearchFoundFlags.none) { searchResult = getResultSearchInWorkbookBefore(spread, searchCondition); } if (searchResult == null || searchResult.searchFoundFlag == GC.Spread.Sheets.Search.SearchFoundFlags.none) { searchResult = getResultSearchInSheetBefore(spread, searchCondition); } } if (searchResult != null && searchResult.searchFoundFlag != GC.Spread.Sheets.Search.SearchFoundFlags.none) { spread.setActiveSheetIndex(searchResult.foundSheetIndex); let sheet = spread.getActiveSheet(); sheet.setActiveCell(searchResult.foundRowIndex, searchResult.foundColumnIndex); if ((searchCondition.searchFlags & GC.Spread.Sheets.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, GC.Spread.Sheets.VerticalPosition.center, GC.Spread.Sheets.HorizontalPosition.center); } else { sheet.repaint(); } } else { //Not Found alert('Not Found'); } } function getSpanInfo(sheet, row, col) { let span = sheet.getSpans(new GC.Spread.Sheets.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(spread, searchCondition) { let sheet = spread.getActiveSheet(); searchCondition.startSheetIndex = spread.getActiveSheetIndex(); searchCondition.endSheetIndex = spread.getActiveSheetIndex(); if (searchCondition.searchOrder == GC.Spread.Sheets.Search.SearchOrder.zOrder) { searchCondition.rowStart = sheet.getActiveRowIndex(); searchCondition.columnStart = sheet.getActiveColumnIndex() + 1; } else if (searchCondition.searchOrder == GC.Spread.Sheets.Search.SearchOrder.nOrder) { searchCondition.rowStart = sheet.getActiveRowIndex() + 1; searchCondition.columnStart = sheet.getActiveColumnIndex(); } if ((searchCondition.searchFlags & GC.Spread.Sheets.Search.SearchFlags.blockRange) > 0) { let 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; } let searchResult = spread.search(searchCondition); return searchResult; } function getResultSearchInSheetBefore(spread, searchCondition) { let sheet = spread.getActiveSheet(); searchCondition.startSheetIndex = spread.getActiveSheetIndex(); searchCondition.endSheetIndex = spread.getActiveSheetIndex(); if ((searchCondition.searchFlags & GC.Spread.Sheets.Search.SearchFlags.blockRange) > 0) { let 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(); } let searchResult = spread.search(searchCondition); return searchResult; } function getResultSearchInWorkbookEnd(spread, searchCondition) { searchCondition.rowStart = -1; searchCondition.columnStart = -1; searchCondition.rowEnd = -1; searchCondition.columnEnd = -1; searchCondition.startSheetIndex = spread.getActiveSheetIndex() + 1; searchCondition.endSheetIndex = -1; let searchResult = spread.search(searchCondition); return searchResult; } function getResultSearchInWorkbookBefore(spread, searchCondition) { searchCondition.rowStart = -1; searchCondition.columnStart = -1; searchCondition.rowEnd = -1; searchCondition.columnEnd = -1; searchCondition.startSheetIndex = -1 searchCondition.endSheetIndex = spread.getActiveSheetIndex() - 1; let searchResult = spread.search(searchCondition); return searchResult; } function getSearchCondition(spread) { let searchCondition = new GC.Spread.Sheets.Search.SearchCondition(); let findWhat = txtSearchWhat.value; let within = searchWithin.value; let order = searchOrder.value; let lookIn = searchLookIn.value; let matchCase = chkSearchMachCase.value; let matchEntire = chkSearchMachEntire.value; let useWildCards = chkSearchUseWildCards.value; searchCondition.searchString = findWhat; if (within == "sheet") { searchCondition.startSheetIndex = spread.getActiveSheetIndex(); searchCondition.endSheetIndex = spread.getActiveSheetIndex(); } if (order == "byRows") { searchCondition.searchOrder = GC.Spread.Sheets.Search.SearchOrder.zOrder; } else { searchCondition.searchOrder = GC.Spread.Sheets.Search.SearchOrder.nOrder; } if (lookIn == "formula") { searchCondition.searchTarget = GC.Spread.Sheets.Search.SearchFoundFlags.cellFormula; } else if (lookIn == "comment") { searchCondition.searchTarget = GC.Spread.Sheets.Search.SearchFoundFlags.cellComment; } else if (lookIn == "tag") { searchCondition.searchTarget = GC.Spread.Sheets.Search.SearchFoundFlags.cellTag; } else { searchCondition.searchTarget = GC.Spread.Sheets.Search.SearchFoundFlags.cellText; } if (!matchCase) { searchCondition.searchFlags |= GC.Spread.Sheets.Search.SearchFlags.ignoreCase; } if (matchEntire) { searchCondition.searchFlags |= GC.Spread.Sheets.Search.SearchFlags.exactMatch; } if (useWildCards) { searchCondition.searchFlags |= GC.Spread.Sheets.Search.SearchFlags.useWildCards; } return searchCondition; } </script> <style scoped> #app { height: 100%; } .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; } </style>
<!DOCTYPE html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>SpreadJS VUE</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/vue3/node_modules/systemjs/dist/system.src.js"></script> <script src="./systemjs.config.js"></script> <script src="./compiler.js" type="module"></script> <script> var System = SystemJS; System.import("./src/app.js"); System.import('$DEMOROOT$/en/lib/vue3/license.js'); </script> </head> <body> <div id="app"></div> </body> </html>
(function (global) { SystemJS.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, packageConfigPaths: [ './node_modules/*/package.json', "./node_modules/@mescius/*/package.json", "./node_modules/@babel/*/package.json", "./node_modules/@vue/*/package.json" ], map: { 'vue': "npm:vue/dist/vue.esm-browser.js", 'tiny-emitter': 'npm:tiny-emitter/index.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', "systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js", '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-en': 'npm:@mescius/spread-sheets-resources-en/index.js', '@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js' }, meta: { '*.css': { loader: 'systemjs-plugin-css' }, '*.vue': { loader: "../plugin-vue/index.js" } } }); })(this);