إرجاع مجموعة من القيم باستخدام ICustomFunction
ICustomFunction تمت إهماله منذ إصدار Aspose.Cells for Java 20.8. يرجى استخدام فئة AbstractCalculationEngine. يتم وصف استخدام فئة AbstractCalculationEngine في المقال التالي.
توفر Aspose.Cells واجهة ICustomFunction التي تُستخدم لتنفيذ وظائف مخصصة أو مخصصة لا تدعمها Microsoft Excel كوظائف مدمجة.
عند تنفيذ طريقة واجهة ICustomFunction عادةً، تحتاج إلى إرجاع قيمة خلية واحدة. ولكن في بعض الأحيان، قد تحتاج إلى إرجاع مجموعة من القيم. سيشرح هذا المقال كيفية إرجاع مجموعة القيم من ICustomFunction.
الكود التالي ينفذ ICustomFunction ويُرجع مجموعة القيم من خلال طريقة الواجهة.
قم بإنشاء فئة تحتوي على وظيفة CalculateCustomFunction. تنفذ هذه الفئة ICustomFunction.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
public class CustomFunctionStaticValue : ICustomFunction | |
{ | |
public object CalculateCustomFunction(string functionName, ArrayList paramsList, ArrayList contextObjects) | |
{ | |
return new object[][] { | |
new object[]{new DateTime(2015, 6, 12, 10, 6, 30), 2}, | |
new object[]{3.0, "Test"} | |
}; | |
} | |
} |
الآن استخدم الوظيفة أعلاه في برنامجك
// 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); | |
// Create workbook | |
Workbook wb = new Workbook(); | |
Cells cells = wb.Worksheets[0].Cells; | |
// Set formula | |
Cell cell = cells[0, 0]; | |
cell.SetArrayFormula("=MYFUNC()", 2, 2); | |
Style style = cell.GetStyle(); | |
style.Number = 14; | |
cell.SetStyle(style); | |
// Set calculation options for formula | |
CalculationOptions copt = new CalculationOptions(); | |
copt.CustomEngine = new CustomFunctionStaticValue(); | |
wb.CalculateFormula(copt); | |
// Save to xlsx by setting the calc mode to manual | |
wb.Settings.FormulaSettings.CalculationMode = CalcModeType.Manual; | |
wb.Save(dataDir + "output_out.xlsx"); | |
// Save to pdf | |
wb.Save(dataDir + "output_out.pdf"); |