طرق لحساب الصيغ

مقدمة

يحتوي Aspose.Cells على محرك حسابي مدمج للصيغ. يمكنه ليس فقط إعادة حساب الصيغ المستوردة من قوالب المصمم ولكن أيضًا دعم حساب النتائج للصيغ المضافة أثناء التشغيل.

إضافة صيغ وحساب النتائج

تدعم Aspose.Cells معظم الصيغ أو الدوال التي تشكل جزءًا من Microsoft Excel. يمكن استخدامها من خلال واجهة برمجة التطبيقات أو باستخدام جداول البيانات المصممة. تدعم Aspose.Cells مجموعة كبيرة من الصيغ الرياضية والنصية والمنطقية والتاريخ/الوقت والإحصائية والبحث والمرجعية.

استخدم طريقة Cell.SetFormula لإضافة صيغة إلى خلية. عند تطبيق صيغة على خلية ، يجب دائمًا أن تبدأ السلسلة برمز يساوي (=) كما تفعل عند إنشاء صيغة في Microsoft Excel. استخدم الفاصلة (,) لفصل معلمات الدالة.

لحساب نتائج الصيغ ، اتصل بطريقة Workbook.CalculateFormula() التي تقوم بمعالجة جميع الصيغ المضمنة في ملف Excel. يرجى الرجوع إلى الكود المثالي التالي الذي يضيف الصيغة ويحسب نتائجها. يرجى التحقق من ملف الإكسل الناتج الذي تم إنشاؤه باستخدام هذا الكود.

كود عينة

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Output directory path
U16String outPath(u"..\\Data\\Output\\");
//Path of output excel file
U16String outputAddingFormulasAndCalculatingResults = outPath + u"outputAddingFormulasAndCalculatingResults.xlsx";
//Create workbook
Workbook wb;
//Access first worksheet in the workbook
Worksheet ws = wb.GetWorksheets().Get(0);
//Adding integer values to cells A1, A2 and A3
ws.GetCells().Get(u"A1").PutValue(10);
ws.GetCells().Get(u"A2").PutValue(20);
ws.GetCells().Get(u"A3").PutValue(70);
//Adding a SUM formula to "A4" cell
ws.GetCells().Get(u"A4").SetFormula(u"=SUM(A1:A3)");
//Calculating the results of formulas
wb.CalculateFormula();
//Get the calculated value of the cell
U16String strVal = ws.GetCells().Get(u"A4").GetStringValue();
//Print the calculated value on console
std::cout << "Calculated Result: " << strVal.ToUtf8() <<std::endl;
//Saving the workbook
wb.Save(outputAddingFormulasAndCalculatingResults);
Aspose::Cells::Cleanup();

حساب الصيغ مرة واحدة فقط

عند استدعاء Workbook.CalculateFormula() لحساب قيم الصيغ في قالب عمل ملف Excel ، تقوم Aspose.Cells بإنشاء سلسلة حسابية. يزيد الأداء عندما تتم حساب الصيغ للمرة الثانية أو الثالثة.

ومع ذلك ، إذا كان القالب يحتوي على الكثير من الصيغ ، فإن الوقت الذي يتم فيه حساب الصيغة لأول مرة يمكن أن يستهلك الكثير من وحدة المعالجة المركزية والذاكرة.

تتيح Aspose.Cells لك إيقاف إنشاء سلسلة الحسابات وهو ما يكون مفيدًا عندما ترغب في حساب الصيغ مرة واحدة فقط.

يرجى استدعاء Workbook.GetISettings().SetCreateCalcChain() مع المعلمة false. يمكنك استخدام ملف الإكسل المقدم لاختبار هذا الكود.

كود عينة

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
U16String dirPath(u"..\\Data\\Formulas\\");
//Path of input excel file
U16String sampleCalculatingFormulasOnceOnly = dirPath + u"sampleCalculatingFormulasOnceOnly.xlsx";
//Create workbook
Workbook wb(sampleCalculatingFormulasOnceOnly);
//Set the CreateCalcChain as false
wb.GetSettings().GetFormulaSettings().SetEnableCalculationChain(false);
//Get the time before formula calculationint
auto startTime = std::chrono::system_clock::now();
//Calculate the workbook formulas
wb.CalculateFormula();
//Get the time after formula calculation
auto interval = std::chrono::system_clock::now() - startTime;
long long time = std::chrono::duration_cast<std::chrono::milliseconds>(interval).count();
std::cout << "Workbook Formula Calculation Elapsed Time in Milliseconds: " << time << std::endl;
Aspose::Cells::Cleanup();