كيفية إضافة التنسيق الشرطي للنص

سيناريوهات الاستخدام المحتملة

استخدام التنسيق الشرطي المبني على النص في جداول البيانات مفيد لتسليط الضوء على الخلايا التي تلبي معايير نصية معينة. يمكن أن يحسن تحليل البيانات ويسهل العثور على المعلومات الرئيسية في مجموعة بيانات كبيرة. إليك بعض الأسباب لاستخدام التنسيق الشرطي للنص:

  1. تسليط الضوء على نص معين: يمكنك تطبيق تنسيق استنادًا إلى كلمات أو عبارات أو رموز محددة. على سبيل المثال، قد ترغب في تمييز جميع الخلايا التي تحتوي على كلمة “عاجل” أو “مكتمل” لتمييز المهام في مشروع معين.
  2. التعرف على الأنماط أو الاتجاهات: إذا كنت تعمل مع فئات أو حالات (مثل “عالي”، “متوسط”، “منخفض”)، يمكن للتنسيق الشرطي النصي أن يميزها بصريًا، مما يسهل تتبع التقدم أو تحديد أولويات المهام.
  3. تنبيهات الأخطاء أو إدخال البيانات: يمكن أن يميز التنسيق النصي الإدخالات غير المتناسقة أو الخاطئة، مثل الأخطاء الإملائية، النص غير المكتمل، أو القيم غير الصحيحة. هذا مفيد بشكل خاص في مجموعات البيانات التي تحتوي على الكثير من الإدخالات النصية.
  4. تحسين قابلية القراءة: تساعد ترميز الألوان للنص أو تغيير نمطه (غامق، مائل، إلخ) على إبراز المعلومات المهمة، مما يحسن من السهولة العامة لقراءة الجدول الخاص بك.
  5. ردود فعل ديناميكية: يمكنك إعداد قواعد تعدل التنسيق تلقائيًا عندما يتطابق النص مع شروط معينة. هذا يعني أنك لست بحاجة إلى تحديث التنسيق يدويًا مع تغير البيانات.

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

كيفية إضافة التنسيق الشرطي للنص باستخدام Excel

لإضافة تنسيق شرطي يعتمد على النص في Excel، اتبع الخطوات التالية:

  1. حدد نطاق الخلايا: ظلل الخلايا التي تريد تطبيق التنسيق الشرطي عليها.
  2. افتح قائمة التنسيق الشرطي: انتقل إلى علامة التبويب الصفحة الرئيسية في شريط Excel. انقر على التنسيق الشرطي في مجموعة “الأساليب”.
  3. اختر “قاعدة جديدة”: من القائمة المنسدلة، اختر قاعدة جديدة.
  4. اختر “تنسق فقط الخلايا التي تحتوي”: في مربع حوار قاعدة التنسيق الجديدة، اختر تنسيق فقط الخلايا التي تحتوي تحت قسم “حدد نوع القاعدة”.
  5. حدد معايير القاعدة: في قسم “تنسق خلايا تحتوي على”، اختر نص محدد من القائمة المنسدلة. اختر إما يحتوي على، يبدأ بـ، أو ينتهي بـ، اعتمادًا على الشرط الذي تريد تطبيقه. أدخل النص الذي تريد تنسيقه (مثل كلمة محددة مثل “عاجل” أو “مكتمل”).
  6. اختر تنسيق الخلية: اضغط على زر تنسيق. في مربع حوار تنسيق الخلايا، يمكنك اختيار لون الخط، لون التعبئة، أو أي خيارات تنسيق أخرى تفضلها.
  7. طبق القاعدة: بمجرد تعيين التنسيق المطلوب، اضغط على موافق لتطبيق القاعدة. ثم اضغط على موافق مرة أخرى في مربع حوار قاعدة التنسيق الجديدة لإغلاقه.
  8. عرض النتائج: ستحتوي الخلايا التي تحتوي على النص المحدد الآن على التنسيق الذي تم تطبيقه، مما يسهل عليك رؤية المعلومات ذات الصلة.

كيفية إضافة التنسيق الشرطي للنص باستخدام Aspose.Cells for .NET

يدعم Aspose.Cells تمامًا التنسيق الشرطي الذي توفره Microsoft Excel 2007 والإصدارات الأحدث بصيغة XLSX على الخلايا أثناء التشغيل. توضح هذه الأمثلة تمرينًا حول أنواع التنسيق الشرطي المتقدمة بما في ذلك BeginsWith، ContainsBlank، ContainsText وغيرها.

تنسيق الخلية عندما يبدأ القيمة بالنص المحدد

// This method implements the BeginsWith conditional formatting type.
private void AddBeginWith()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E15:G16", Color.LightGoldenrodYellow, _sheet);
int idx = conds.AddCondition(FormatConditionType.BeginsWith);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
cond.Text = "ab";
Cell c = _sheet.Cells["E15"];
c.PutValue("abc");
c = _sheet.Cells["G16"];
c.PutValue("babx");
book.Save("BeginsWith.xlsx");
}
// This method adds formatted conditions.
private FormatConditionCollection GetFormatCondition(string cellAreaName, Color color, Worksheet _sheet)
{
// Adds an empty conditional formattings
int index = _sheet.ConditionalFormattings.Add();
// Get the formatted conditions
FormatConditionCollection formatConditions = _sheet.ConditionalFormattings[index];
// Get the cell area calling the custom GetCellAreaByName method
CellArea area = GetCellAreaByName(cellAreaName);
// Add the formatted conditions cell area.
formatConditions.AddArea(area);
// Call the custom FillCell method
FillCell(cellAreaName, color, _sheet);
// Return the formatted conditions
return formatConditions;
}
// This method specifies the cell shading color for the conditional formattings cellarea range.
private void FillCell(string cellAreaName, Color color, Worksheet _sheet)
{
CellArea area = GetCellAreaByName(cellAreaName);
int k = 0;
for (int i = area.StartColumn; i <= area.EndColumn; i++)
{
for (int j = area.StartRow; j <= area.EndRow; j++)
{
Cell c = _sheet.Cells[j, i];
if (!color.IsEmpty)
{
Style s = c.GetStyle();
s.ForegroundColor = color;
s.Pattern = BackgroundType.Solid;
c.SetStyle(s);
}
// Set some random values to the cells in the cellarea range
int value = j + i + k;
c.PutValue(value);
k++;
}
}
}
// This method specifies the CellArea range (start row, start col, end row, end col etc.)
// For the conditional formatting
internal static CellArea GetCellAreaByName(string s)
{
CellArea area = new CellArea();
string[] strCellRange = s.Replace("$", "").Split(':');
int column;
CellsHelper.CellNameToIndex(strCellRange[0], out area.StartRow, out column);
area.StartColumn = column;
if (strCellRange.Length == 1)
{
area.EndRow = area.StartRow;
area.EndColumn = area.StartColumn;
}
else
{
CellsHelper.CellNameToIndex(strCellRange[1], out area.EndRow, out column);
area.EndColumn = column;
}
return area;
}

تنسيق الخلية عندما تحتوي القيمة على فارغ

// This method implements the ContainsBlank conditional formatting type.
private void AddContainsBlank()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E9:G10", Color.LightBlue, _sheet);
int idx = conds.AddCondition(FormatConditionType.ContainsBlanks);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E9"];
c.PutValue(" ");
c = _sheet.Cells["G10"];
c.PutValue(" ");
book.Save("ContainsBlank.xlsx");
}

تنسيق الخلية عندما تحتوي القيمة على أخطاء

// This method implements the ContainsErrors conditional formatting type.
private void AddContainsErrors()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E17:G18", Color.LightSkyBlue, _sheet);
int idx = conds.AddCondition(FormatConditionType.ContainsErrors);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E17"];
c.PutValue(" ");
c = _sheet.Cells["G18"];
c.PutValue(" ");
book.Save("ContainsErrors.xlsx");
}

تنسيق الخلية عندما تحتوي القيمة على نص محدد

// This method implements the ContainsText conditional formatting type.
private void AddContainsText()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E5:G6", Color.LightBlue, _sheet);
int idx = conds.AddCondition(FormatConditionType.ContainsText);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
cond.Text = "1";
book.Save("ContainsText.xlsx");
}

تنسيق الخلية عندما تحتوي القيمة على قيم مكررة

// This method implements the DuplicateValues conditional formatting type.
private void AddDuplicateValues()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E23:G24", Color.LightSlateGray, _sheet);
int idx = conds.AddCondition(FormatConditionType.DuplicateValues);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E23"];
c.PutValue("bb");
c = _sheet.Cells["G24"];
c.PutValue("bb");
book.Save("DuplicateValues.xlsx");
}

تنسيق الخلية عندما تنتهي القيمة بنص معين

// This method implements the EndsWith conditional formatting type.
private void AddEndWith()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E13:G14", Color.LightGray, _sheet);
int idx = conds.AddCondition(FormatConditionType.EndsWith);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
cond.Text = "ab";
Cell c = _sheet.Cells["E13"];
c.PutValue("nnnab");
c = _sheet.Cells["G14"];
c.PutValue("mmmabc");
book.Save("EndsWith.xlsx");
}

تنسيق الخلية عندما لا تحتوي القيمة على فارغ

// This method implements the NotContainsBlank conditional formatting type.
private void AddNotContainsBlank()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E11:G12", Color.LightCoral, _sheet);
int idx = conds.AddCondition(FormatConditionType.NotContainsBlanks);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E11"];
c.PutValue("abc");
c = _sheet.Cells["G12"];
c.PutValue(" ");
book.Save("NotContainsBlank.xlsx");
}

تنسيق الخلية عندما لا تحتوي القيمة على أخطاء

// This method implements the NotContainsErrors conditional formatting type.
private void AddNotContainsErrors()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E19:G20", Color.LightSeaGreen, _sheet);
int idx = conds.AddCondition(FormatConditionType.NotContainsErrors);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E19"];
c.PutValue(" ");
c = _sheet.Cells["G20"];
c.PutValue(" ");
book.Save("NotContainsErrors.xlsx");
}

تنسيق الخلية عندما لا تحتوي القيمة على نص معين

// This method implements the NotContainsText conditional formatting type.
private void AddNotContainsText()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E7:G8", Color.LightCoral, _sheet);
int idx = conds.AddCondition(FormatConditionType.NotContainsText);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Pink;
cond.Style.Pattern = BackgroundType.Solid;
cond.Text = "3";
book.Save("NotContainsText.xlsx");
}

تنسيق الخلية عندما تحتوي القيمة على قيم فريدة

// This method implements the UniqueValues conditional formatting type.
private void AddUniqueValues()
{
// Instantiate a workbook object
Workbook book = new Workbook();
// Create a worksheet object and get the first worksheet
Worksheet _sheet = book.Worksheets[0];
FormatConditionCollection conds = GetFormatCondition("E21:G22", Color.LightSalmon, _sheet);
int idx = conds.AddCondition(FormatConditionType.UniqueValues);
FormatCondition cond = conds[idx];
cond.Style.BackgroundColor = Color.Yellow;
cond.Style.Pattern = BackgroundType.Solid;
Cell c = _sheet.Cells["E21"];
c.PutValue("aa");
c = _sheet.Cells["G22"];
c.PutValue("aa");
book.Save("UniqueValues.xlsx");
}