ضبط التنسيقات الشرطية لملفات Excel و ODS

مقدمة

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

Aspose.Cells تدعم تطبيق التنسيق الشرطي على الخلايا أثناء التشغيل. يوضح هذا المقال كيفية القيام بذلك. كما يوضح كيفية حساب اللون الذي يستخدمه Excel لتنسيق اللون الشرطي.

تطبيق التنسيق الشرطي

Aspose.Cells تدعم التنسيق الشرطي بعدة طرق:

  • باستخدام جدول البيانات للمصمم
  • باستخدام طريقة النسخ.
  • إنشاء التنسيق الشرطي أثناء التشغيل.

استخدام جدول البيانات للمصمم

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

استخدام طريقة النسخ

تسمح Aspose.Cells للمطورين بنسخ إعدادات التنسيق الشرطي من خلية إلى أخرى في ورقة العمل عن طريق استدعاء الطريقة Range.copy().

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open a template Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "book1.xlsx"));
// Get the first worksheet in the workbook
const sheet = workbook.getWorksheets().get(0);
// Get the A1 cell
const cell = sheet.getCells().get("A1");
// Get the conditional formatting result object
const cfr = cell.getConditionalFormattingResult();
// Get the icon set
const icon = cfr.getConditionalFormattingIcon();
// Create the image file based on the icon's image data
require("fs").writeFileSync(path.join(dataDir, "imgIcon.out.jpg"), icon.getImageData());

تطبيق التنسيق الشرطي أثناء التشغيل

Aspose.Cells تتيح لك إضافة وإزالة التنسيق الشرطي أثناء التشغيل. الأمثلة البرمجية أدناه تُظهر كيفية تحديد التنسيق الشرطي:

  1. قم بإنشاء ورقة العمل.
  2. أضف تنسيق شرطي فارغ.
  3. حدد النطاق الذي ينبغي تطبيق التنسيق عليه.
  4. حدد شروط التنسيق.
  5. حفظ الملف.

بعد هذا المثال يأتي عدد من الأمثلة الصغيرة الأخرى التي توضح كيفية تطبيق إعدادات الخط، إعدادات الحدود، وأنماط.

أضاف Microsoft Excel 2007 تنسيقًا شرطيًا أكثر تقدمًا تدعمه Aspose.Cells أيضًا. توضح الأمثلة هنا كيفية استخدام التنسيق البسيط، في حين توضح أمثلة Microsoft Excel 2007 كيفية تطبيق تنسيق شرطي أكثر تقدمًا.

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "Book1.xlsx");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().getCount();
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
let ca = AsposeCells.CellArea.createCellArea(0, 0, 0, 0);
fcs.addArea(ca);
ca = AsposeCells.CellArea.createCellArea(1, 1, 1, 1);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "=A2", "100");
// Adds condition.
const conditionIndex2 = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "50", "100");
// Sets the background color.
const fc = fcs.get(conditionIndex);
fc.getStyle().setBackgroundColor(AsposeCells.Color.Red);
// Saving the Excel file
workbook.save(path.join(dataDir, "output.xls"));

تعيين الخط

const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open a template Excel file
const workbook = new AsposeCells.Workbook(path.join(dataDir, "book1.xlsx"));
// Get the first worksheet in the workbook
const sheet = workbook.getWorksheets().get(0);
// Get the A1 cell
const cell = sheet.getCells().get("A1");
// Get the conditional formatting result object
const cfr = cell.getConditionalFormattingResult();
// Get the icon set
const icon = cfr.getConditionalFormattingIcon();
// Create the image file based on the icon's image data
require("fs").writeFileSync(path.join(dataDir, "imgIcon.out.jpg"), icon.getImageData());

تعيين الحدود

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().add();
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(0, 0, 5, 3);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "50", "100");
// Sets the background color.
const fc = fcs.get(conditionIndex);
fc.getStyle().getBorders().get(AsposeCells.BorderType.LeftBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.RightBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.TopBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.BottomBorder).setLineStyle(AsposeCells.CellBorderType.Dashed);
fc.getStyle().getBorders().get(AsposeCells.BorderType.LeftBorder).setColor(new AsposeCells.Color(0, 255, 255));
fc.getStyle().getBorders().get(AsposeCells.BorderType.RightBorder).setColor(new AsposeCells.Color(0, 255, 255));
fc.getStyle().getBorders().get(AsposeCells.BorderType.TopBorder).setColor(new AsposeCells.Color(0, 255, 255));
fc.getStyle().getBorders().get(AsposeCells.BorderType.BottomBorder).setColor(new AsposeCells.Color(255, 255, 0));
workbook.save(path.join(dataDir, "output.xlsx"));

تعيين النمط

const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Instantiating a Workbook object
const workbook = new AsposeCells.Workbook();
const sheet = workbook.getWorksheets().get(0);
// Adds an empty conditional formatting
const index = sheet.getConditionalFormattings().add();
const fcs = sheet.getConditionalFormattings().get(index);
// Sets the conditional format range.
const ca = AsposeCells.CellArea.createCellArea(0, 0, 5, 3);
fcs.addArea(ca);
// Adds condition.
const conditionIndex = fcs.addCondition(AsposeCells.FormatConditionType.CellValue, AsposeCells.OperatorType.Between, "50", "100");
const fc = fcs.get(conditionIndex);
fc.getStyle().setPattern(AsposeCells.BackgroundType.ReverseDiagonalStripe);
fc.getStyle().setForegroundColor(new AsposeCells.Color(255, 255, 0));
fc.getStyle().setBackgroundColor(new AsposeCells.Color(0, 255, 255));
workbook.save(path.join(dataDir, "output.xlsx"));

مواضيع متقدمة