ICustomFunction Özelliği Kullanımı

Kullanıcı Tanımlı Bir İşlev Oluşturma ve Değerlendirme

Bu makale, ICustomFunction arabirimini uygulayarak özel bir işlevi yazmayı ve bu işlevi elektronik tabloda kullanmayı ve sonuçları almayı göstermektedir. MyFunc adında 2 parametre kabul eden özel bir işlevi tanımlayacağız.

    1. parametre, bir hücreye atıfta bulunur
    1. parametre, hücrelerin bir aralığına atıfta bulunur

Özel işlev, 2. parametre olarak belirtilen hücre aralığındaki tüm değerleri ekler ve 1. parametredeki değerle bölerek sonucu verir.

İşte CalculateCustomFunction yöntemini nasıl uyguladığımız.

// 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;
}
}

Yeni tanımlanan işlevi bir elektronik tabloda nasıl kullandığımız.

// 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");

Genel Bakış

Aspose.Cells API’leri, ilgili parametre bir başvuru olduğunda veya hesaplanan sonucu bir başvuru olduğunda, ReferredArea nesnesini ‘paramsList’ içine yerleştirir. Başvuruyu kendiniz kullanmak istiyorsanız, ReferredArea’yı doğrudan kullanabilirsiniz. Formülün konumuna karşılık gelen başvurudan tek bir hücre değerini almanız gerekiyorsa, ReferredArea.GetValue(rowOffset, int colOffset) yöntemini kullanabilirsiniz. Alanın tamamı için hücre değerleri dizisi almanız gerekiyorsa, ReferredArea.GetValues yöntemini kullanabilirsiniz.

Aspose.Cells API’leri tarafından ‘paramsList’ içine ReferredArea verildiğinden, ‘contextObjects’ içindeki ReferredAreaCollection artık gereksiz olmayacak (eski sürümlerde özel işlev parametrelerine her zaman birbirine tekabül etmeyen bir eşleme yapamazdı) ve bu nedenle ‘contextObjects’ içinden kaldırılmıştır.

 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...

    ...

}