基本应用

SpreadJS 提供了很多种单元格类型。它支持双向绑定(数据发生变化视图也会响应变化)。每一个单元格类型都提供了两种模式:展示模式和编辑模式。这些单元格允许你自定义单元格的行为,可以让你在你的应用里实现不同的输入方式。

想要设置单元格类型,首先就要创建一个单元格类型对象,然后使用 setCellType 方法将单元格类型设置到表单,单元格,列,行上。例如: 你可以通过将单元格类型绑定到列上这种方式来使用单元格类型。比如: 当你设置完单元格类型后,可以使用 getCellType 方法获取单元格类型。如果你想移除单元格类型,你可以将单元格类型的值设置为 null 或者 undefined。 SpreadJS 在 Spread 上提供了一个事件 (ButtonClicked) 。如果你点击了按钮,复选框或者超链接事件就会触发。例如:
window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), { sheetCount: 2 }); initSpread(spread); }; function initSpread(spread) { var spreadNS = GC.Spread.Sheets, sheet = spread.getSheet(0); var eventTrigger = document.getElementById("eventTrigger"); spread.bind(spreadNS.Events.ButtonClicked, function(e, args) { var text = ""; if (eventTrigger.value === "undefined") { eventTrigger.value = ""; } var sheet = args.sheet, row = args.row, col = args.col; var cellType = sheet.getCellType(row, col); if (cellType instanceof GC.Spread.Sheets.CellTypes.Button) { text = eventTrigger.value + "You click a button.\n"; } if (cellType instanceof GC.Spread.Sheets.CellTypes.CheckBox) { text = eventTrigger.value + "You click a check box.\n"; } if (cellType instanceof GC.Spread.Sheets.CellTypes.HyperLink) { text = eventTrigger.value + "You click a hyperlink.\n"; } eventTrigger.value = text; }); spread.bind(spreadNS.Events.CellClick, function(e, args) { var text = ""; if (eventTrigger.value === "undefined") { eventTrigger.value = ""; } var sheet = args.sheet, row = args.row, col = args.col; var cellType = sheet.getCellType(row, col); if (cellType instanceof GC.Spread.Sheets.CellTypes.ComboBox) { text = eventTrigger.value + "You click a combo box.\n"; } eventTrigger.value = text; }); sheet.suspendPaint(); sheet.name('Basic Usage'); sheet.setColumnWidth(2, 140); 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"); var c = new spreadNS.CellTypes.CheckBox(); c.isThreeState(false); c.textTrue("Checked!"); c.textFalse("Check Me!"); sheet.setCellType(2, 2, c, spreadNS.SheetArea.viewport); sheet.setValue(2, 1, "CheckBoxCellType"); var combo = new spreadNS.CellTypes.ComboBox(); combo.items([{ text: "Oranges", value: "11k" }, { text: "Apples", value: "15k" }, { text: "Grape", value: "100k" }]); combo.editorValueType(spreadNS.CellTypes.EditorValueType.text); sheet.getCell(3, 2, spreadNS.SheetArea.viewport).cellType(combo).value("Grape"); sheet.setValue(3, 1, "ComboBoxCellType"); var h = new spreadNS.CellTypes.HyperLink(); h.text("SpreadJS Overview"); sheet.setCellType(4, 2, h, spreadNS.SheetArea.viewport); sheet.getCell(4, 2, spreadNS.SheetArea.viewport).value("https://www.grapecity.com.cn/developer/spreadjs").hAlign(spreadNS.HorizontalAlign.center); sheet.setValue(4, 1, "HyperLinkCellType"); sheet.resumePaint(); var sheet1 = spread.getSheet(1); sheet1.name('DataBind'); sheet1.suspendPaint(); var _lines = ["Computers", "Washers", "Stoves"]; var _colors = ["Red", "Green", "Blue", "White"]; var _ratings = ["Terrible", "Bad", "Average", "Good", "Great", "Epic"]; var lineCellType = new spreadNS.CellTypes.ComboBox(); lineCellType.items(_lines); var colorCellType = new spreadNS.CellTypes.ComboBox(); colorCellType.items(_colors); var checkBoxCellType = new spreadNS.CellTypes.CheckBox(); var ratingCellType = new spreadNS.CellTypes.ComboBox(); ratingCellType.items(_ratings); var datasource = [ { name: "Stoves S0", line: "Washers", color: "Green", discontinued: true, rating: "Average" }, { name: "Computers C1", line: "Washers", color: "Green", discontinued: true, rating: "Average" }, { name: "Washers W3", line: "Washers", color: "Green", discontinued: true, rating: "Average" } ]; var colInfos = [ { name: "name", size: 100 }, { name: "line", cellType: lineCellType, size: 80 }, { name: "color", cellType: colorCellType }, { name: "discontinued", cellType: checkBoxCellType, size: 80 }, { name: "rating", cellType: ratingCellType } ]; sheet1.autoGenerateColumns = false; sheet1.setDataSource(datasource); sheet1.bindColumns(colInfos); sheet1.resumePaint(); };
<!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"> This text box will display information about the different cell types that are interacted with. <div class="option-row"> <textarea id="eventTrigger" style="width: 100%; height: 120px"></textarea> </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; }