概要と基本的な使い方

タグを使用すると、個人的なデータを保存できます。SpreadJSでは、セル、行、列、およびシートにタグを追加できます。

<p><strong>Sheet.tag</strong>(value?): シートのタグを取得または設定します。</p> <p><strong>Sheet.getTag</strong>(row, col, sheetArea?) / <strong>Sheet.setTag</strong>(row, col, tag, sheetArea?): 引数に従い以下のようにタグを取得または設定します。</p> <ul> <li><strong>rowとcolが-1の場合、シートタグを設定および取得します。</strong></li> <li><strong>rowが0以上、colが-1の場合、rowの示す行のタグを設定および取得します。</strong></li> <li><strong>rowが-1、colが0以上の場合、colの示す列のタグを設定および取得します。</strong></li> <li><strong>rowとcolが0以上の場合、rowおよびcolの示すセルのタグを設定および取得します。</strong></li> </ul> <p><strong>CellRange.tag</strong>(value?): セルのタグを取得または設定します。</p> <p>タグを設定すると、以下のイベントが発生します。</p> <ul> <li><strong>CellChangedイベント / CellRange.tag(value)実行の場合</strong></li> <li><strong>RowChangedイベント / CellRange.tag(value)実行の場合</strong></li> <li><strong>ColumnChangedイベント / CellRange.tag(value)実行の場合</strong></li> </ul> <p>タグをクリアするには、対象のタグをnullまたは未定義に設定するか、または<strong>Sheet.clear</strong>(row, column, rowCount, columnCount, sheetArea, StorageType.tag)を使用します。</p> <p>次に、例を示します。</p> <pre><code class="hljs js language-js"> sheet.tag(<span class="hljs-string">'Sheet tag'</span>); sheet.setTag(<span class="hljs-number">3</span>, <span class="hljs-number">-1</span>, <span class="hljs-string">'Row tag'</span>); sheet.getRange(<span class="hljs-number">5</span>, <span class="hljs-number">-1</span>, <span class="hljs-number">1</span>, <span class="hljs-number">-1</span>).tag(<span class="hljs-string">'Row tag too'</span>); sheet.getRange(<span class="hljs-number">-1</span>, <span class="hljs-number">4</span>, <span class="hljs-number">-1</span>, <span class="hljs-number">3</span>).tag(<span class="hljs-string">'Column tag'</span>); sheet.getCell(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>).tag(<span class="hljs-string">'A1 tag'</span>); sheet.getRange(<span class="hljs-number">6</span>, <span class="hljs-number">0</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>).tag(<span class="hljs-string">'Cells tag'</span>); sheet.clear(<span class="hljs-number">6</span>, <span class="hljs-number">0</span>, <span class="hljs-number">9</span>, <span class="hljs-number">2</span>, spreadNS.SheetArea.viewport, spreadNS.StorageType.tag); </code></pre>
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 1}); spread.suspendPaint(); initSpread(spread); bindEvents(spread.getActiveSheet()); spread.resumePaint(); }; function bindEvents(sheet) { sheet.bind(GC.Spread.Sheets.Events.SelectionChanged, function () { getTag(sheet); }); } function getTag(sheet) { var selections = sheet.getSelections(); if (!selections || selections.length === 0) { return; } var sel = selections[0], row = sel.row, col = sel.col, tag = document.getElementById('txtTag'); if (row === -1 && col === -1) { tag.value = sheet.tag() !== undefined ? sheet.tag() : ""; } else { tag.value = sheet.getTag(row, col)!== undefined ? sheet.getTag(row, col) : ""; } }; function initSpread(spread) { var spreadNS = GC.Spread.Sheets, sheet = spread.sheets[0]; var sd = dataSource; if (sd.length > 0) { sheet.setDataSource(sd); } sheet.setColumnWidth(0, 160); sheet.setColumnWidth(1, 70); sheet.setColumnWidth(2, 90); sheet.setColumnWidth(3, 110); sheet.setColumnWidth(4, 80); sheet.setColumnWidth(6, 110); // sheet tag sheet.tag("Sheet tag"); // row tag sheet.setTag(3, -1, "Row 4 tag"); sheet.getRange(5, -1, 1, -1).tag("Row 6 tag"); sheet.getCell(5, -1, GC.Spread.Sheets.SheetArea.rowHeader).backColor('red'); // column tag sheet.getRange(-1, 4, -1, 3).tag("Column E-G tag"); sheet.getCell(-1, 4, GC.Spread.Sheets.SheetArea.colHeader).backColor('red'); // cell tag sheet.getCell(0, 0).tag("A1 tag"); sheet.getCell(0, 0).backColor("tan"); sheet.getRange(6, 0, 4, 3).tag("Cells tag (A7:C10)"); sheet.getRange(6, 0, 4, 3).backColor("tan"); document.getElementById('btnSetTag').onclick = function () { var tag = document.getElementById('txtTag').value; var selections = sheet.getSelections(); if (!selections || selections.length === 0) { return; } sheet.suspendPaint(); var length = selections.length; for (var i = 0; i < length; i++) { var sel = selections[i], rowIndex = sel.row, colIndex = sel.col, rowCount = sel.rowCount, colCount = sel.colCount, maxRow = rowIndex + rowCount, maxColumn = colIndex + colCount, r, c; if (rowIndex === -1 && colIndex === -1) { sheet.tag(tag); } else if (rowIndex === -1) { for (c = colIndex; c < maxColumn; c++) { sheet.setTag(-1, c, tag); } } else if (colIndex === -1) { for (r = rowIndex; r < maxRow; r++) { sheet.setTag(r, -1, tag); } } else { for (r = rowIndex; r < maxRow; r++) { for (c = colIndex; c < maxColumn; c++) { sheet.setTag(r, c, tag); } } } } sheet.resumePaint(); getTag(sheet); }; document.getElementById('btnClearTag').onclick = function () { var selections = sheet.getSelections(); if (!selections || selections.length === 0) { return; } sheet.suspendPaint(); var length = selections.length; for (var i = 0; i < length; i++) { var sel = selections[i], row = sel.row, col = sel.col; if (row === -1 && col === -1) { sheet.tag(null); } else { sheet.clear(row, col, sel.rowCount, sel.colCount, spreadNS.SheetArea.viewport, spreadNS.StorageType.tag); } } sheet.resumePaint(); getTag(sheet); }; }
<!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/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/spread/source/data/data.js" type="text/javascript"></script> <script src="$DEMOROOT$/ja/purejs/node_modules/@grapecity/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"> <p> <span>選択したアイテムに対し、タグを設定、取得、または削除できます。</span> <span>このデモでは、シート、行4、行6、列E~G、セルA1、セル範囲A7:C10にタグが設定されています。</span> </p> </div> <div class="option-row"> <label for="txtTag">タグ:</label> <input id="txtTag" /> </div> <div class="option-row"> <input type="button" style="width: 120px"id="btnSetTag" value="タグを設定" title="選択したアイテムにタグを設定します" /> <input type="button" style="width: 120px"id="btnGetTag" value="タグを取得" title="最初に選択したアイテムからタグを取得します" /> <input type="button" style="width: 200px" id="btnClearTag" value="タグをクリア" title="選択したアイテムからタグを削除します" /> </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; } label { display: inline-block; margin-bottom: 6px; } input { padding: 4px; width: 100%; margin: 0 4px 4px 0; box-sizing: border-box; } input[type=button] { width: 30%; } p { background-color: lavender; padding: 4px; } p span { display: block; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }