按钮单元格

SpreadJS 支持在单元格中嵌入按钮,为表格应用提供交互式操作入口。按钮单元格支持自定义文本、背景色、边距等样式属性,适用于触发表单提交、执行计算、打开面板等需要用户点击操作的场景。

概述 本 Demo 展示了如何创建和自定义按钮单元格,并提供了交互式的属性编辑功能。Demo 中创建了一个按钮单元格,用户可以通过右侧面板动态修改按钮的文本、背景色和边距等属性。 实现思路 创建 Button 单元格类型实例并设置初始属性(文本、边距) 使用 setCellType 方法将按钮应用到指定单元格 监听工作表的 SelectionChanged 事件,当用户选择单元格时判断是否为按钮类型 如果选中的是按钮单元格,读取其属性并显示在右侧面板的输入框中 点击"更新"按钮时,重新设置按钮属性并刷新工作表显示 代码解析 创建按钮单元格 这段代码创建了一个按钮单元格并设置其属性。首先创建 Button 实例,然后使用链式调用设置文本和四个方向的边距(单位为像素),最后使用 setCellType 方法将其应用到工作表的第 1 行第 2 列。 动态修改按钮属性 这段代码获取选中单元格的类型,并判断是否为按钮类型。如果不是按钮,则禁用"更新"按钮。getCellType 方法返回单元格的类型对象,使用 instanceof 可以判断具体类型。 更新按钮样式 这段代码从输入框读取用户设置的属性值,并应用到按钮单元格。buttonBackColor 方法设置按钮背景色,支持颜色名称(如 'red')或十六进制颜色值。修改属性后需要调用 sheet.repaint() 刷新显示。 运行效果 工作表中显示一个文本为 "Margin" 的按钮单元格 点击选择该按钮单元格,右侧面板会显示当前的边距、文本和背景色属性 修改右侧面板中的属性值,点击"更新"按钮,按钮单元格会实时更新显示效果 可以设置按钮的四个方向边距、文本内容和背景颜色 API 参考 Button 构造函数 创建一个按钮单元格类型实例。 Button 常用方法 text(value?): 获取或设置按钮显示的文本内容 buttonBackColor(value?): 获取或设置按钮的背景颜色 marginLeft(value?): 获取或设置按钮相对于单元格左侧的边距(像素) marginTop(value?): 获取或设置按钮相对于单元格顶部的边距(像素) marginRight(value?): 获取或设置按钮相对于单元格右侧的边距(像素) marginBottom(value?): 获取或设置按钮相对于单元格底部的边距(像素) setCellType 方法 row: 行索引 col: 列索引 cellType: 单元格类型对象(如 Button 实例) sheetArea: 可选,指定工作表区域 getCellType 方法 获取指定位置的单元格类型对象。
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss")); initSpread(spread); }; function initSpread(spread) { var spreadNS = GC.Spread.Sheets; var sheet = spread.getSheet(0); sheet.bind(spreadNS.Events.SelectionChanged, function () { propertyChange(false); }); sheet.suspendPaint(); sheet.setColumnWidth(2, 120); sheet.setColumnWidth(1, 120); sheet.setRowHeight(1, 50); var b0 = new spreadNS.CellTypes.Button(); b0.text("Margin"); b0.marginLeft(15); b0.marginTop(7); b0.marginRight(15); b0.marginBottom(7); sheet.setCellType(1, 2, b0, spreadNS.SheetArea.viewport); sheet.setValue(1, 1, "ButtonCellType"); sheet.resumePaint(); _getElementById("changeProperty").addEventListener('click',function () { propertyChange(true); }); function propertyChange(isSet) { var sheet = spread.getActiveSheet(); var sels = sheet.getSelections(); if (sels && sels.length > 0) { var sel = getActualRange(sels[0], sheet.getRowCount(), sheet.getColumnCount()); var buttonCellType = sheet.getCellType(sel.row, sel.col); if (!(buttonCellType instanceof spreadNS.CellTypes.Button)) { _getElementById("changeProperty").setAttribute("disabled",'disabled'); return; } if (!isSet) { _getElementById("changeProperty").removeAttribute("disabled"); _getElementById("txtButtonCellMarginLeft").value=buttonCellType.marginLeft(); _getElementById("txtButtonCellMarginTop").value=buttonCellType.marginTop(); _getElementById("txtButtonCellMarginRight").value=buttonCellType.marginRight(); _getElementById("txtButtonCellMarginBottom").value=buttonCellType.marginBottom(); _getElementById("txtButtonCellText").value=buttonCellType.text(); _getElementById("txtButtonCellBackColor").value=buttonCellType.buttonBackColor(); } else { buttonCellType.marginLeft(parseInt(_getElementById("txtButtonCellMarginLeft").value)); buttonCellType.marginTop(parseInt(_getElementById("txtButtonCellMarginTop").value)); buttonCellType.marginRight(parseInt(_getElementById("txtButtonCellMarginRight").value)); buttonCellType.marginBottom(parseInt(_getElementById("txtButtonCellMarginBottom").value)); buttonCellType.text(_getElementById("txtButtonCellText").value); buttonCellType.buttonBackColor(_getElementById("txtButtonCellBackColor").value); } } sheet.repaint(); } function getActualRange(range, maxRowCount, maxColCount) { var row = range.row < 0 ? 0 : range.row; var col = range.col < 0 ? 0 : range.col; var rowCount = range.rowCount < 0 ? maxRowCount : range.rowCount; var colCount = range.colCount < 0 ? maxColCount : range.colCount; return new spreadNS.Range(row, col, rowCount, colCount); } } function _getElementById(id){ return document.getElementById(id); }
<!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-software/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/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"> <label>在 Spread 中选择按钮单元格,并使用这些文本框编辑其选项。</label> <div class="option-row"> <label>左外边距:</label> <input type="text" id="txtButtonCellMarginLeft" /> <label>上外边距:</label> <input type="text" id="txtButtonCellMarginTop" /> </div> <div class="option-row"> <label>右外边距:</label> <input type="text" id="txtButtonCellMarginRight" /> <label>下外边距:</label> <input type="text" id="txtButtonCellMarginBottom" /> </div> <div class="option-row"> <label>文本:</label> <input type="text" id="txtButtonCellText" /> <label>背景颜色:</label> <input type="text" id="txtButtonCellBackColor" /> </div> <div class="option-row"> <label></label> <input type="button" id="changeProperty" value="更新" /> </div> </div> </div> </body> </html>
.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; overflow: auto; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; } .option-row { padding-bottom: 8px; } label { padding-bottom: 4px; display: block; } input { width: 100%; padding: 4px 8px; box-sizing: border-box; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }