通过行为改变值

SpreadJS 支持一些默认的行为, 例如剪贴/复制/粘贴行为, 拖动释放行为, 和拖动填充行为等等。这些行为都可以改变单元格的值。

你可以复制或剪贴一个区域, 并将此区域粘贴到当前表单或另一个表单上。在这个用例中, 单元格的值和样式被同时复制。不过, 如果你将 Spread 控件的内容粘贴到其他的应用程序中, 例如记事本或是 Excel, 那么只有值被粘贴。 你也可以从其他的应用程序中复制或剪贴数据并且粘贴到 Spread 控件中。 如果你剪贴或复制一些单元格, 控件将显示一个剪贴复制提示框在你复制的区域以此告诉你哪些区域你复制了。你可以调用 cutCopyIndicatorVisible 方法来控制是否显示剪贴复制提示框, 也可以调用 cutCopyIndicatorBorderColor 方法来设置剪贴复制提示框的边框颜色, 示例代码如下: 你可拖拽一片区域单元格并将他们释放在其他的单元格中。如果你在释放时点击 Ctrl 键, 那么你将会复制被拖拽区域的单元格。如果你不点击 Ctrl 键, 那么被拖拽区域的单元格将被剪切。 SpreadJS 提供了 allowUserDragDrop 方法来控制用户是否能够拖拽和释放单元格。此外, 你可以调用 showDragDropTip 方法来控制在拖拽过程中是否显示提示框。提示框显示在你正在拖拽的位置。
window.onload = function() { var spread = new GC.Spread.Sheets.Workbook(_getElementById('ss'), { sheetCount: 1 }); initSpread(spread); }; function initSpread(spread) { var sheet = spread.getActiveSheet(); spread.fromJSON(data[0]); _getElementById('chkShowDragDropTip').addEventListener('click', function() { spread.options.showDragDropTip = _getElementById('chkShowDragDropTip').checked; }); _getElementById('chkAllowUserDragDrop').addEventListener('click', function() { spread.options.allowUserDragDrop = _getElementById('chkAllowUserDragDrop').checked; }); _getElementById('cutCopyIndicatorVisible').addEventListener('change', function() { var value = _getElementById('cutCopyIndicatorVisible').checked; spread.options.cutCopyIndicatorVisible = value; }); _getElementById('setCutCopyIndicatorBorderColor').addEventListener('click', function() { var value = _getElementById('cutCopyIndicatorBorderColor').value; spread.options.cutCopyIndicatorBorderColor = value; }); _getElementById('setIsProtected').addEventListener('click', function() { var sheet = spread.getActiveSheet(); var value = _getElementById('setIsProtected').checked; sheet.options.isProtected = value; }); } 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$/spread/source/data/data.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> <script src="data.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"> <input id="chkAllowUserDragDrop" type="checkbox" checked="checked" /> <label for="chkAllowUserDragDrop" class="sizedLabel">AllowUserDragDrop</label> </div> <div class="option-row"> <input id="chkShowDragDropTip" type="checkbox" checked="checked" /> <label for="chkShowDragDropTip" class="sizedLabel">ShowDragDropTip</label> </div> <div class="option-row"> <input type="checkbox" id="cutCopyIndicatorVisible" checked="checked" /> <label for="cutCopyIndicatorVisible" class="sizedLabel">CutCopyIndicatorVisible</label> </div> <div class="option-row"> <input type="checkbox" id="setIsProtected" /> <label for="setIsProtected" class="sizedLabel">IsProtected</label> </div> <hr> <div class="option-row"> <label>Enter a color name like "red" or "blue" for change the color of the cut/copy indicator border.</label> </div> <label>Cut/Copy Indicator Border Color:</label> <div class="option-row"> <input type="text" style="margin:0 8px; width: 150px;" id="cutCopyIndicatorBorderColor" /> <input type="button" value="Set" id="setCutCopyIndicatorBorderColor" /> </div> </div> </div> </body> </html>
var data = [{ "version":"12.0.0", "tabStripRatio":0.6, "sheetCount":1, "sheets":{ "Sheet1":{ "name":"Sheet1", "rowCount":114, "columnCount":21, "activeRow":2, "activeCol":2, "data":{ "dataTable":{ "0":{ "0":{ "value":"Salesperson", "style": { "foreColor" : "#FFFFFF", "backColor" : "#808080", "hAlign" : 1 } }, "1":{ "value":"Region", "style": { "foreColor" : "#FFFFFF", "backColor" : "#808080", "hAlign" : 1 } }, "2":{ "value":"Sales", "style": { "foreColor" : "#FFFFFF", "backColor" : "#808080", "hAlign" : 1 } } }, "1":{ "0":{ "value":"Ally", "style":{"hAlign" : 1} }, "1":{ "value":"North", "style":{"hAlign" : 1} }, "2":{ "value":24234324, "style":{"hAlign" : 1, "formatter": "$#,##0"} } }, "2":{ "0":{ "value":"Tom", "style":{"hAlign" : 1} }, "1":{ "value":"South", "style":{"hAlign" : 1} }, "2":{ "value":2342342, "style":{"hAlign" : 1, "formatter": "$#,##0"} } }, "3":{ "0":{ "value":"Jack", "style":{"hAlign" : 1} }, "1":{ "value":"South", "style":{"hAlign" : 1} }, "2":{ "value":324234, "style":{"hAlign" : 1, "formatter": "$#,##0"} } }, "4":{ "0":{ "value":"John", "style":{"hAlign" : 1} }, "1":{ "value":"West", "style":{"hAlign" : 1} }, "2":{ "value":2342443, "style":{"hAlign" : 1, "formatter": "$#,##0"} } }, "5":{ "0":{ "value":"Lily", "style":{"hAlign" : 1} }, "1":{ "value":"North", "style":{"hAlign" : 1} }, "2":{ "value":2342342, "style":{"hAlign" : 1, "formatter": "$#,##0"} } }, "6":{ "0":{ "value":"Linda", "style":{"hAlign" : 1} }, "1":{ "value":"East", "style":{"hAlign" : 1} }, "2":{ "value":5857858, "style":{"hAlign" : 1, "formatter": "$#,##0"} } }, "7":{ "0":{ "value":"Will", "style":{"hAlign" : 1} }, "1":{ "value":"North", "style":{"hAlign" : 1} }, "2":{ "value":437587965, "style":{"hAlign" : 1, "formatter": "$#,##0"} } } } }, "defaults": {"colHeaderRowHeight": 20, "colWidth": 120, "rowHeaderColWidth": 40, "rowHeight": 26}, } } }];
label { display:inline-block; } .sizedLabel { width: 180px; } hr { border-color: #fff; opacity: .2; margin-top: 20px; } .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 { margin-bottom: 6px; } input { padding: 4px 6px; } input[type=button] { margin-top: 6px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }