تطبيق التنسيق الشرطي في الأوراق العمل

استخدام Aspose.Cells لتطبيق تنسيق مشروط بناءً على قيمة الخلية

  1. قم بتنزيل وتثبيت Aspose.Cells.
    1. قم بتنزيل Aspose.Cells للبايثون via .NET.
  2. قم بتثبيته على كمبيوتر التطوير الخاص بك. جميع مكونات Aspose، عند التثبيت، تعمل في وضع التقييم. وضع التقييم لا يحتوي على حد زمني ويقوم فقط بحقن العلامات المائية إلى الوثائق المنتجة.
  3. إنشاء مشروع. ابدأ Visual Studio.NET وأنشئ تطبيق وحدة تحكم جديد. يخلق هذا المثال تطبيق وحدة تحكم بايثون، لكن يمكنك أيضًا استخدام VB.NET.
  4. إضافة المراجع. أضف مرجعًا إلى Aspose.Cells لمشروعك.
  5. *تطبيق التنسيق الشرطي بناءً على قيمة الخلية. أدناه هو الكود المستخدم لإنجاز المهمة. يتم تطبيق التنسيق الشرطي على خلية.
from aspose.cells import CellArea, FormatConditionType, OperatorType, SaveFormat, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca.start_row = 0
ca.end_row = 0
ca.start_column = 0
ca.end_column = 0
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.CELL_VALUE, OperatorType.BETWEEN, "50", "100")
# Sets the background color.
fc = fcs[conditionIndex]
fc.style.background_color = Color.red
# Saving the Excel file
workbook.save(dataDir + "output.out.xls", SaveFormat.AUTO)

عند تنفيذ الكود أعلاه، سيتم تطبيق التنسيق الشرطي على الخلية “A1” في ورقة العمل الأولى للملف الناتج (output.xls). التنسيق الشرطي الذي تم تطبيقه على A1 يعتمد على قيمة الخلية. إذا كانت قيمة الخلية A1 بين 50 و 100، سيكون لون الخلفية أحمرًا بسبب التنسيق الشرطي المطبق.

استخدام Aspose.Cells لتطبيق التنسيق الشرطي بناءً على الصيغة

  1. تطبيق التنسيق الشرطي اعتمادًا على الصيغة (كود مصغر) أدناه هو الكود لإنجاز المهمة. يتم تطبيق التنسيق الشرطي على B3.
from aspose.cells import CellArea, FormatConditionType, SaveFormat, Workbook
from aspose.pydrawing import Color
from os import os, path
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Create directory if it is not already present.
IsExists = path.isdir(dataDir)
if notIsExists:
os.makedirs(dataDir)
# Instantiating a Workbook object
workbook = Workbook()
sheet = workbook.worksheets[0]
# Adds an empty conditional formatting
index = sheet.conditional_formattings.add()
fcs = sheet.conditional_formattings[index]
# Sets the conditional format range.
ca = CellArea()
ca = CellArea()
ca.start_row = 2
ca.end_row = 2
ca.start_column = 1
ca.end_column = 1
fcs.add_area(ca)
# Adds condition.
conditionIndex = fcs.add_condition(FormatConditionType.EXPRESSION)
# Sets the background color.
fc = fcs[conditionIndex]
fc.formula1 = "=IF(SUM(B1:B2)>100,TRUE,FALSE)"
fc.style.background_color = Color.red
sheet.cells.get("B3").formula = "=SUM(B1:B2)"
sheet.cells.get("C4").put_value("If Sum of B1:B2 is greater than 100, B3 will have RED background")
# Saving the Excel file
workbook.save(dataDir + "output.out.xls", SaveFormat.AUTO)

عند تنفيذ الكود أعلاه، سيتم تطبيق التنسيق الشرطي على الخلية “B3” في الورقة العمل الأولى للملف الناتج (output.xls). يعتمد التنسيق الشرطي المطبق على الصيغة التي تحسب قيمة “B3” كمجموع B1 و B2.