calcWorker를 사용하면 SpreadJS가 웹 워커에서 셀 계산을 실행하고, UI 스레드는 사용자 작업에 집중할 수 있습니다. 이는 통합 문서에 많은 수식이 포함되어 있을 때 UI가 응답하지 않는 현상을 방지합니다.
이 기능을 사용하려면 plugins/gc.spread.sheets.calcworker.min.js 스크립트 파일을 참조하거나 npm 패키지 'spread-sheets-calc-worker'을 가져온 다음 incrementalCalculation을 활성화해야 합니다.
예를 들어:
incrementalCalculation이 true로 설정되면 셀 수식과 사용자 정의 이름이 웹 워커로 전달됩니다. 계산할 때 calcWorker는 UI 스레드에서 값을 가져오고 계산 결과를 UI 스레드로 전송합니다.
사용자 정의 함수는 메인 스레드에 등록되고 평가된 상태로 유지됩니다. 이렇게 하면 API를 이전 버전과 일치하게 유지할 수 있습니다.
조건부 서식, 도형, 차트는 최신 값을 사용하여 UI 스레드에서 평가됩니다.
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, function() {
let sheet = spread.getActiveSheet();
sheet.charts.add('Chart1', GC.Spread.Sheets.Charts.ChartType.line, 100, 100, 400, 300, "O3:O102");
}, 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="spreadjs culture" content="ko-kr"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css">
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets/dist/gc.spread.sheets.all.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-print/dist/gc.spread.sheets.print.min.js"
type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-io/dist/gc.spread.sheets.io.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-shapes/dist/gc.spread.sheets.shapes.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-charts/dist/gc.spread.sheets.charts.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-slicers/dist/gc.spread.sheets.slicers.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-calc-worker/dist/gc.spread.sheets.calcworker.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-pivot-addon/dist/gc.spread.pivot.pivottables.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-reportsheet-addon/dist/gc.spread.report.reportsheet.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-tablesheet/dist/gc.spread.sheets.tablesheet.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-ganttsheet/dist/gc.spread.sheets.ganttsheet.min.js" type="text/javascript"></script>
<script src="$DEMOROOT$/ko/purejs/node_modules/@mescius/spread-sheets-resources-ko/dist/gc.spread.sheets.resources.ko.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;
}