Custom Fill

Besides the mouse drag actions, you can also customize the fill data. This allows you to fill data at run-time, which can be useful when you want to customize how data is filled after loading from a database.

<p>There are five types of fill data:</p> <ul> <li><strong>Auto: The auto fill type.</strong></li> <li><strong>Direction: The direction fill type.</strong></li> <li><strong>Linear: The linear fill type.</strong></li> <li><strong>Growth: The growth fill type.</strong></li> <li><strong>Date: The date fill type.</strong></li> </ul> <p>SpreadJS provides methods to perform the fill.</p> <pre><code class="hljs js language-js"> <span class="hljs-comment">// Fills the specified range automatically.</span> <span class="hljs-comment">// When the value is a string, the value is copied to other cells.</span> <span class="hljs-comment">// When the value is a number, the new value is generated by the TREND formula.</span> sheet.fillAuto(startRange, fillRange, {<span class="hljs-attr">series</span>: GC.Spread.Sheets.Fill.FillSeries.column, <span class="hljs-attr">fillType</span>: GC.Spread.Sheets.Fill.FillType.auto}); <span class="hljs-comment">// Fills the specified range in the specified direction.</span> sheet.fillAuto(startRange, fillRange, {<span class="hljs-attr">direction</span>: GC.Spread.Sheets.Fill.FillDirection.left, <span class="hljs-attr">fillType</span>: GC.Spread.Sheets.Fill.FillType.direction}); <span class="hljs-comment">// Fills the specified range linear trend when the source value type is number.</span> <span class="hljs-comment">// The next value is generated by the step and stop values.</span> <span class="hljs-comment">// The next value is computed by adding the step value to the current cell value.</span> sheet.fillAuto(startRange, fillRange, {<span class="hljs-attr">series</span>: GC.Spread.Sheets.Fill.FillSeries.column, <span class="hljs-attr">step</span>: step, <span class="hljs-attr">stop</span>: stop, <span class="hljs-attr">fillType</span>: GC.Spread.Sheets.Fill.FillType.linear}); <span class="hljs-comment">// Fills the specified range growth trend when the source value type is number.</span> <span class="hljs-comment">// The next value is generated by the step and stop values.</span> <span class="hljs-comment">// The next value is computed by multiplying the step value with the current cell.</span> sheet.fillAuto(startRange, fillRange, {<span class="hljs-attr">series</span>: GC.Spread.Sheets.Fill.FillSeries.column, <span class="hljs-attr">step</span>: step, <span class="hljs-attr">stop</span>: stop, <span class="hljs-attr">fillType</span>: GC.Spread.Sheets.Fill.FillType.growth}; <span class="hljs-comment">// Fills the specified range when the source value type is date.</span> <span class="hljs-comment">// The next value is generated by adding the step value to the current value. </span> <span class="hljs-comment">// The step value is affected by the fill date unit.</span> sheet.fillAuto(startRange, fillRange, {<span class="hljs-attr">series</span>: GC.Spread.Sheets.Fill.FillSeries.column, <span class="hljs-attr">unit</span>: GC.Spread.Sheets.Fill.FillDateUnit.month, <span class="hljs-attr">step</span>: step, <span class="hljs-attr">stop</span>: stop, <span class="hljs-attr">fillType</span>: GC.Spread.Sheets.Fill.FillType.date}); </code></pre>
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 charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/en/purejs/node_modules/@grapecity/spread-sheets/dist/gc.spread.sheets.all.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; }