Come aggiungere la formattazione condizionale del testo

Possibili Scenari di Utilizzo

Usare la formattazione condizionale basata su testo nei fogli di calcolo è utile per evidenziare le celle che soddisfano criteri testuali specifici. Questo può migliorare l’analisi dei dati e rendere più facile trovare informazioni chiave in un grande set di dati. Ecco alcune ragioni per usare la formattazione condizionale del testo:

  1. Evidenziare testo specifico: Puoi applicare formattazione basata su parole, frasi o caratteri specifici. Per esempio, potresti voler evidenziare tutte le celle che contengono la parola “Urgente” o “Completato” per differenziare facilmente i compiti in un progetto.
  2. Identificare schemi o tendenze: Se lavori con categorie o stati (come “Alto”, “Medio”, “Basso”), la formattazione condizionale del testo può distinguere visivamente tra di loro, facilitando il monitoraggio dei progressi o la prioritizzazione dei compiti.
  3. Avvisi di errore o inserimento dati: La formattazione del testo può segnalare inserimenti incoerenti o errati, come parole mal scritte, testo incompleto o valori incorretti. Questo è particolarmente utile in set di dati con molto input testuale.
  4. Maggiore leggibilità: Codificare a colori il testo o cambiarne lo stile (grassetto, corsivo, ecc.) aiuta a far risaltare le informazioni importanti, migliorando la leggibilità complessiva del foglio.
  5. Feedback dinamico: Puoi impostare regole che regolano automaticamente la formattazione quando il testo corrisponde a determinate condizioni. Ciò significa che non devi aggiornare manualmente la formattazione man mano che i dati cambiano.

In sostanza, la formattazione condizionale del testo ti aiuta a individuare rapidamente informazioni rilevanti, errori e tendenze, rendendola uno strumento potente per gestire e interpretare dati testuali.

Come aggiungere la formattazione condizionale del testo usando Excel

Per aggiungere una formattazione condizionale basata su testo in Excel, segui questi passaggi:

  1. Seleziona l’intervallo di celle: Evidenzia le celle su cui vuoi applicare la formattazione condizionale.
  2. Apri il menu di formattazione condizionale: Vai alla scheda Home nella barra di Excel. Clicca su Formattazione condizionale nel gruppo “Stili”.
  3. Scegli “Nuova regola”: Dal menu a discesa, seleziona Nuova regola.
  4. Seleziona “Formatta solo le celle che contengono”: Nella finestra di dialogo Nuova regola di formattazione, scegli Formatta solo le celle che contengono sotto la sezione “Seleziona un tipo di regola”.
  5. Imposta i criteri della regola: Nella sezione “Formatta celle con”, scegli Testo specifico dal menu a discesa. Seleziona contiene, inizia con o termina con, a seconda della condizione che vuoi applicare. Inserisci il testo che desideri formattare (ad esempio, una parola specifica come “Urgente” o “Completato”).
  6. Scegli la formattazione: Clicca sul pulsante Formatta. Nella finestra di dialogo Formato celle, puoi selezionare il colore carattere, il colore riempimento o altre opzioni di formattazione preferite.
  7. Applica la regola: Una volta impostato il formato desiderato, clicca su OK per applicare la regola. Clicca nuovamente OK nella finestra di dialogo della Nuova regola di formattazione per chiuderla.
  8. Visualizza i risultati: Le celle contenenti il testo specificato avranno ora la formattazione applicata, facilitando l’individuazione delle informazioni rilevanti.

Come aggiungere la formattazione condizionale del testo usando Aspose.Cells for .NET

Aspose.Cells supporta pienamente la formattazione condizionale fornita da Microsoft Excel 2007 e versioni successive in formato XLSX sulle celle durante l’esecuzione. Questo esempio dimostra esercizi avanzati di formattazione condizionale includendo BeginsWith, ContainsBlank, ContainsText e altri.

Formatta la cella quando il valore inizia con il testo specificato

// 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;
}

Formatta la cella quando il valore contiene uno spazio vuoto

// 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");
}

Formatta la cella quando il valore contiene errori

// 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");
}

Formatta la cella quando il valore contiene testo specificato

// 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");
}

Formatta cella quando il valore contiene valori duplicati

// 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");
}

Formatta cella quando il valore termina con testo specificato

// 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");
}

Formatta cella quando il valore non contiene vuoto

// 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");
}

Formatta cella quando il valore non contiene errori

// 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");
}

Formatta cella quando il valore non contiene testo specificato

// 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");
}

Formatta cella quando il valore contiene valori unici

// 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");
}