ICustomFunction機能の使用

ユーザー定義関数の作成と評価

この記事では、ICustomFunctionインタフェースの実装をデモし、スプレッドシートでカスタム関数を作成し、その結果を取得する方法を説明します。名前が MyFunc のカスタム関数を定義し、次の詳細を持つ2つのパラメーターを受け入れるカスタム関数を定義します。

  • 1番目のパラメーターは単一のセルを参照します
  • 2番目のパラメーターはセル範囲を参照します

カスタム関数は、指定された2番目のパラメーターからすべての値を追加し、1番目のパラメーターの値で結果を除算します。

次は、CalculateCustomFunction メソッドの実装方法です。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
public class CustomFunction : ICustomFunction
{
public object CalculateCustomFunction(string functionName, System.Collections.ArrayList paramsList, System.Collections.ArrayList contextObjects)
{
decimal total = 0M;
try
{
// Get value of first parameter
decimal firstParamB1 = System.Convert.ToDecimal(paramsList[0]);
// Get value of second parameter
Array secondParamC1C5 = (Array)(paramsList[1]);
// get every item value of second parameter
foreach (object[] value in secondParamC1C5)
{
total += System.Convert.ToDecimal(value[0]);
}
total = total / firstParamB1;
}
catch
{
}
// Return result of the function
return total;
}
}

新しく定義された関数をスプレッドシートで使用する方法は次のとおりです。

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
// Open the workbook
Workbook workbook = new Workbook();
// Obtaining the reference of the first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Adding sample values to cells
worksheet.Cells["B1"].PutValue(5);
worksheet.Cells["C1"].PutValue(100);
worksheet.Cells["C2"].PutValue(150);
worksheet.Cells["C3"].PutValue(60);
worksheet.Cells["C4"].PutValue(32);
worksheet.Cells["C5"].PutValue(62);
// Adding custom formula to Cell A1
workbook.Worksheets[0].Cells["A1"].Formula = "=MyFunc(B1,C1:C5)";
// Calcualting Formulas
workbook.CalculateFormula(false, new CustomFunction());
// Assign resultant value to Cell A1
workbook.Worksheets[0].Cells["A1"].PutValue(workbook.Worksheets[0].Cells["A1"].Value);
// Save the file
workbook.Save(dataDir + "UsingICustomFunction_out.xls");

概要

Aspose.Cells API は、対応するパラメータが参照またはその計算結果が参照の場合、「paramsList」に ReferredArea オブジェクトを置きます。参照自体が必要な場合は ReferredArea を直接使用できます。式の位置に対応する参照から単一のセルの値を取得する必要がある場合は、ReferredArea.GetValue(rowOffset, int colOffset) メソッドを使用できます。全体の範囲のセル値の配列が必要な場合は、ReferredArea.GetValues メソッドを使用できます。

Aspose.CellsのAPIが"paramsList"にReferredAreaを与えるため、「contextObjects」でのReferredAreaCollectionはもはや必要ありません(以前のバージョンでは、カスタム関数のパラメーターに常に1対1マップを提供することができなかったため、これが"contextObjects"から削除されました)。

 public object CalculateCustomFunction(string functionName, ArrayList paramsList, ArrayList contextObjects)

{

    ...

    object o = paramsList[i];

    if(o is ReferredArea) //fetch data from reference

    {

        ReferredArea ra = (ReferredArea)o;

        if(ra.IsArea)

        {

            o = ra.GetValues();

        }

        else

        {

            o = ra.GetValue(0, 0);

        }

    }

    if (o is Array)

    {

        ...

    }

    else if...

    ...

}