Hur man lägger till textbaserad villkorsstyrd formatering
Möjliga användningsscenario
Att använda textbaserad villkorsstyrd formatering i kalkylblad är användbart för att markera celler som uppfyller specifika textkrav. Detta kan förbättra dataanalys och göra det lättare att hitta nyckelinformation i en stor datamängd. Här är några anledningar till att använda textbaserad villkorsstyrd formatering:
- Markera särskild text: Du kan tillämpa formatering baserat på specifika ord, fraser eller tecken. Till exempel kan du vilja markera alla celler som innehåller ordet “Brådskande” eller “Avslutad” för att enkelt särskilja uppgifter i ett projekt.
- Identifiera mönster eller trender: Om du arbetar med kategorier eller statusar (som “Hög”, “Medium”, “Låg”) kan textbaserad villkorsstyrd formatering visuellt skilja mellan dem, vilket gör det lättare att följa framsteg eller prioritera uppgifter.
- Fel- eller dataregistreringsvarningar: Textformatering kan flagga inkonsekventa eller felaktiga inmatningar, som stavfel, ofullständig text eller felaktiga värden. Detta är särskilt användbart i dataset med mycket textinmatning.
- Förbättrad läsbarhet: Färgmärkning av text eller ändring av stil (fet, kursiv osv.) hjälper till att framhäva viktig information, vilket förbättrar det övergripande läsbarheten.
- Dynaisk feedback: Du kan skapa regler som automatiskt justerar formateringen när text matchar vissa villkor. Detta innebär att du inte behöver uppdatera formateringen manuellt när data ändras.
Kort sagt hjälper textbaserad villkorsstyrd formatering dig att snabbt upptäcka relevant information, fel och trender, vilket gör det till ett kraftfullt verktyg för att hantera och tolka textuell data.
Hur man lägger till textvillkorsstyrd formatering med Excel
För att lägga till textbaserad villkorsstyrd formatering i Excel, följ dessa steg:
- Markera cellområdet: Markera de celler där du vill tillämpa villkorsstyrd formatering.
- Öppna menyn Villkorsstyrd formatering: Gå till fliken Start i Excel-rollen. Klicka på Villkorsstyrd formatering i “Stilar”-gruppen.
- Välj “Nytt regel”: Från rullgardinsmenyn, välj Ny regel.
- Välj “Formatera endast celler som innehåller”: I dialogrutan Ny formatregel, välj Formatera endast celler som innehåller under avsnittet “Välj regeltyp”.
- Ange regelkriterier: I avsnittet “Formatera celler med”, välj Specifik text från rullgardinen. Välj antingen innehåller, börjar med eller slutar med, beroende på villkoret du vill tillämpa. Ange den text du vill formatera (t.ex. ett specifikt ord som “Brådskande” eller “Avslutad”).
- Välj formateringen: Klicka på knappen Formatera. I dialogrutan Formatera celler kan du välja teckensfärg, fyllnadsfärg eller andra formateringsalternativ.
- Tillämpa regeln: När du har ställt in önskad formatering klickar du OK för att tillämpa regeln. Klicka på OK igen i dialogrutan Ny formatregel för att stänga den.
- Visa resultaten: Cellerna som innehåller den angivna texten kommer nu att ha den tillämpliga formateringen, vilket gör det enkelt att hitta relevant information.
Hur man lägger till textvillkorsstyrd formatering med Aspose.Cells for .NET
Aspose.Cells stöder fullt ut den villkorsstyrda formatering som tillhandahålls av Microsoft Excel 2007 och senare versioner i XLSX-format på cellnivå i realtid. Detta exempel demonstration för avancerade typer av villkorsstyrd formatering inklusive Börjar med, Innehåller tomt, Innehåller text och så vidare.
Formatera cell när värdet börjar med specifik text
// 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; | |
} |
Formatera cell när värdet innehåller tomt
// 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"); | |
} |
Formatera cell när värdet innehåller fel
// 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"); | |
} |
Formatera cell när värdet innehåller angiven text
// 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"); | |
} | |
Formatera cell när värdet innehåller dubbletter
// 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"); | |
} |
Formatera cell när värdet slutar med specificerad text
// 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"); | |
} |
Formatera cell när värdet inte innehåller tomt
// 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"); | |
} |
Formatera cell när värdet inte innehåller fel
// 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"); | |
} |
Formatera cell när värdet inte innehåller specificerad text
// 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"); | |
} |
Formatera cell när värdet innehåller unika värden
// 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"); | |
} |