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

Contents
[ ]

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

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

لنبدأ بكتابة بعض الأكواد لتحقيق هذا الهدف بمساعدة واجهة برمجة تطبيقات Aspose.Cells for Node.js via C++.

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create an instance of Workbook or load existing
const book = new AsposeCells.Workbook();
// Access the Worksheet on which desired rule has to be applied
const sheet = book.getWorksheets().get(0);
// Add FormatConditions to the instance of Worksheet
let idx = sheet.getConditionalFormattings().add();
// Access the newly added FormatConditions via its index
const conditionCollection = sheet.getConditionalFormattings().get(idx);
// Define a CellsArea on which conditional formatting will be applicable
// The code creates a CellArea ranging from A1 to I20
const area = AsposeCells.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(AsposeCells.FormatConditionType.Expression);
// Access the newly added FormatCondition via its index
const formatCondition = conditionCollection.get(idx);
// Set the formula for the FormatCondition
formatCondition.setFormula1("=MOD(ROW(),2)=0");
// Set the background color and pattern for the FormatCondition's Style
formatCondition.getStyle().setBackgroundColor(AsposeCells.Color.Blue);
formatCondition.getStyle().setPattern(AsposeCells.BackgroundType.Solid);
// Save the result on disk
book.save(path.join(dataDir, "output_out.xlsx"));

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

todo:image_alt_text

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

todo:image_alt_text