タグの検索

文字列型のセルタグは、検索することができます。検索順序はZOrder(列から)またはNOrder(行から)に設定できます。

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">次に一致するタグを検索(大文字と小文字を区別):</label> <input type="text" id="txtSearchTag" placeholder="タグ名を入力してください。例:B4タグのデモ" v-model="searchTag" /> </div> <div class="option-row"> <label for="searchOrder">検索順序:</label> <select id="searchOrder" v-model="searchOrderRef"> <option value="0">列を先に検索(Z字順)</option> <option value="1">行を先に検索(N字順)</option> </select> <input type="button" id="btnSearchTag" value="タグを検索" title="指定の値を含むタグの設定されたセルを検索し、このセルをアクティブにします" @click="handelSearchTag" /> </div> <div class="option-row"> <label for="txtTag" style=" width: 210px">選択されたセルのタグ:</label> <input type="text" id="txtTag" v-model="txtTag" /> </div> </div> </div> </template> <script setup> import '@mescius/spread-sheets-vue' import { ref, computed } from "vue"; import GC from '@mescius/spread-sheets'; import '@mescius/spread-sheets-resources-ja'; GC.Spread.Common.CultureManager.culture("ja-jp"); const spreadRef = ref(null); const txtTag = ref(""); const searchTag = ref(""); const searchOrderRef = ref('0'); function initSpread(spread) { spreadRef.value = spread; let spreadNS = GC.Spread.Sheets; 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) { txtTag.value = ''; } else if (typeof tag === "string") { txtTag.value = tag; } else { txtTag.value = JSON.stringify(tag); } }); sheet.resumePaint(); } function handelSearchTag() { let searchOrder = parseInt(searchOrderRef.value, 10); let spreadNS = GC.Spread.Sheets; let sheet = spreadRef.value.sheets[0]; if (isNaN(searchOrder)) { return; } let condition = new spreadNS.Search.SearchCondition(); condition.searchTarget = spreadNS.Search.SearchFoundFlags.cellTag; condition.searchString = searchTag.value; condition.rowStart = sheet.getActiveRowIndex(); condition.columnStart = sheet.getActiveColumnIndex(); condition.searchOrder = searchOrder; if (searchOrder === 0) { condition.columnStart++; } else { condition.rowStart++; } let result = sheet.search(condition); if (result.foundRowIndex < 0 && result.foundColumnIndex < 0) { condition.rowStart = 0; condition.columnStart = 0; result = sheet.search(condition); } let row = result.foundRowIndex, col = result.foundColumnIndex; if (row < 0 && col < 0) { txtTag.value = 'Not found'; } else { sheet.setActiveCell(row, col); txtTag.value = sheet.getTag(row, col); } } </script> <style scoped> .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; } </style>
<!DOCTYPE html> <html lang="en" 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$/ja/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/ja/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$/ja/lib/vue3/license.js'); </script> </head> <body> <div id="app" style="height: 100%;"></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: { '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js', '@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js', '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", }, meta: { '*.css': { loader: 'systemjs-plugin-css' }, '*.vue': { loader: "../plugin-vue/index.js" } } }); })(this);