計算処理

SpreadJS では、calculate API を使用することで、計算処理を一時停止することや計算モードを手動に切り替えることができます。

計算処理の一時停止 ユーザーは、バッチ処理でAPIを呼び出す際に、パフォーマンス向上のために計算処理を一時停止することができます。 例: 計算のオプション SpreadJSは、計算処理モードCalculationModeを手動または自動に設定することができます。 自動計算モード (デフォルト) セルに何かを入力したり、コピー・貼り付けを実行したりするように関連データが変更されるたびに、再計算が必要と認識したセルを自動的に計算します。 手動計算モード 数式が入力または更新された場合のみ、数式を再評価してダーティな状態を保持します。この場合、切り取りやコピー・貼り付けを実行すると、数式とセルの値が設定されますが、数式の再計算は行いません。 Calculate API calculate APIを呼び出すことで、強制的に計算処理を行うことができます。 Calculate functionを使用して、ダーティセルを再構築し、ダーティと標記してブロードキャストし、計算を行います。 まず、計算タイプによって数式参照のセルをダーティとして標記し、再構築を行います。`GC.Spread.Sheets.CalculationType`の一覧は以下となります: all - デフォルト。範囲内のセルをダーティに標記して計算します。 rebuild - 範囲内にあるすべての数式を再構築して、ダーティに標記して計算します。 minimal - 現在のセルをダーティな状態を維持します。 regular - 自動再計算と循環参照のセルをダーティに標記して計算します。 再帰的にワークブックでダーティ状態のセルを参照したセルをダーティに標記します。 最後は計算を行います。自動計算モードの場合、ダーティに標記した全てのセルの再計算を行います。 手動計算モードの場合、数式参照のセルのみ再計算され、他のセルはダーティ状態を保持します。 例:
<template> <div class="sample-tutorial"> <gc-spread-sheets class="sample-spreadsheets" @workbookInitialized="initSpread"> <gc-worksheet> </gc-worksheet> </gc-spread-sheets> <div class="options-container"> <div class="option-row"> <input style="width: 20px;float: left;" type="checkbox" id="enableCalcService" checked="checked" @change="enableCalcService($event)" /> <label for="enableCalcService">計算処理を有効にする</label> </div> <div class="option-row"> <p>計算処理モードを変更し、数式を入力して動作確認できます。</p> <label for="calculationMode">計算処理モード</label> <select id="calculationMode" @change="calculationMode"> <option value="0">Automatic</option> <option value="1">Manual</option> </select> </div> <div class="option-row"> <p>計算範囲を指定して、計算処理モードや計算タイプを変更できます。</p> <label for="changeType">計算タイプ</label> <select id="changeType" @change="changeType"> <option value="all">All</option> <option value="rebuild">Rebuild</option> <option value="minimal">Minimal</option> <option value="regular">Regular</option> </select> <button id="calculateSpread" @click="calculateSpread($event)">スプレッド範囲の計算</button> <br /> <button id="calculateSheet" @click="calculateSheet($event)">シート範囲の計算</button> <br /> <button id="calculateRange" @click="calculateRange($event)">選択範囲の計算</button> </div> </div> </div> </template> <script setup> import '@mescius/spread-sheets-vue'; import { ref } from "vue"; import GC from '@mescius/spread-sheets'; import '@mescius/spread-sheets-resources-ja'; GC.Spread.Common.CultureManager.culture("ja-jp"); const spreadRef = ref(null); let calculationType; let initSpread = function (spread) { spreadRef.value = spread; let sheet = spread.getActiveSheet(); spread.setSheetCount(2); let 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(); } let enableCalcService = function (e) { if (e.target.checked) { spreadRef.value.resumeCalcService(); } else { spreadRef.value.suspendCalcService(); } } let calculationMode = function (e) { let spread = spreadRef.value; if (e.target.value === "0") { spread.options.calculationMode = GC.Spread.Sheets.CalculationMode.auto; spread.calculate(false); } else { spread.options.calculationMode = GC.Spread.Sheets.CalculationMode.manual; } } let changeType = function (e) { calculationType = GC.Spread.Sheets.CalculationType[e.target.value]; } let calculateSpread = function (e) { spreadRef.value.calculate(calculationType); } let calculateSheet = function (e) { spreadRef.value.calculate(calculationType, spreadRef.value.getActiveSheet().name()); } let calculateRange = function (e) { let sheet = spreadRef.value.getActiveSheet(); let range = sheet.getSelections()[0]; spreadRef.value.calculate(calculationType, sheet.name() + "!" + GC.Spread.Sheets.CalcEngine.rangeToFormula(range)); } </script> <style scoped> #app { height: 100%; } .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; } </style>
<!DOCTYPE html> <html style="height:100%;font-size:14px;"> <head> <meta charset="utf-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <title>SpreadJS VUE</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link rel="stylesheet" type="text/css" href="$DEMOROOT$/ja/vue3/node_modules/@mescius/spread-sheets/styles/gc.spread.sheets.excel2013white.css"> <script src="$DEMOROOT$/ja/vue3/node_modules/systemjs/dist/system.src.js"></script> <script src="./systemjs.config.js"></script> <script src="./compiler.js" type="module"></script> <script> var System = SystemJS; System.import("./src/app.js"); System.import('$DEMOROOT$/ja/lib/vue3/license.js'); </script> </head> <body> <div id="app"></div> </body> </html>
(function (global) { SystemJS.config({ transpiler: 'plugin-babel', babelOptions: { es2015: true }, paths: { // paths serve as alias 'npm:': 'node_modules/' }, packageConfigPaths: [ './node_modules/*/package.json', "./node_modules/@mescius/*/package.json", "./node_modules/@babel/*/package.json", "./node_modules/@vue/*/package.json" ], map: { 'vue': "npm:vue/dist/vue.esm-browser.js", 'tiny-emitter': 'npm:tiny-emitter/index.js', 'plugin-babel': 'npm:systemjs-plugin-babel/plugin-babel.js', "systemjs-babel-build": "npm:systemjs-plugin-babel/systemjs-babel-browser.js", '@mescius/spread-sheets': 'npm:@mescius/spread-sheets/index.js', '@mescius/spread-sheets-resources-ja': 'npm:@mescius/spread-sheets-resources-ja/index.js', '@mescius/spread-sheets-vue': 'npm:@mescius/spread-sheets-vue/index.js' }, meta: { '*.css': { loader: 'systemjs-plugin-css' }, '*.vue': { loader: "../plugin-vue/index.js" } } }); })(this);