الحساب المباشر للوظيفة المخصصة دون كتابتها في ورقة عمل
الحساب المباشر للوظيفة المخصصة دون كتابتها في ورقة العمل
يرجى الاطلاع على الكود العينة التالي الذي يوضح استخدام هذه الطريقة. لقد استخدمنا دالة مخصصة تدعى MyCompany.CustomFunction() وقمنا بحساب قيمتها كـ “Aspose.Cells.” بأنفسنا، ثم يتم دمج هذه القيمة تلقائيًا مع قيمة الخلية A1 وهي “مرحبًا بكم في " من قبل محرك الحساب وتُرجع القيمة المحسوبة النهائية كـ “مرحبًا بكم في Aspose.Cells.” كما ترون في الكود فإننا لم نكتب دالتنا المخصصة في أي مكان في ورقة العمل وتتم حسابها مباشرة بواسطة منطقنا المخصص الخاص.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
public class CustomEngine extends AbstractCalculationEngine | |
{ | |
// Override the Calculate method with custom logic | |
public void calculate(CalculationData data) | |
{ | |
// Check the forumla name and calculate it yourself | |
if (data.getFunctionName().equals("MyCompany.CustomFunction")) | |
{ | |
// This is our calculated value | |
data.setCalculatedValue("Aspose.Cells."); | |
} | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
// Create a workbook | |
Workbook wb = new Workbook(); | |
// Accesss first worksheet | |
Worksheet ws = wb.getWorksheets().get(0); | |
// Add some text in cell A1 | |
ws.getCells().get("A1").putValue("Welcome to "); | |
// Create a calculation options with custom engine | |
CalculationOptions opts = new CalculationOptions(); | |
opts.setCustomEngine(new CustomEngine()); | |
// 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 | |
System.out.println("Calculated Value: " + ret.toString()); | |
} |
مخرجات الوحدة
فيما يلي إخراج وحدة التحكم للرمز العيني أعلاه.
Calculated Value: Welcome to Aspose.Cells.