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="ja-jp" />
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ja/purejs/node_modules/@mescius/spread-sheets-resources-ja/dist/gc.spread.sheets.resources.ja.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">次に一致するタグを検索(大文字と小文字を区別):</label>
<input type="text" id="txtSearchTag" placeholder="タグ名を入力してください。例:B4タグのデモ" />
</div>
<div class="option-row">
<label for="searchOrder">検索順序:</label>
<select id="searchOrder">
<option value="0">列を先に検索(Z字順)</option>
<option value="1">行を先に検索(N字順)</option>
</select>
<input type="button" id="btnSearchTag" value="タグを検索" title="指定の値を含むタグの設定されたセルを検索し、このセルをアクティブにします" />
</div>
<div class="option-row">
<label for="txtTag" style="width:210px">選択されたセルのタグ:</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;
}