Node.jsを介したC++による式の計算

数式の追加と結果の計算

Aspose.Cellsには組み込みの式計算エンジンがあります。これにより、デザイナーテンプレートからインポートした式の再計算だけでなく、ランタイムで追加された式の結果の計算もサポートしています。

Aspose.Cellsは、Microsoft Excelの多くの式や関数をサポートしています(サポートされる関数のリストを参照)。これらの関数はAPIまたはデザイナースプレッドシートを通じて使用可能です。Aspose.Cellsは、大規模な数学論理、文字列、ブール値、日付/時間、統計、データベース、検索、および参照式のセットをサポートしています。

セルに式を追加するには、CellクラスのgetFormula()プロパティまたはsetFormula(string, object)メソッドを使用します。式を適用する際は、Microsoft Excelでの作成と同じく文字列の先頭に等号(=)を付け、関数パラメータをカンマ(,)で区切ります。

式の結果を計算するには、ユーザーはWorkbookクラスのcalculateFormula()メソッドを呼び出してExcelファイル内のすべての式を処理させるか、WorksheetクラスのcalculateFormula(string)メソッドを呼び出してシート内のすべての式を処理させるか、または、Cellクラスのcalculate(CalculationOptions)メソッドを呼び出して特定のセルの式を処理させることができます。

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

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();

// Adding a new worksheet to the Excel object
const sheetIndex = workbook.getWorksheets().add();

// Obtaining the reference of the newly added worksheet by passing its sheet index
const worksheet = workbook.getWorksheets().get(sheetIndex);

// Adding a value to "A1" cell
worksheet.getCells().get("A1").putValue(1);

// Adding a value to "A2" cell
worksheet.getCells().get("A2").putValue(2);

// Adding a value to "A3" cell
worksheet.getCells().get("A3").putValue(3);

// Adding a SUM formula to "A4" cell
worksheet.getCells().get("A4").setFormula("=SUM(A1:A3)");

// Calculating the results of formulas
workbook.calculateFormula();

// Get the calculated value of the cell
const value = worksheet.getCells().get("A4").getValue().toString();

// Saving the Excel file
workbook.save(path.join(dataDir, "output.xls"));

数式に関する重要な点

数式の直接計算

Aspose.Cellsには、埋め込みファイルからインポートされた数式を計算するだけでなく、直接数式の結果を計算する機能があります。

ときには、シートに追加せずに式の結果を直接計算する必要があります。式に使用されるセルの値はすでにシートに存在し、それらの値の結果をMicrosoft Excelの式に基づいて見つけたい場合です。

Aspose.Cellsの式計算エンジンAPIを使用して、WorksheetからcalculateFormula(string, FormulaParseOptions, CalculationOptions, number, number, CalculationData)までの式の結果をシートに追加せずに計算できます。

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

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");

// Create a workbook
const workbook = new AsposeCells.Workbook();

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

// Put 20 in cell A1
const cellA1 = worksheet.getCells().get("A1");
cellA1.putValue(20);

// Put 30 in cell A2
const cellA2 = worksheet.getCells().get("A2");
cellA2.putValue(30);

// Calculate the Sum of A1 and A2
const results = worksheet.calculateFormula("=Sum(A1:A2)");

// Print the output
console.log("Value of A1: " + cellA1.getStringValue());
console.log("Value of A2: " + cellA2.getStringValue());
console.log("Result of Sum(A1:A2): " + results.toString());

上記のコードは次の出力を生成します:

Value of A1: 20
Value of A2: 30
Result of Sum(A1:A2): 50.0

数式を繰り返し計算する方法

ワークブックに多くの式があり、少しの変更を加えながら繰り返し計算する必要がある場合、パフォーマンス向上のために式の計算チェーンを有効にすることが役立ちます:formulaSettings.getEnableCalculationChain()

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

// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Load the template workbook
const workbook = new AsposeCells.Workbook(path.join(dataDir, "book1.xls"));

// Print the time before formula calculation
console.log(new Date());

// Set the CreateCalcChain as true
workbook.getSettings().getFormulaSettings().setEnableCalculationChain(true);

// Calculate the workbook formulas
workbook.calculateFormula();

// Print the time after formula calculation
console.log(new Date());

// Change the value of one cell
workbook.getWorksheets().get(0).getCells().get("A1").putValue("newvalue");

// Re-calculate those formulas which depend on cell A1
workbook.calculateFormula();

重要なこと

高度なトピック