基本应用

你可以使用标签来存储私有的数据。 SpreadJS 支持单元格标签,行标签,列标签和工作表标签。

Sheet.tag(value?): 设置或者获取表单上的标签。 Sheet.getTag(row, col, sheetArea?) / Sheet.setTag(row, col, tag, sheetArea?): 也可以通过以下方式为不同的单元设置标签: 表单: 当 row 和 col 都为 -1 时 行: 当 col 为 -1 并且 row 大于等于 0 时 列: 当 row 为 -1 并且 col 大于等于 0 时 单元格: 当 row 大于等于 0 并且 col 大于等于 0 时 CellRange.tag(value?): 设置或者获取单元格上的标签。 当设置标签的时,会触发以下事件,你可以监听此类事件完成自己的业务逻辑: CellChanged 事件在设置单元格标签时触发,比如 CellRange.tag(value) RowChanged 事件在设置行标签时触发,比如 CellRange.tag(value) ColumnChanged 事件在设置列标签时触发,比如 CellRange.tag(value) 你可以设置标签为 null 或者 undefined 或者使用 Sheet.clear(row, column, rowCount, columnCount, sheetArea, StorageType.Tag) 方法来删除标签。例如:
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="zh-cn" /> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/zh/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/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$/zh/purejs/node_modules/@grapecity/spread-sheets-resources-zh/dist/gc.spread.sheets.resources.zh.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>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; }