数式

ReoGridは、セル参照、範囲参照、名前付き範囲、組み込み関数、カスタム関数を備えたExcel互換の数式計算をサポートしています。

名前空間

using unvell.ReoGrid;

数式の設定

セルの編集による設定

等号(=)で始まる値を入力すると、数式が設定されます。

28

プログラムによる設定

// Method 1: Set data starting with "="
sheet["E3"] = "=(A3+B3)*C3";

// Method 2: Via cell instance (no "=" prefix needed)
var cell = sheet.Cells["E3"];
cell.Formula = "(A3+B3)*C3";

// Method 3: Via worksheet method
sheet.SetCellFormula("E3", "(A3+B3)*C3");
sheet.SetCellFormula(new CellPosition("E3"), "(A3+B3)*C3");
sheet.SetCellFormula(2, 4, "(A3+B3)*C3");

数式の取得

// Via worksheet method
string formula = sheet.GetCellFormula("C1");
string formula = sheet.GetCellFormula(0, 2);
string formula = sheet.GetCellFormula(new CellPosition("C1"));

// Via cell instance
var cell = sheet.Cells["C1"];
string formula = cell.Formula;

数式の状態を確認する

var cell = sheet.Cells["C1"];

// Check if the cell has a formula
bool hasFormula = cell.HasFormula;

// Check formula calculation status
FormulaStatus status = cell.FormulaStatus;

FormulaStatus列挙型

