自定义填充

除了鼠标拖拽操作之外,你也可以自定义填充数据。 它允许你在运行时设置, 这将会非常有用当你想给一片从数据源读出来的数据进行填充设置的时候。

有5种类型的填充数据: Auto: 自动填充类型。 Direction: 填充方向。 Linear:线性填充类型。 Growth: 增长填充类型。 Date: 日期填充类型。 SpreadJS 提供了一些方法来执行填充操作。
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss"), {sheetCount: 1}); initSpread(spread); }; function initSpread(spread) { var startRangeFormulaTextBox = new GC.Spread.Sheets.FormulaTextBox.FormulaTextBox(document.getElementById('startRange'), {rangeSelectMode: true}); var wholeRangeFormulaTextBox = new GC.Spread.Sheets.FormulaTextBox.FormulaTextBox(document.getElementById('wholeRange'), {rangeSelectMode: true}); startRangeFormulaTextBox.workbook(spread); wholeRangeFormulaTextBox.workbook(spread); spread.options.newTabVisible = false; var sheet = spread.getSheet(0); sheet.setValue(0, 0, 1); _getElementById("fillDirectionSet").style.display='none'; _getElementById("fillDateUnitSet").style.display='none'; _getElementById("fillStepAndStop").style.display='none'; _getElementById("btnFillAutobyDirection").style.display='none'; _getElementById("btnFillLinear").style.display='none'; _getElementById("btnFillGrowth").style.display='none'; _getElementById("btnFillDate").style.display='none'; _getElementById("fillMethods").addEventListener('change',function () { switch (_getElementById("fillMethods").value) { case '0': _getElementById("fillSeriesSet").style.display='block'; _getElementById("btnFillAuto").style.display='block'; _getElementById("fillDateUnitSet").style.display='none'; _getElementById("fillDirectionSet").style.display='none'; _getElementById("btnFillAutobyDirection").style.display='none'; _getElementById("btnFillLinear").style.display='none'; _getElementById("btnFillGrowth").style.display='none'; _getElementById("btnFillDate").style.display='none'; _getElementById("fillStepAndStop").style.display='none'; break; case '1': _getElementById("fillDirectionSet").style.display='block'; _getElementById("btnFillAutobyDirection").style.display='block'; _getElementById("fillDateUnitSet").style.display='none'; _getElementById("fillSeriesSet").style.display='none'; _getElementById("btnFillAuto").style.display='none'; _getElementById("btnFillLinear").style.display='none'; _getElementById("btnFillGrowth").style.display='none'; _getElementById("btnFillDate").style.display='none'; _getElementById("fillStepAndStop").style.display='none'; break; case '2': _getElementById("fillSeriesSet").style.display='block'; _getElementById("btnFillLinear").style.display='block'; _getElementById("fillStepAndStop").style.display='block'; _getElementById("fillDateUnitSet").style.display='none'; _getElementById("fillDirectionSet").style.display='none'; _getElementById("btnFillAuto").style.display='none'; _getElementById("btnFillAutobyDirection").style.display='none'; _getElementById("btnFillGrowth").style.display='none'; _getElementById("btnFillDate").style.display='none'; break; case '3': _getElementById("fillSeriesSet").style.display='block'; _getElementById("btnFillGrowth").style.display='block'; _getElementById("fillStepAndStop").style.display='block'; _getElementById("fillDateUnitSet").style.display='none'; _getElementById("fillDirectionSet").style.display='none'; _getElementById("btnFillAuto").style.display='none'; _getElementById("btnFillAutobyDirection").style.display='none'; _getElementById("btnFillLinear").style.display='none'; _getElementById("btnFillDate").style.display='none'; break; case '4': _getElementById("fillSeriesSet").style.display='block'; _getElementById("btnFillDate").style.display='block'; _getElementById("fillStepAndStop").style.display='block'; _getElementById("fillDateUnitSet").style.display='block'; _getElementById("fillDirectionSet").style.display='none'; _getElementById("btnFillAuto").style.display='none'; _getElementById("btnFillAutobyDirection").style.display='none'; _getElementById("btnFillGrowth").style.display='none'; _getElementById("btnFillLinear").style.display='none'; break; } }); _getElementById("btnFillAuto").addEventListener('click',function() { var sheet = spread.getActiveSheet(); var startRange = getRange(sheet, startRangeFormulaTextBox.text()); var wholeRange = getRange(sheet, wholeRangeFormulaTextBox.text()); if(parseInt(_getElementById("fillSeries").value) === 1) { wholeRange.rowCount = startRange.rowCount; } else { wholeRange.colCount = startRange.colCount; } sheet.fillAuto(startRange, wholeRange, { series: parseInt(_getElementById("fillSeries").value), fillType: GC.Spread.Sheets.Fill.FillType.auto }); }); _getElementById("btnFillAutobyDirection").addEventListener('click',function () { var sheet = spread.getActiveSheet(); var startRange = getRange(sheet, startRangeFormulaTextBox.text()); var wholeRange = getRange(sheet, wholeRangeFormulaTextBox.text()); sheet.fillAuto(startRange, wholeRange, { direction: parseInt(_getElementById("fillDirection").value), fillType: GC.Spread.Sheets.Fill.FillType.direction }); }); _getElementById("btnFillLinear").addEventListener('click',function () { var sheet = spread.getActiveSheet(); var startRange = getRange(sheet, startRangeFormulaTextBox.text()); var wholeRange = getRange(sheet, wholeRangeFormulaTextBox.text()); var step = parseInt(_getElementById("step").value); var stopString = _getElementById("stop").value; var stop; if (stopString !== '') { stop = parseInt(stopString); } sheet.fillAuto(startRange, wholeRange, { series: parseInt(_getElementById("fillSeries").value), step: step, stop: stop, fillType: GC.Spread.Sheets.Fill.FillType.linear }); }); _getElementById("btnFillGrowth").addEventListener('click',function () { var sheet = spread.getActiveSheet(); var startRange = getRange(sheet, startRangeFormulaTextBox.text()); var wholeRange = getRange(sheet, wholeRangeFormulaTextBox.text()); var step = parseInt(_getElementById("step").value); var stopString = _getElementById("stop").value; var stop; if (stopString !== '') { stop = parseInt(stopString); } sheet.fillAuto(startRange, wholeRange, { series: parseInt(_getElementById("fillSeries").value), step: step, stop: stop, fillType: GC.Spread.Sheets.Fill.FillType.growth }); }); _getElementById("btnFillDate").addEventListener('click',function () { var sheet = spread.getActiveSheet(); var startRange = getRange(sheet, startRangeFormulaTextBox.text()); var wholeRange = getRange(sheet, wholeRangeFormulaTextBox.text()); var step = parseInt(_getElementById("step").value); var stop = null; if (_getElementById("stop").value !== '') { stop = new Date(_getElementById("stop").value); } sheet.fillAuto(startRange, wholeRange, { series: parseInt(_getElementById("fillSeries").value), unit: parseInt(_getElementById("fillDateUnit").value), step: step, stop: stop, fillType: GC.Spread.Sheets.Fill.FillType.date }); }); }; function getRange(sheet, rangeString) { try{ var rangeObject = GC.Spread.Sheets.CalcEngine.formulaToExpression(sheet, rangeString).getRange(0, 0); var range = new GC.Spread.Sheets.Range(rangeObject.row, rangeObject.col, rangeObject.rowCount, rangeObject.colCount); return range; }catch (e){ throw new Error("Select Ranges are not right"); } } 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"> <p> Use the "Start Range" field to select a range to start the fill, "Whole Range" to select the entire range to fill, and the "Fill Methods" and "Fill Series" fields to change how the fill happens. </p> <p> Try selecting Sheet1!A1 as the start range and Sheet1!A1:C2 as the whole range, keep fillAuto as the fill method and column as fill series. Then click the "fillAuto" button and see the sheet fill those cells. </p> <div class="option-row"> <label>Start Range</label> <div id="startRange" spellcheck="false" style="display:inline-block;border: 1px solid #808080;width:134px;vertical-align: middle;"> </div> </div> <div class="option-row"> <label>Whole Range</label> <div id="wholeRange" spellcheck="false" style="display:inline-block;border: 1px solid #808080;width:134px;vertical-align: middle;"> </div> </div> <div class="option-row"> <label>Fill Methods</label> <select id="fillMethods"> <option value='0' selected>fillAuto</option> <option value='1'>fillAutobyDirection</option> <option value='2'>fillLinear</option> <option value='3'>fillGrowth</option> <option value='4'>fillDate</option> </select> </div> <div class="option-row"> <div id="fillSeriesSet"> <label>FillSeries</label> <select id="fillSeries"> <option value='0' selected>Column</option> <option value='1'>Row</option> </select> </div> <div id="fillDirectionSet"> <label>FillDirection</label> <select id="fillDirection"> <option value='0' selected>Left</option> <option value='1'>Right</option> <option value='2'>Up</option> <option value='3'>Down</option> </select> </div> </div> <div class="option-row" id="fillDateUnitSet"> <label>FillDateUnit</label> <select id="fillDateUnit"> <option value='0' selected>Day</option> <option value='1'>Weekday</option> <option value='2'>Month</option> <option value='3'>Year</option> </select> </div> <div class="option-row" id="fillStepAndStop"> <label>Step</label> <input type="text" id="step" /> <label>Stop</label> <input type="text" id="stop" /> </div> <div class="option-row"> <label></label> <input type="button" id="btnFillAuto" value="fillAuto" /> <input type="button" id="btnFillAutobyDirection" value="fillAutobyDirection" /> <input type="button" id="btnFillLinear" value="fillLinear" /> <input type="button" id="btnFillGrowth" value="fillGrowth" /> <input type="button" id="btnFillDate" value="fillDate" /> </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; padding: 12px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .option-row { font-size: 14px; padding: 5px; margin-top: 10px; } input, select { padding: 4px 8px; } input { width: 100%; margin-top: 6px; box-sizing: border-box; } label { display:inline-block; min-width: 85px; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }