首先预备一个 SearchCondition ,然后将 searchTarget 设置成 SearchFoundFlags.cellTag 。然后就可以搜索标签了。
通过 sheet.search(searchCondition) 方法来完成搜索。
搜索结果的 foundRowIndex 和 foundColumnIndex 参数说明了第一个匹配的单元格的位置;如果没有找到那么结果都是-1。示例代码如下:
<template>
<div class="sample-tutorial">
<gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread">
<gc-worksheet />
</gc-spread-sheets>
<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" v-model="searchTag" />
</div>
<div class="option-row">
<label for="searchOrder">Search Order:</label>
<select id="searchOrder" v-model="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" @click="handelSearchTag" />
</div>
<div class="option-row">
<label for="txtTag" style=" width: 210px">Selected cell tag:</label>
<input type="text" id="txtTag" v-model="txtTag" />
</div>
</div>
</div>
</template>
<script>
import Vue from "vue";
import '@grapecity-software/spread-sheets-resources-zh';
GC.Spread.Common.CultureManager.culture("zh-cn");
import "@grapecity-software/spread-sheets-vue";
import GC from "@grapecity-software/spread-sheets";
import "./styles.css";
let App = Vue.extend({
name: "app",
data: function() {
return {
spread: null,
txtTag: "",
searchTag: '',
searchOrder: '0',
};
},
methods: {
initSpread(spread) {
let spreadNS = GC.Spread.Sheets;
let self = this;
this.spread = spread;
let 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() {
let tag = sheet.getTag(sheet.getActiveRowIndex(), sheet.getActiveColumnIndex());
if (tag == null) {
self.txtTag = '';
} else if (typeof tag === "string") {
self.txtTag = tag;
} else {
self.txtTag = JSON.stringify(tag);
}
});
sheet.resumePaint();
},
handelSearchTag() {
let searchOrder = parseInt(this.searchOrder, 10);
let spreadNS = GC.Spread.Sheets;
let sheet = this.spread.sheets[0];
if (isNaN(searchOrder)) {
return;
}
let condition = new spreadNS.Search.SearchCondition();
condition.searchTarget = spreadNS.Search.SearchFoundFlags.cellTag;
condition.searchString = this.searchTag;
condition.findBeginRow = sheet.getActiveRowIndex();
condition.findBeginColumn = sheet.getActiveColumnIndex();
condition.searchOrder = searchOrder;
if (searchOrder === 0) {
condition.findBeginColumn++;
} else {
condition.findBeginRow++;
}
let result = sheet.search(condition);
if (result.foundRowIndex < 0 && result.foundColumnIndex < 0) {
condition.findBeginRow = 0;
condition.findBeginColumn = 0;
result = sheet.search(condition);
}
let row = result.foundRowIndex,
col = result.foundColumnIndex;
if (row < 0 && col < 0) {
this.txtTag = 'Not found';
} else {
sheet.setActiveCell(row, col);
this.txtTag = sheet.getTag(row, col);
}
}
}
});
new Vue({
render: h => h(App)
}).$mount("#app");
</script>
<!doctype html>
<html style="height:100%;font-size:14px;">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css"
href="$DEMOROOT$/zh/vue/node_modules/@grapecity-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<!-- SystemJS -->
<script src="$DEMOROOT$/zh/vue/node_modules/systemjs/dist/system.src.js"></script>
<script src="systemjs.config.js"></script>
<script>
System.import('./src/app.vue');
System.import('$DEMOROOT$/zh/lib/vue/license.js');
</script>
</head>
<body>
<div id="app"></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;
}
(function(global) {
System.config({
transpiler: 'plugin-babel',
babelOptions: {
es2015: true
},
meta: {
'*.css': { loader: 'css' },
'*.vue': { loader: 'vue-loader' }
},
paths: {
// paths serve as alias
'npm:': 'node_modules/'
},
// map tells the System loader where to look for things
map: {
'@grapecity-software/spread-sheets': 'npm:@grapecity-software/spread-sheets/index.js',
'@grapecity-software/spread-sheets-resources-zh': 'npm:@grapecity-software/spread-sheets-resources-zh/index.js',
'@grapecity-software/spread-sheets-vue': 'npm:@grapecity-software/spread-sheets-vue/index.js',
'@grapecity-software/jsob-test-dependency-package/react-components': 'npm:@grapecity-software/jsob-test-dependency-package/react-components/index.js',
'jszip': 'npm:jszip/dist/jszip.js',
'css': 'npm:systemjs-plugin-css/css.js',
'vue': 'npm:vue/dist/vue.min.js',
'vue-loader': 'npm:systemjs-vue-browser/index.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'
},
// packages tells the System loader how to load when no filename and/or no extension
packages: {
src: {
defaultExtension: 'js'
},
rxjs: {
defaultExtension: 'js'
},
"node_modules": {
defaultExtension: 'js'
}
}
});
})(this);