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

مقدمة

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

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

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

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

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

عينة من الرموز

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Output directory path
StringPtr outPath = new String("..\\Data\\Output\\");
//Path of output excel file
StringPtr outputAddingFormulasAndCalculatingResults = outPath->StringAppend(new String("outputAddingFormulasAndCalculatingResults.xlsx"));
//Create workbook
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook();
//Access first worksheet in the workbook
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0);
//Adding integer values to cells A1, A2 and A3
ws->GetICells()->GetObjectByIndex(new String("A1"))->PutValue(10);
ws->GetICells()->GetObjectByIndex(new String("A2"))->PutValue(20);
ws->GetICells()->GetObjectByIndex(new String("A3"))->PutValue(70);
//Adding a SUM formula to "A4" cell
ws->GetICells()->GetObjectByIndex(new String("A4"))->SetFormula(new String("=SUM(A1:A3)"));
//Calculating the results of formulas
wb->CalculateFormula();
//Get the calculated value of the cell
intrusive_ptr<String> strVal = ws->GetICells()->GetObjectByIndex(new String("A4"))->GetStringValue();
//Print the calculated value on console
StringPtr str1 = new String("Calculated Result: ");
Console::WriteLine(str1->StringAppend(strVal));
//Saving the workbook
wb->Save(outputAddingFormulasAndCalculatingResults);

الحساب المباشر للصيغة

في بعض الأحيان ، تحتاج إلى حساب نتائج الصيغة مباشرة دون إضافتها إلى ورقة عمل. قيم الخلايا المستخدمة في الصيغة موجودة بالفعل في ورقة عمل وكل ما تحتاجه هو العثور على نتيجة هذه القيم بناءً على بعض صيغة Excel Microsoft دون إضافة الصيغة في ورقة العمل.

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

ينتج الكود أدناه الإخراج التالي.

 Value of A1: 20

Value of A2: 30

Result of Sum(A1:A2): 50

عينة من الرموز

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Create workbook
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook();
//Access first worksheet in the workbook
intrusive_ptr<IWorksheet> ws = wb->GetIWorksheets()->GetObjectByIndex(0);
//Put 20 in cell A1
intrusive_ptr<ICell> cellA1 = ws->GetICells()->GetObjectByIndex(new String("A1"));
cellA1->PutValue(20);
//Put 30 in cell A2
intrusive_ptr<ICell> cellA2 = ws->GetICells()->GetObjectByIndex(new String("A2"));
cellA2->PutValue(30);
//Calculate the Sum of A1 and A2
intrusive_ptr<Aspose::Cells::System::Object> results = ws->CalculateFormula(new String("=Sum(A1:A2)"));
//Print the output
StringPtr str1 = new String("Value of A1: ");
Console::WriteLine(str1->StringAppend(cellA1->GetStringValue()));
StringPtr str2 = new String("Value of A2: ");
Console::WriteLine(str2->StringAppend(cellA2->GetStringValue()));
StringPtr str3 = new String("Result of Sum(A1:A2): ");
Console::WriteLine(str3->StringAppend(results->ToString()));

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

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

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

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

يرجى الاتصال بـ Workbook.GetISettings (). SetCreateCalcChain () باستخدام معلمة خاطئة. يمكنك استعمال القدم ملف اكسل لاختبار هذا الرمز.

عينة من الرموز

//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
StringPtr dirPath = new String("..\\Data\\Formulas\\");
//Path of input excel file
StringPtr sampleCalculatingFormulasOnceOnly = dirPath->StringAppend(new String("sampleCalculatingFormulasOnceOnly.xlsx"));
//Create workbook
intrusive_ptr<IWorkbook> wb = Factory::CreateIWorkbook(sampleCalculatingFormulasOnceOnly);
//Set the CreateCalcChain as false
wb->GetISettings()->SetCreateCalcChain(false);
//Get the time in milliseconds before formula calculation
int before_miliseconds = Aspose::Cells::System::DateTime::GetNow()->GetMillisecond();
//Calculate the workbook formulas
wb->CalculateFormula();
//Get the time in milliseconds after formula calculation
int after_miliseconds = Aspose::Cells::System::DateTime::GetNow()->GetMillisecond();
//Print the difference in milliseconds
StringPtr str1 = new String("Workbook Formula Calculation Elapsed Time in Milliseconds: ");
Console::WriteLine(str1->StringAppend(Int32Helper::ToString(after_miliseconds - before_miliseconds)));