Eğitim tablosuna yazmadan özel bir fonksiyonun doğrudan hesaplanması
Özel işlevin çalışma tablosuna yazılmadan doğrudan hesaplanması
Bu konu, bir elektronik tabloda öncelikle onları yazmadan özel işlevlerinizi doğrudan nasıl hesaplayabileceğinizi açıklar. Bu amacı için Worksheet.CalculateFormula(string formula, CalculationOptions opts) yöntemini kullanın.
Bu metodu kullanımını gösteren aşağıdaki örnek kodu inceleyin. MyCompany.CustomFunction() adında özel bir işlev kullandık ve bu işlevin değerini “Aspose.Cells.” olarak kendimiz hesapladık ve bu değer otomatik olarak hesaplama motoru tarafından “Hoş geldiniz " olan A1 hücresinin değeri ile birleştirildi ve son hesaplanmış değer olarak “Hoş geldiniz Aspose.Cells.” olarak döndü. Kodun içinde özel bir işlevimizi çalıştırmadığımızı ve bunun yerine, özel mantığımızla doğrudan hesaplandığını görebilirsiniz.
Programlama Örneği
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
class ICustomEngine : AbstractCalculationEngine | |
{ | |
// Override the Calculate method with custom logic | |
public override void Calculate(CalculationData data) | |
{ | |
// Check the forumla name and calculate it yourself | |
if (data.FunctionName == "MyCompany.CustomFunction") | |
{ | |
// This is our calculated value | |
data.CalculatedValue = "Aspose.Cells."; | |
} | |
} | |
} | |
class ImplementDirectCalculationOfCustomFunction | |
{ | |
public static void Run() | |
{ | |
// Create a workbook | |
Workbook wb = new Workbook(); | |
// Accesss first worksheet | |
Worksheet ws = wb.Worksheets[0]; | |
// Add some text in cell A1 | |
ws.Cells["A1"].PutValue("Welcome to "); | |
// Create a calculation options with custom engine | |
CalculationOptions opts = new CalculationOptions(); | |
opts.CustomEngine = new ICustomEngine(); | |
// This line shows how you can call your own custom function without | |
// a need to write it in any worksheet cell | |
// After the execution of this line, it will return | |
// Welcome to Aspose.Cells. | |
object ret = ws.CalculateFormula("=A1 & MyCompany.CustomFunction()", opts); | |
// Print the calculated value on Console | |
Console.WriteLine("Calculated Value: " + ret); | |
} | |
} |
Konsol Çıktısı
Yukarıdaki örnek kodun konsol çıktısı aşağıdaki gibidir.
Calculated Value: Welcome to Aspose.Cells.