Node.jsをC++経由でセルの計算時間を短縮する

可能な使用シナリオ

通常、Workbook.calculateFormula()メソッドを一度呼び出し、その後個別のセルの計算済み値を取得することを推奨します。しかし、時にはユーザーは全体のワークブックを計算したくない場合もあります。特定のセルだけを計算したい場合、Aspose.CellsはCalculationOptions.getRecursive()プロパティを提供しており、これをfalseに設定することで個々のセルの計算時間を大幅に短縮できます。recursiveプロパティをtrueに設定すると、すべての依存セルが各呼び出し時に再計算されますが、falseの場合、依存セルは一度だけ計算され、その後再計算されません。

Cell.calculate()メソッドの計算時間を短縮

以下のサンプルコードは、CalculationOptions.getRecursive()プロパティの使用例です。このコードを指定されたサンプルExcelファイルで実行し、そのコンソール出力を確認してください。recursiveプロパティをfalseに設定することで、計算時間が大幅に短縮されることがわかります。また、このプロパティについての詳細理解のためにコメントもお読みください。

const AsposeCells = require("aspose.cells.node");
const path = require("path");

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "sample.xlsx");
// Loads the workbook which contains hidden external links
const workbook = new AsposeCells.Workbook(filePath);

// Test calculation time after setting recursive true
workbook.calculateFormula(); // Call calculateFormula method to initiate calculation

// Test calculation time after setting recursive false
workbook.calculateFormula(false); // Specify ignoreError as false
const AsposeCells = require("aspose.cells.node");
const path = require("path");

function testCalcTimeRecursive(rec) {
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Load your sample workbook
const workbook = new AsposeCells.Workbook(path.join(dataDir, "sample.xlsx"));

// Access first worksheet
const ws = workbook.getWorksheets().get(0);

// Set the calculation option, set recursive true or false as per parameter
const opts = new AsposeCells.CalculationOptions();
opts.setRecursive(rec);

// Start stop watch            
const start = process.hrtime();

// Calculate cell A1 one million times
for (let i = 0; i < 1000000; i++) {
ws.getCells().get("A1").calculate(opts);
}

// Stop the watch
const end = process.hrtime(start);

// Calculate elapsed time in seconds
const estimatedTime = end[0] + end[1] / 1e9;

// Print the elapsed time in seconds
console.log(`Recursive ${rec}: ${estimatedTime} seconds`);
}

// Call the function for testing
testCalcTimeRecursive(true);
testCalcTimeRecursive(false);

コンソール出力

上記サンプルコードの実行結果のコンソール出力例です。サンプルExcelファイルを使用しています。ご注意:出力は異なる場合がありますが、recursiveプロパティをfalseに設定した後の経過時間は、常にtrueに設定した場合よりも短くなります。

  
Recursive True: 96 seconds  

Recursive False: 42 seconds