ICustomFunction Özelliğini Kullanma

Kullanıcı Tanımlı Fonksiyon Oluşturma ve Değerlendirme

Bu makalede, özel bir işlev yazmak ve sonuçları almak için bunu elektronik tabloda kullanmak için ICustomFunction arabiriminin uygulanması gösterilmektedir. Özel bir işlevi ada göre tanımlayacağızİşlevim aşağıdaki ayrıntılara sahip 2 parametreyi kabul edecektir.

    1. parametre tek bir hücreyi ifade eder
    1. parametre bir hücre aralığını ifade eder

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

HesaplamaCustomFunction yöntemini şu şekilde uyguladık.

Java

 public class CustomFunction implements ICustomFunction

{

    @Override

    public Object calculateCustomFunction(String functionName, java.util.ArrayList paramsList, java.util.ArrayList contextObjects)

    {

        double result = 0f;

        double sum = 0;

        //Get the value of 1st parameter

        Object firstParamB1 = paramsList.get(0);

        if (firstParamB1 instanceof ReferredArea)

        {

            ReferredArea ra = (ReferredArea)firstParamB1;

            firstParamB1 = ra.getValue(0, 0);

        }

        //Get values of 2nd parameter

        Object secondParamC1C5 = paramsList.get(1);

        if (secondParamC1C5 instanceof ReferredArea)

        {

            ReferredArea ra = (ReferredArea)secondParamC1C5;

            for (int i = ra.getStartRow(); i <= ra.getEndRow(); i++)

            {

                //Add all values

                sum += (double)ra.getValue(i, 0);

            }

        }

        result = sum / (double)firstParamB1;

        // Return result of the function

        return result;

    }

}

Yeni tanımlanan işlevin bir e-tabloda nasıl kullanılacağı aşağıda açıklanmıştır

Java

 //Open the workbook

Workbook workbook = new Workbook();

//Obtaining the reference of the first worksheet

Worksheet worksheet = workbook.getWorksheets().get(0);

//Adding a sample value to "A1" cell

worksheet.getCells().get("B1").putValue(5);

//Adding a sample value to "A2" cell

worksheet.getCells().get("C1").putValue(100);

//Adding a sample value to "A3" cell

worksheet.getCells().get("C2").putValue(150);

//Adding a sample value to "B1" cell

worksheet.getCells().get("C3").putValue(60);

//Adding a sample value to "B2" cell

worksheet.getCells().get("C4").putValue(32);

//Adding a sample value to "B2" cell

worksheet.getCells().get("C5").putValue(62);

//Adding custom formula to Cell A1

worksheet.getCells().get("A1").setFormula("=MyFunc(B1,C1:C5)");

//Calcualating Formulas

workbook.calculateFormula(false, new CustomFunction());

//Assign resultant value to Cell A1

worksheet.getCells().get("A1").putValue(worksheet.getCells().get("A1").getValue());

//Save the file

workbook.save(dir + "UsingICustomFunction.xls");

Genel Bakış

Aspose.Cells API’leri, karşılık gelen parametre bir referans olduğunda veya hesaplanan sonucu referans olduğunda, ReferredArea nesnesini “paramsList"e yerleştirir. Referansın kendisine ihtiyacınız varsa, doğrudan ReferredArea’yı kullanabilirsiniz. Formülün konumuna karşılık gelen referanstan tek bir hücrenin değerini almanız gerekiyorsa ReferredArea.getValue(rowOffset, int colOffset) yöntemini kullanabilirsiniz. Alanın tamamı için hücre değerleri dizisine ihtiyacınız varsa ReferredArea.getValues yöntemini kullanabilirsiniz.

Aspose.Cells API’leri “paramsList"te ReferredArea değerini verdiğinden, “contextObjects” içindeki ReferredAreaCollection’a artık ihtiyaç duyulmayacak (eski sürümlerde özel işlevin parametrelerine her zaman bire bir harita veremiyordu), bu nedenle “contextObjects"ten kaldırıldı.

 public Object calculateCustomFunction(String functionName, ArrayList paramsList, ArrayList contextObjects)

{

    ...

    Object o = paramsList.get(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...

    ...

}