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

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

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

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

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

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
String dataDir = Utils.getDataDir(ApplyShadingToAlternateRowsAndColumns.class);
/*
* Create an instance of Workbook Optionally load an existing spreadsheet by passing its stream or path to Workbook
* constructor
*/
Workbook book = new Workbook();
// Access the Worksheet on which desired rule has to be applied
Worksheet sheet = book.getWorksheets().get(0);
// Add FormatConditions to the instance of Worksheet
int index = sheet.getConditionalFormattings().add();
// Access the newly added FormatConditions via its index
FormatConditionCollection conditionCollection = sheet.getConditionalFormattings().get(index);
// Define a CellsArea on which conditional formatting will be applicable
CellArea 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
index = conditionCollection.addCondition(FormatConditionType.EXPRESSION);
// Access the newly added FormatCondition via its index
FormatCondition formatCondirion = conditionCollection.get(index);
// Set the formula for the FormatCondition. Formula uses the Excel's built-in functions as discussed earlier in this
// article
formatCondirion.setFormula1("=MOD(ROW(),2)=0");
// Set the background color and patter for the FormatCondition's Style
formatCondirion.getStyle().setBackgroundColor(Color.getBlue());
formatCondirion.getStyle().setPattern(BackgroundType.SOLID);
// Save the result on disk
book.save(dataDir + "output.xlsx");

يوضح المثال التالي لقطة للجدول النهائي المحمّل في تطبيق Excel.

todo:image_alt_text

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

todo:image_alt_text