Cómo agregar formateo condicional de valores por encima del promedio
Escenarios de uso posibles
Usar formato condicional de por encima del promedio en herramientas como Microsoft Excel o Google Sheets es una manera rápida y visual de resaltar datos destacados—específicamente, valores que son mayores que el promedio en un rango. Aquí las razones para usarlo:
- Detectar tendencias rápidamente: Te ayuda a identificar instantáneamente valores de alto rendimiento sin calcular manualmente los promedios o escanear números.
- Simplificar análisis de datos: No necesitas calcular ni ingresar fórmulas, es una forma automática de aplicar formato basado en lógica, ahorrando tiempo.
- Mejorar el atractivo visual: La codificación por colores ayuda a que tu hoja de cálculo sea más fácil de leer y visualmente más atractiva, especialmente durante presentaciones.
- Apoyar la toma de decisiones: Identificar rápidamente valores por encima del promedio puede orientar acciones, como recompensar a los que rinden alto o investigar por qué ciertos productos superan a otros.
Cómo agregar formateo condicional de por encima del promedio usando Excel
Para agregar formateo condicional de por encima del promedio en Excel, así es como puedes hacerlo paso a paso:
- Selecciona el rango de celdas al que deseas aplicar el formato. Por ejemplo: A1:A20.
- Ve a la pestaña Inicio en la cinta de opciones.
- Haz clic en Formato condicional en el grupo Estilos.
- Pasa el cursor sobre Reglas superiores/inferiores.
- Haz clic en Por encima del promedio…
- En el cuadro de diálogo que aparece: Detectará automáticamente “Formatar celdas que están POR ENCIMA del media.” Puedes cambiar el estilo de formato haciendo clic en la flecha hacia abajo junto a (por ejemplo, elegir un color de relleno o formato personalizado).
- Haz clic en Aceptar. Todas las celdas en tu rango seleccionado que estén por encima del promedio de ese rango serán resaltadas.
Cómo agregar formateo condicional de por encima del promedio usando Aspose.Cells for .NET
Aspose.Cells admite completamente el formato condicional proporcionado por Microsoft Excel 2007 y versiones posteriores en formato XLSX en las celdas en tiempo de ejecución. Este ejemplo demuestra un ejercicio para el formato condicional de Superiores a Promedio con diferentes conjuntos de atributos.
private void TestAboveAverage() | |
{ | |
// Instantiate a workbook object | |
Workbook book = new Workbook(); | |
// Create a worksheet object and get the first worksheet | |
Worksheet _sheet = book.Worksheets[0]; | |
AddAboveAverage(_sheet); | |
AddAboveAverage2(_sheet); | |
AddAboveAverage3(_sheet); | |
book.Save(filePath + "AboveAverage.xlsx"); | |
} | |
// This method implements the AboveAverage conditional formatting type. | |
private void AddAboveAverage(Worksheet _sheet) | |
{ | |
FormatConditionCollection conds = GetFormatCondition("A11:C12", Color.Tomato, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.AboveAverage); | |
FormatCondition cond = conds[idx]; | |
cond.Style.BackgroundColor = Color.Pink; | |
cond.Style.Pattern = BackgroundType.Solid; | |
} | |
// This method implements an AboveAverage conditional formatting type with some custom attributes. | |
private void AddAboveAverage2(Worksheet _sheet) | |
{ | |
FormatConditionCollection conds = GetFormatCondition("A13:C14", Color.Empty, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.AboveAverage); | |
FormatCondition cond = conds[idx]; | |
cond.AboveAverage.IsAboveAverage = false; | |
cond.AboveAverage.IsEqualAverage = true; | |
cond.Style.BackgroundColor = Color.Pink; | |
cond.Style.Pattern = BackgroundType.Solid; | |
} | |
// This method implements an AboveAverage conditional formatting type with some custom attributes. | |
private void AddAboveAverage3(Worksheet _sheet) | |
{ | |
FormatConditionCollection conds = GetFormatCondition("A15:C16", Color.Empty, _sheet); | |
int idx = conds.AddCondition(FormatConditionType.AboveAverage); | |
FormatCondition cond = conds[idx]; | |
cond.AboveAverage.IsAboveAverage = false; | |
cond.AboveAverage.IsEqualAverage = true; | |
cond.AboveAverage.StdDev = 3; | |
cond.Style.BackgroundColor = Color.Pink; | |
cond.Style.Pattern = BackgroundType.Solid; | |
} | |
// 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; | |
} |