説明
Normalエラーは検出されていません
SyntaxError数式に構文エラーがあり、解析できません
CircularReference循環参照が検出されました
InvalidValue計算中に無効な値の型が検出されました(Excelの#VALUE!
InvalidReferenceセルまたは範囲の参照が無効です(Excelの#REF!
InvalidName参照された名前が見つかりません(Excelの#NAME!
MismatchedParameter関数が間違ったパラメータの型または数で呼び出されました
UnspecifiedError解析または計算中に不明なエラーが発生しました

数式の結果を取得する

編集が完了すると、数式は即座に解析および計算されます。

29

// Method 1: Access cell data directly
object value = sheet["E3"];

// Method 2: Via cell instance
var cell = sheet.Cells["E3"];
object value = cell.Data;

数式の値の型

ReoGridは数式の値に.NETネイティブ型を使用します。

.NET型用途
doubleすべての数値(int、long、floatはdoubleに変換されます)
stringテキスト値
boolブール値
DateTime日付と時刻の値
sheet["E3"] = "=(A3+B3)*C3";
System.Diagnostics.Debug.Assert(sheet["E3"] is double);  // true

テキストが数式として扱われるのを防ぐ

数式の解析を防ぐには、シングルクォート(')をプレフィックスとして付けます。

sheet["A1"] = "'=10";  // Cell displays "=10" as text

セル参照

ReoGridは2つのアドレス形式をサポートしています。

形式種類説明
A1相対数式がコピー/移動されるときに調整されます
$A$1絶対数式がコピー/移動されても固定されます

13_2

範囲参照

開始セル:終了セルの形式で範囲を参照します。

91

結果: 92

名前付き範囲の参照

数式内で名前付き範囲をその名前で直接参照できます。

sheet.DefineNamedRange("myrange", "A1:B5");
sheet.Cells["E10"].Formula = "SUM(myrange)";

名前付き範囲の定義については、名前付き範囲を参照してください。

数式の削除

// Delete formula from a single cell
sheet.DeleteCellFormula("E3");
sheet.DeleteCellFormula(new CellPosition("E3"));
sheet.DeleteCellFormula(2, 4);

// Clear formulas from a range
sheet.ClearRangeContent("A1:B5", CellElementFlag.Formula);

数式の参照リストを取得する

数式が参照しているセルと範囲のリストを取得します。

sheet["H8"] = "=A1+B1-SUM(A1:C3)+AVERAGE(D1:H5)";

var rangeList = sheet.GetCellFormulaReferenceRanges("H8");

// rangeList[0].Range == "A1"
// rangeList[1].Range == "B1"
// rangeList[2].Range == "A1:C3"
// rangeList[3].Range == "D1:H5"

参照元と参照先のトレース

トレース矢印で数式の依存関係を視覚化します。

83

参照元のトレース

// Via cell property
var cell = sheet.Cells["C5"];
cell.Formula = "C2+C3";
cell.TraceFormulaPrecedents = true;

// Via worksheet method
sheet.TraceCellPrecedents("C5");
sheet.TraceCellPrecedents(new CellPosition(4, 2));

参照先のトレース

// Via cell property
cell.TraceFormulaDependents = true;

// Via worksheet method
sheet.TraceCellDependents("C2");
sheet.TraceCellDependents(new CellPosition(1, 2));

トレース矢印の削除

// Remove specific traces
sheet.RemoveCellTracePrecedents("C5");
sheet.RemoveCellTraceDependents("C2");

// Remove all trace arrows from a cell
sheet.RemoveCellAllTraceArrows("C5");

// Remove all trace arrows from a range
sheet.RemoveRangeAllTraceArrows("A1:H10");

トレースの状態を確認する

bool hasPrecedents = sheet.IsCellInTracePrecedents(new CellPosition("C5"));
bool hasDependents = sheet.IsCellInTraceDependents(new CellPosition("C2"));

// Get all cells with trace dependents
var tracedCells = sheet.GetAllTraceDependentCells();

トレースを常に表示する

数式が変更されるとトレース矢印は消えます。常に表示させるには以下のようにします。

sheet.CellDataChanged += (s, e) => e.Cell.TraceFormulaPrecedents = true;

セルの数式プロパティ

プロパティ説明
Formulastring数式の式を取得または設定します
HasFormulaboolセルに数式があるかどうか(読み取り専用)
FormulaStatusFormulaStatus数式の計算状態(読み取り専用)
TraceFormulaPrecedentsbool参照元のトレース矢印を表示/非表示にします
TraceFormulaDependentsbool参照先のトレース矢印を表示/非表示にします

自動参照更新

セルの値が変更されると、そのセルを参照しているすべてのセルが自動的に再計算されます。

一時停止と再開

大量のデータ操作の際は、パフォーマンスを向上させるために自動更新を一時停止できます。

// Suspend updates
sheet.SuspendFormulaReferenceUpdates();

// ... bulk data operations ...

// Resume updates
sheet.ResumeFormulaReferenceUpdates();

// Check if suspended
bool isSuspended = sheet.IsSuspendingFormulaReferenceUpdates;

またはワークシートの設定経由で行えます。

sheet.SetSettings(WorksheetSettings.Formula_AutoUpdateReferenceCell, false);
sheet.SetSettings(WorksheetSettings.Formula_AutoUpdateReferenceCell, true);

強制再計算

ワークシート内のすべての数式を再計算します。

// Recalculate entire worksheet
sheet.Recalculate();

// Recalculate a specific range
sheet.Recalculate(new RangePosition("A1:H10"));

// Recalculate a single cell
sheet.RecalcCell("E3");
sheet.RecalcCell(new CellPosition("E3"));
sheet.RecalcCell(2, 4);

// Refresh all formula references
sheet.RefreshFormulaReferences();

数式の設定

設定説明
Formula_AutoUpdateReferenceCellセルの値が変更されたときに数式の参照を自動更新します
Formula_AutoPickingCellAddress数式編集中にセルアドレスの選択を許可します(予約済み)
Formula_AutoFormat数式を自動的に修正およびフォーマットします
sheet.SetSettings(WorksheetSettings.Formula_AutoFormat, true);

FormulaExtension

FormulaExtensionクラスは、数式の動作をカスタマイズするための静的プロパティを提供します。

using unvell.ReoGrid.Formula;
プロパティ説明
CustomFunctionsDictionary<string, Func<Cell, object[], object>>カスタム関数レジストリ
NameReferenceProviderFunc<Cell, string, object>カスタム名前解決プロバイダー
EmptyCellReferenceProviderFunc<Worksheet, CellPosition, Cell, object>カスタム空セル値プロバイダー
ParameterSeparatorstring数式のパラメータ区切り文字(デフォルト: ",")
NumberDecimalSeparatorstring小数点区切り文字(デフォルト: ".")
FormulaCalculationEplisondouble計算のイプシロン(デフォルト: 0.000001

メソッド

メソッド説明
ChangeToStandardFunctionNames()標準の英語関数名を使用します
ChangeToRussianFunctionNames()ロシア語の関数名を使用します

組み込み関数

ReoGridはExcel互換の組み込み関数を提供しています。完全なリストは数式関数を参照してください。

カスタム関数

FormulaExtension.CustomFunctionsでカスタム関数を追加できます。詳しい例はカスタム関数を参照してください。

関連トピック


ページの内容は役に立ちましたか?