计算服务

SpreadJS提供了通过calculate API暂停公式计算和切换计算模式为手动的功能。

挂起计算服务 用户在批量调用API时,可以暂停计算服务,以获得更好的体验。 For example: 计算选项 SpreadJS支持计算模式 CalculationMode,可以设置为手动 manual或自动 automatic。 自动计算 (默认) 在相关数据发生更改时,如有内容输入或复制粘贴到单元格中,每次都计算所有脏单元格。 手动计算 只在输入或更新公式时计算该公式,并保持其依赖项为脏单元格。在剪切/复制粘贴时,该模式将设置公式和单元格的值,但不会重新计算任何公式。 计算 API 您可以通过调用calculate API来强制进行计算。 Calculate函数将重新构建、标记脏单元格、广播脏单元格并计算脏单元格。 首先,使用计算类型重新构建并标记公式引用的单元格为脏单元格。以下是GC.Spread.Sheets.CalculationType的列表: all - 默认的计算类型是将范围内的单元格标记为脏单元格以进行计算。 rebuild - 首先重新构建范围内的所有公式模型,然后将它们标记为脏单元格以进行计算。 minimal - 保持当前单元格的计算脏状态。 regular - 将不稳定和循环引用的单元格标记为脏单元格以进行计算。 广播脏单元格将递归地将工作簿中依赖于脏单元格的单元格设置为脏单元格。 最后一步是计算。在自动模式下,所有脏单元格将被计算。 在手动模式下,只计算公式引用的单元格,其他单元格将保持其脏状态。 例如:
window.onload = function () { var spread = new GC.Spread.Sheets.Workbook(_getElementById("ss")); initSpread(spread); }; function initSpread(spread) { var sheet = spread.getActiveSheet(); spread.setSheetCount(2); var sheet2 = spread.sheets[1]; spread.suspendPaint(); spread.suspendCalcService(); spread.options.iterativeCalculationMaximumIterations = 1; spread.options.iterativeCalculationMaximumChange = 10; sheet.setValue(0,0,"Tick:"); sheet.setFormula(0,1,"=REFRESH(B1+1,2,80)"); sheet.setValue(2,0,"Value:"); sheet.setFormula(2,1,"=IFERROR(CHOOSE(MOD(B1,16),5,-5,20,-2),0)+RAND()"); sheet.setValue(0,4,"Values:"); sheet.getRange(1,4,60,1).formula("=IF(MOD($B$1,60)=(ROW()-2),$B$3,E2)", true); sheet2.setValue(0,6,"Values:"); sheet2.getRange(1,6,60,1).formula("=OFFSET(Sheet1!$E$2,MOD(Sheet1!$B$1-1+ROW(),60),0)", true); sheet.setValue(1,6,'Value Sparkline:'); sheet.addSpan(2,6,6,6); sheet.setFormula(2,6,'=LINESPARKLINE(E2:E61,0,,,)'); sheet.setValue(10,6,'Sheet2 reorder values:'); sheet.addSpan(11,6,6,6); sheet.setFormula(11,6,'=LINESPARKLINE(Sheet2!G2:G61,0,,,)'); spread.resumeCalcService(); spread.resumePaint(); bindEvent(spread); } function bindEvent (spread) { _getElementById("enableCalcService").addEventListener('change', function () { if (this.checked) { spread.resumeCalcService(); } else { spread.suspendCalcService(); } }); _getElementById("calculationMode").addEventListener('change', function () { console.log(JSON.stringify(this.value)); if (this.value === "0") { spread.options.calculationMode = GC.Spread.Sheets.CalculationMode.auto; spread.calculate(false); } else { spread.options.calculationMode = GC.Spread.Sheets.CalculationMode.manual; } }); _getElementById("calculateSpread").addEventListener('click', function () { let type = _getElementById("calculationType").value; spread.calculate(GC.Spread.Sheets.CalculationType[type]); }); _getElementById("calculateSheet").addEventListener('click', function () { let type = _getElementById("calculationType").value; spread.calculate(GC.Spread.Sheets.CalculationType[type], spread.getActiveSheet().name()); }); _getElementById("calculateRange").addEventListener('click', function () { let type = _getElementById("calculationType").value; let sheet = spread.getActiveSheet(); let range = sheet.getSelections()[0]; spread.calculate(GC.Spread.Sheets.CalculationType[type], sheet.name()+"!"+GC.Spread.Sheets.CalcEngine.rangeToFormula(range)); }); } 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"> <div class="option-row"> <input style="width: 20px;float: left;" type="checkbox" id="enableCalcService" checked="checked" /> <label for="enableCalcService">enable Calculation Service</label> </div> <div class="option-row"> <p>Change the Calculation Mode and input formulas to see the calculation behavior. </p> <label for="calculationMode">Calculation Mode</label> <select id="calculationMode"> <option value="0">Automatic</option> <option value="1">Manual</option> </select> </div> <div class="option-row"> <p>Check the different calculate scopes with different calculation types in different calculation modes. </p> <label for="calculationType">Calculation Type</label> <select id="calculationType"> <option value="all">All</option> <option value="rebuild">Rebuild</option> <option value="minimal">Minimal</option> <option value="regular">Regular</option> </select> <button id="calculateSpread">Calculate Spread</button> <br /> <button id="calculateSheet">Calculate Sheet</button> <br /> <button id="calculateRange">Calculate Selection</button> </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 { margin-bottom: 5px; padding: 2px 4px; width: 100%; box-sizing: border-box; } body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }