تطبيق التظليل على الصفوف والأعمدة البديلة بالتنسيق الشرطي

Contents
[ ]

تستخدم هذه المقالة الوظائف المضمنة في Excel مثل ROW و COLUMN & MOD. فيما يلي بعض التفاصيل حول هذه الوظائف من أجل فهم أفضل لمقتطف الشفرة المقدم مسبقًا.

  • صف() تقوم الدالة بإرجاع رقم الصف لمرجع الخلية. إذا تم حذف المعلمة المرجعية ، فإنها تفترض أن المرجع هو عنوان الخلية التي تم إدخال الدالة ROW فيها.
  • عمود() تقوم الدالة بإرجاع رقم العمود لمرجع الخلية. إذا تم حذف المعلمة المرجعية ، فإنها تفترض أن المرجع هو عنوان الخلية التي تم إدخال الدالة COLUMN فيها.
  • عصري() تُرجع الدالة الباقي بعد قسمة الرقم على القاسم ، حيث تكون المعلمة الأولى للدالة هي القيمة الرقمية التي ترغب في العثور على الباقي والمعلمة الثانية هي الرقم المستخدم للقسمة على معامل الرقم. إذا كان المقسوم عليه 0 ، فسيتم إرجاع # DIV / 0! خطأ.

لنبدأ في كتابة بعض التعليمات البرمجية لتحقيق هذا الهدف بمساعدة Aspose.Cells for .NET API.

// 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 an instance of Workbook or load existing
var book = new Workbook();
// Access the Worksheet on which desired rule has to be applied
var sheet = book.Worksheets[0];
// Add FormatConditions to the instance of Worksheet
int idx = sheet.ConditionalFormattings.Add();
// Access the newly added FormatConditions via its index
var conditionCollection = sheet.ConditionalFormattings[idx];
// Define a CellsArea on which conditional formatting will be applicable
// The code creates a CellArea ranging from A1 to I20
var area = CellArea.CreateCellArea("A1", "I20");
//Add area to the instance of FormatConditions
conditionCollection.AddArea(area);
// Add a condition to the instance of FormatConditions
// For this case, the condition type is expression, which is based on some formula
idx = conditionCollection.AddCondition(FormatConditionType.Expression);
// Access the newly added FormatCondition via its index
FormatCondition formatCondirion = conditionCollection[idx];
// Set the formula for the FormatCondition
// Formula uses the Excel's built-in functions as discussed earlier in this article
formatCondirion.Formula1 = @"=MOD(ROW(),2)=0";
// Set the background color and patter for the FormatCondition's Style
formatCondirion.Style.BackgroundColor = Color.Blue;
formatCondirion.Style.Pattern = BackgroundType.Solid;
// Save the result on disk
book.Save(dataDir + "output_out.xlsx");

تُظهر اللقطة التالية جدول البيانات الناتج الذي تم تحميله في تطبيق Excel.

ما يجب القيام به: image_بديل_نص

لتطبيق التظليل على أعمدة بديلة ، كل ما عليك فعله هو تغيير الصيغة**= MOD (ROW ()، 2) = 0** مثل**= MOD (عمود () ، 2) = 0** ، هذا هو؛ بدلاً من الحصول على فهرس الصف ، قم بتعديل الصيغة لاسترداد فهرس العمود. سيبدو جدول البيانات الناتج ، في هذه الحالة ، على النحو التالي.

ما يجب القيام به: image_بديل_نص