增量计算

SpreadJS 支持增量计算模式,将大型公式的计算任务分解为多个小批次执行,在每个批次之间响应 UI 交互。这避免了在包含大量公式的工作簿中进行计算时界面长时间冻结,提升了用户体验。

概述 本 Demo 展示了如何启用增量计算功能,通过将计算任务分批执行,防止 UI 在计算大量公式时冻结。Demo 演示了如何通过配置选项启用增量计算、在文件加载时应用增量计算,以及如何通过状态栏实时查看计算进度。 实现思路 初始化 Workbook 和 StatusBar,并将状态栏绑定到 spread 实例,状态栏会自动显示计算进度 为复选框添加 change 事件监听器,动态切换增量计算开关 为"Open"按钮添加点击事件,根据文件类型调用 open() 或 import() 方法 在加载文件时设置增量加载模式(openMode: 2)和增量计算选项(incrementalCalculation: true) 页面加载时自动打开示例文件,演示增量计算效果 代码解析 启用增量计算 这段代码为复选框添加了 change 事件监听器。当用户勾选或取消复选框时,会动态切换 spread.options.incrementalCalculation 选项,启用或禁用增量计算模式。 绑定状态栏显示计算进度 创建状态栏实例并绑定到 spread 后,状态栏会自动显示计算进度。当增量计算启用时,进度以百分比格式显示;对于迭代计算,则以迭代轮次显示。 加载文件时应用增量计算 加载文件时,通过 options 参数设置增量加载模式和增量计算选项。openMode: 2 表示增量加载模式,incrementalCalculation 根据复选框状态决定是否启用增量计算。对于 .sjs 文件使用 open() 方法,其他格式使用 import() 方法。 运行效果 页面加载后会自动打开一个包含大量公式的示例文件,并启用增量计算 计算过程中,底部状态栏会实时显示计算进度(如"Calculating 45%") 用户可以取消勾选"Enable incremental calculation"复选框,改为同步计算模式 点击"Open"按钮可以打开其他文件,加载时会应用当前选择的增量计算设置 点击"Calculate All"按钮可以手动触发全量计算 API 参考 incrementalCalculation 选项 是否启用增量计算模式,默认为 false。启用后会将计算任务分批执行,避免 UI 冻结。 open 方法 从 .sjs 压缩文件加载工作簿状态。 file: 压缩的电子表格数据文件 successCallback: 加载成功的回调函数 errorCallback: 加载失败的回调函数 openOptions: 打开选项,包含 incrementalCalculation、openMode 等 import 方法 从 Excel、SSJSON 或 CSV 文件导入工作簿状态。参数与 open 方法类似。 CalculationProgress 事件 在增量计算过程中触发,用于跟踪计算进度。状态栏会自动监听此事件并显示进度。
window.onload = function () { let spread = new GC.Spread.Sheets.Workbook(document.getElementById("ss")); let statusBar = new GC.Spread.Sheets.StatusBar.StatusBar(document.getElementById('statusBar')); statusBar.bind(spread); let incrementalCalculationElement = document.getElementById("enable-incremental-calculation"); incrementalCalculationElement.addEventListener("change", function () { spread.options.incrementalCalculation = incrementalCalculationElement.checked; }); document.getElementById('open').onclick = function () { let file = document.querySelector('#selectedFile').files[0]; if (!file) { return; } let options = { openMode: 2 /* incremental loading */, fullRecalc: true, dynamicReferences: false, incrementalCalculation: incrementalCalculationElement.checked }; if (isSJSFile(file)) { spread.open(file, function() {}, function() {}, options); } else { spread.import(file, function() {}, function() {}, options); } }; document.getElementById('calculate-all').onclick = function () { spread.calculate(); }; fetch(getFileUrl()).then(res => res.blob()).then(file => spread.open(file, null, null, { openMode: 2 /* incremental loading */, fullRecalc: true, incrementalCalculation: true })); }; function isSJSFile (file) { let fileName = file.name; let extensionName = fileName.substring(fileName.lastIndexOf(".") + 1); return extensionName === "sjs"; } function getFileUrl () { return '$DEMOROOT$/spread/source/data/incremental-calculation.sjs'; }
<!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" /> <meta name="spreadjs culture" content="zh-cn" /> <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-print/dist/gc.spread.sheets.print.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-io/dist/gc.spread.sheets.io.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-charts/dist/gc.spread.sheets.charts.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-slicers/dist/gc.spread.sheets.slicers.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-pivot-addon/dist/gc.spread.pivot.pivottables.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-reportsheet-addon/dist/gc.spread.report.reportsheet.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-tablesheet/dist/gc.spread.sheets.tablesheet.min.js" type="text/javascript"></script> <script src="$DEMOROOT$/zh/purejs/node_modules/@grapecity-software/spread-sheets-ganttsheet/dist/gc.spread.sheets.ganttsheet.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/FileSaver.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 class="sample-container"> <div id="ss" class="sample-spreadsheets"></div> <div id="statusBar"></div> </div> <div class="options-container"> <div class="option-row"> <div class="inputContainer"> <input id="selectedFile" type="file" name="files[]"accept=".sjs, .xlsx, .xlsm, .ssjson, .json, .csv" /> <button class="settingButton" id="open">Open</button> <div class="options"> <div class="item"><input id="enable-incremental-calculation" type="checkbox" checked><label for="enable-incremental-calculation">Enable incremental calculation</label></div> </div> <button class="settingButton" id="calculate-all">Calculate All</button> </div> </div> </div> </div></body> </html>
body { position: absolute; top: 0; bottom: 0; left: 0; right: 0; } .sample-tutorial { position: relative; height: 100%; overflow: hidden; } .sample-container { width: calc(100% - 280px); height: 100%; float: left; } .sample-spreadsheets { width: 100%; height: calc(100% - 25px); overflow: hidden; } #statusBar { bottom: 0; height: 25px; width: 100%; position: relative; } .options-container { float: right; width: 280px; height: 100%; box-sizing: border-box; background: #fbfbfb; overflow: auto; } .sample-options { z-index: 1000; } .inputContainer { width: 100%; height: auto; border: 1px solid #eee; padding: 6px 12px; margin-bottom: 10px; box-sizing: border-box; } .settingButton { color: #fff; background: #82bc00; outline: 0; line-height: 1.5715; position: relative; display: inline-block; font-weight: 400; white-space: nowrap; text-align: center; height: 32px; padding: 4px 15px; font-size: 14px; border-radius: 2px; user-select: none; cursor: pointer; border: 1px solid #82bc00; box-sizing: border-box; margin-bottom: 10px; margin-top: 10px; } .settingButton:hover { color: #fff; border-color: #88b031; background: #88b031; } .options-title { font-weight: bold; margin: 4px 2px; } #selectedFile { width: 180px; } #saveFileType { width: 120px; height: 31px; } .options .item { margin: 5px 0px; display: flex; } .save-options .item { margin: 5px 0px; display: flex; } label { margin-left: 3px; } select, input[type="text"], input[type="number"] { display: inline-block; margin-left: auto; width: 120px; font-weight: 400; outline: 0; line-height: 1.5715; border-radius: 2px; border: 1px solid #F4F8EB; box-sizing: border-box; }