حساب الصيغ

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

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

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

استخدم المعادلة الملكية أو[SetFormula (…]](https://reference.aspose.com/cells/net/aspose.cells.cell/setformula/methods/2) طرقCellفئة لإضافة صيغة إلى خلية. عند تطبيق صيغة ، ابدأ السلسلة دائمًا بعلامة يساوي (=) كما تفعل عند إنشاء صيغة في Microsoft Excel واستخدم فاصلة (،) لتحديد معاملات الدالة.

لحساب نتائج الصيغ ، يمكن للمستخدم استدعاءاحسب الصيغة طريقةدفتر العملفئة تعالج جميع الصيغ المضمنة في ملف Excel. أو ، يمكن للمستخدم استدعاءاحسب الصيغة طريقةورقة فئة تعالج جميع الصيغ المضمنة في ورقة. أو ، يمكن للمستخدم أيضًا الاتصال بـاحسب طريقةCellفئة تعالج صيغة واحدة Cell:

// 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 directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Adding a new worksheet to the Excel object
int sheetIndex = workbook.Worksheets.Add();
// Obtaining the reference of the newly added worksheet by passing its sheet index
Worksheet worksheet = workbook.Worksheets[sheetIndex];
// Adding a value to "A1" cell
worksheet.Cells["A1"].PutValue(1);
// Adding a value to "A2" cell
worksheet.Cells["A2"].PutValue(2);
// Adding a value to "A3" cell
worksheet.Cells["A3"].PutValue(3);
// Adding a SUM formula to "A4" cell
worksheet.Cells["A4"].Formula = "=SUM(A1:A3)";
// Calculating the results of formulas
workbook.CalculateFormula();
// Get the calculated value of the cell
string value = worksheet.Cells["A4"].Value.ToString();
// Saving the Excel file
workbook.Save(dataDir + "output.xls");

من المهم أن تعرف

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

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

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

يمكنك استخدام Aspose.Cells ‘واجهات برمجة تطبيقات محرك حساب الصيغة لـورقة عمل إلىاحسب نتائج هذه الصيغ دون إضافتها إلى ورقة العمل:

// 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 directory if it is not already present.
bool IsExists = System.IO.Directory.Exists(dataDir);
if (!IsExists)
System.IO.Directory.CreateDirectory(dataDir);
// Create a workbook
Workbook workbook = new Workbook();
// Access first worksheet
Worksheet worksheet = workbook.Worksheets[0];
// Put 20 in cell A1
Cell cellA1 = worksheet.Cells["A1"];
cellA1.PutValue(20);
// Put 30 in cell A2
Cell cellA2 = worksheet.Cells["A2"];
cellA2.PutValue(30);
// Calculate the Sum of A1 and A2
var results = worksheet.CalculateFormula("=Sum(A1:A2)");
// Print the output
System.Console.WriteLine("Value of A1: " + cellA1.StringValue);
System.Console.WriteLine("Value of A2: " + cellA2.StringValue);
System.Console.WriteLine("Result of Sum(A1:A2): " + results.ToString());

ينتج الكود أعلاه المخرجات التالية:

Value of A1: 20
Value of A2: 30
Result of Sum(A1:A2): 50.0

تكرار حساب الصيغ

عندما يكون هناك الكثير من الصيغ في المصنف ويحتاج المستخدم إلى حسابها بشكل متكرر مع تعديل جزء صغير منها فقط ، فقد يكون من المفيد للأداء تمكين سلسلة حساب الصيغة:FormulaSettings.EnableCalculationChain.

// 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);
// Load the template workbook
Workbook workbook = new Workbook(dataDir + "book1.xls");
// Print the time before formula calculation
Console.WriteLine(DateTime.Now);
// Set the CreateCalcChain as tue
workbook.Settings.FormulaSettings.EnableCalculationChain = true;
// Calculate the workbook formulas
workbook.CalculateFormula();
// Print the time after formula calculation
Console.WriteLine(DateTime.Now);
//change the value of one cell
workbook.Worksheets[0].Cells["A1"].PutValue("newvalue");
//re-calculate those formulas which depend on cell A1
workbook.CalculateFormula();

من المهم أن تعرف

موضوعات مسبقة