Overview

You can use tags to store private data. SpreadJS supports tags for cells, rows, columns, and sheets.

<p><strong>Sheet.tag</strong>(value?): Gets or sets the tag for the sheet.</p> <p><strong>Sheet.getTag</strong>(row, col, sheetArea?) or <strong>Sheet.setTag</strong>(row, col, tag, sheetArea?): Get or set tag for:</p> <ul> <li><strong>sheet: when row is -1 and col is -1</strong></li> <li><strong>row: when col is -1 and row >= 0</strong></li> <li><strong>column: when row is -1 and col >= 0</strong></li> <li><strong>cell: when row >=0 and col >= 0</strong></li> </ul> <p><strong>CellRange.tag</strong>(value?): Gets or sets the tag for the cell.</p> <p>When a tag is set, the following events will occur:</p> <ul> <li><strong>CellChanged event for CellRange.tag(value)</strong></li> <li><strong>RowChanged event for CellRange.tag(value)</strong></li> <li><strong>ColumnChanged event for CellRange.tag(value)</strong></li> </ul> <p>You can clear the tag by setting the tag to null or undefined or by using <strong>Sheet.clear</strong>(row, column, rowCount, columnCount, sheetArea, StorageType.tag).</p> <p>For example:</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 charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/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$/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>You can set, get or clear tag of selected items.</span> <span>Tags are provided in this demo: sheet, Row 4, Row 6, Column E-G, A1, A7:C10.</span> </p> </div> <div class="option-row"> <label for="txtTag">Tag: </label> <input id="txtTag" /> </div> <div class="option-row"> <input type="button" id="btnSetTag" value="Set tag" title="Set tag to selected item(s)" /> <input type="button" id="btnGetTag" value="Get tag" title="Get tag of 1st selected item" /> <input type="button" id="btnClearTag" value="Clear tag" title="Clear tag of selected items" /> </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; }