حساب الصيغ
إضافة صيغ وحساب النتائج
تحتوي Aspose.Cells على محرك حساب مدمج للصيغ. ليس فقط يمكنه إعادة حساب الصيغ المستوردة من قوالب المصمم بل يدعم أيضًا حساب نتائج الصيغ المضافة في وقت التشغيل.
يدعم Aspose.Cells معظم الصيغ أو الوظائف التي تُعتبر جزءًا من Microsoft Excel (اقرأ قائمة الوظائف المدعومة من قبل محرك الحساب). يمكن استخدام هذه الوظائف من خلال واجهات برمجة التطبيقات أو جداول البيانات التي يصممها المستخدم. تدعم Aspose.Cells مجموعة كبيرة من الصيغ الرياضية والنصية والمنطقية والتاريخية/الزمنية والإحصائية وقواعد البيانات والبحث والمرجعية.
استخدم خصائص Formula أو طرق SetFormula(…) لفئة Cell لإضافة صيغة لخلية. عند تطبيق الصيغة، ابدأ دائمًا السلسلة برمز يساوي (=) كما تفعل عند إنشاء صيغة في Microsoft Excel واستخدم فاصلة (،) لفصل معلمات الدالة.
لحساب نتائج الصيغ، يمكن للمستخدم استدعاء الطريقة CalculateFormula لفئة Workbook التي تقوم بمعالجة جميع الصيغ المضمنة في ملف Excel. أو، يمكن للمستخدم استدعاء الطريقة CalculateFormula لفئة Worsheet التي تقوم بمعالجة جميع الصيغ المضمنة في ورقة. أو، يمكن أيضًا للمستخدم استدعاء الطريقة Calculate لفئة 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 حساب نتائج الصيغ مباشرة.
في بعض الأحيان، قد تحتاج إلى حساب نتائج الصيغ مباشرة دون إضافتها إلى ورقة عمل. قد تكون قيم الخلايا المستخدمة في الصيغة موجودة بالفعل في ورقة عمل وكل ما تحتاجه هو إيجاد نتيجة تلك القيم بناءً على بعض صيغ Microsoft Excel دون إضافة الصيغة في ورقة عمل.
يمكنك استخدام واجهات برمجة التطبيقات لمحرك حساب الصيغ في Aspose.Cells لـ Worksheet لـ calculate نتائج مثل هذه الصيغ دون إضافتها إلى ورقة العمل:
// 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(); |
مهم معرفته
مواضيع متقدمة
- إضافة الخلايا إلى نافذة مراقبة صيغ Microsoft Excel
- حساب وظيفة IFNA باستخدام Aspose.Cells
- حساب الصيغة الصفية للجداول البيانات
- حساب وظائف MINIFS و MAXIFS في Excel 2016
- تقليل وقت الحساب لطريقة Cell.Calculate
- اكتشاف المراجعة الدائرية
- الحساب المباشر للوظيفة المخصصة دون كتابتها في ورقة العمل
- تنفيذ محرك الحساب المخصص لتوسيع محرك الحساب الافتراضي لـ Aspose.Cells
- إيقاف أو إلغاء حساب الصيغ في سجل العمل
- إرجاع مجموعة من القيم باستخدام AbstractCalculationEngine
- إرجاع مجموعة من القيم باستخدام ICustomFunction
- ضبط وضع حساب الصيغة في سجل العمل
- استخدام وظيفة FormulaText في Aspose.Cells
- استخدام ميزة ICustomFunction