Last active
December 13, 2024 11:38
-
-
Save pfelipm/2f2ef24c25ec21123502881faf57152e to your computer and use it in GitHub Desktop.
Ejemplo de generación automatizada de reglas de formato condicional en Google Sheets con Apps Script | Example of automatically generating conditional formatting rules in Google Sheets using Apps Script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Genera reglas de formato condicional sobre las celdas indicadas para modificar | |
* sus colores de fondo de acuerdo con la paleta seleccionada en el desplegable de referencia | |
* @OnlyCurrentDoc | |
*/ | |
function generaReglasFormatoCondicional() { | |
const hoja = SpreadsheetApp.getActive().getSheetByName('SPARKLINE_BARRAS'); | |
const celdasObjetivo = ['C5', 'D5', 'E5', 'F5', 'G5']; | |
const paletas = [ | |
['fríocalor', '4285F4', '46BDC6', 'FBBC04', 'FF6D01', 'EA4335'], | |
['esmeralda', 'BCF7C6', '8DE3A4', '6BB57A', '5C9C67', '4E8072'], | |
['aire', 'BCE0F7', '8DB8E3', '6B95B5', '5C829C', '4E5080'], | |
['berenjena', 'E1C2D2', 'C391AD', '9A7186', '846274', '734E61'], | |
['lavanda', 'D0BCF8', 'B28DE3', '866BB6', '715B9B', '7C4E80'], | |
['musgo', 'D5E0C1', 'B3C28F', '8B9970', '798562', '66734E'], | |
['kaki', 'E0CEC1', 'C2A28F', '998070', '857062', '735D4E'], | |
['oro', 'E6DCB8', 'CFBD7E', 'B09C53', '998746', '85753A'], | |
['rosa', 'E9B9C5', 'D37E99', 'B6536E', '9F465C', '89394E'], | |
['violeta', 'BBB7E5', '867FCF', '5B53AF', '4D4699', '403A85'], | |
['cacao', 'F0C9AD', 'E39C69', 'D17534', 'B7642A', '9B5422'], | |
['plata', 'CFCFCF', 'A6A6A6', '828282', '707070', '5E5E5E'], | |
['rubí', 'F0ADAD', 'E36868', 'D92B2B', 'BF2222', '9E1C1C'] | |
]; | |
const celdaReferencia = 'C4'; | |
// Obtiene todas las reglas de formato condicional de la hoja que no afectan a las celdas objetivo, | |
// no discrimina intervalos mayores a una celda (por ejemplo, no detecta A1 dentro de A1:A5), en este | |
// caso no es un problema. | |
const reglasFormatoCondicional = hoja.getConditionalFormatRules() | |
.filter(regla => regla.getRanges().every(rango => !celdasObjetivo.includes(rango.getA1Notation()))); | |
console.info(reglasFormatoCondicional.map(regla => regla.getRanges().map(rango => rango.getA1Notation()))) | |
// Genera reglas de formato condicional para las celdas objetivo | |
paletas.forEach(paleta => celdasObjetivo.forEach((celda, indiceCelda) => { | |
reglasFormatoCondicional.push( | |
SpreadsheetApp.newConditionalFormatRule() | |
.setRanges([hoja.getRange([celda])]) | |
.whenFormulaSatisfied(`=LOWER(${celdaReferencia})="${paleta[0]}"`) // aquí el nombre de la función puede ir también en [ES] | |
.setBackground(`#${paleta[indiceCelda + 1]}`) | |
.build() | |
); | |
})); | |
// Actualiza las reglas de formato de la hoja | |
hoja.setConditionalFormatRules(reglasFormatoCondicional); | |
} | |
/** | |
* Generates conditional formatting rules on the specified cells to modify | |
* their background colors according to the selected palette in the reference dropdown | |
* @OnlyCurrentDoc | |
*/ | |
function generateConditionalFormattingRules() { | |
const sheet = SpreadsheetApp.getActive().getSheetByName('SPARKLINE_BARRAS'); | |
const targetCells = ['C5', 'D5', 'E5', 'F5', 'G5']; | |
const palettes = [ | |
['fríocalor', '4285F4', '46BDC6', 'FBBC04', 'FF6D01', 'EA4335'], | |
['esmeralda', 'BCF7C6', '8DE3A4', '6BB57A', '5C9C67', '4E8072'], | |
['aire', 'BCE0F7', '8DB8E3', '6B95B5', '5C829C', '4E5080'], | |
['berenjena', 'E1C2D2', 'C391AD', '9A7186', '846274', '734E61'], | |
['lavanda', 'D0BCF8', 'B28DE3', '866BB6', '715B9B', '7C4E80'], | |
['musgo', 'D5E0C1', 'B3C28F', '8B9970', '798562', '66734E'], | |
['kaki', 'E0CEC1', 'C2A28F', '998070', '857062', '735D4E'], | |
['oro', 'E6DCB8', 'CFBD7E', 'B09C53', '998746', '85753A'], | |
['rosa', 'E9B9C5', 'D37E99', 'B6536E', '9F465C', '89394E'], | |
['violeta', 'BBB7E5', '867FCF', '5B53AF', '4D4699', '403A85'], | |
['cacao', 'F0C9AD', 'E39C69', 'D17534', 'B7642A', '9B5422'], | |
['plata', 'CFCFCF', 'A6A6A6', '828282', '707070', '5E5E5E'], | |
['rubí', 'F0ADAD', 'E36868', 'D92B2B', 'BF2222', '9E1C1C'] | |
]; | |
const referenceCell = 'C4'; | |
// Gets all conditional formatting rules from the sheet that do not affect the target cells, | |
// does not discriminate intervals larger than one cell (for example, does not detect A1 within A1:A5), in this | |
// case this is not a problem. | |
const conditionalFormattingRules = sheet.getConditionalFormatRules() | |
.filter(rule => rule.getRanges().every(range => !targetCells.includes(range.getA1Notation()))); | |
console.info(conditionalFormattingRules.map(rule => rule.getRanges().map(range => range.getA1Notation()))) | |
// Generates conditional formatting rules for the target cells | |
palettes.forEach(palette => targetCells.forEach((cell, cellIndex) => { | |
conditionalFormattingRules.push( | |
SpreadsheetApp.newConditionalFormatRule() | |
.setRanges([sheet.getRange([cell])]) | |
.whenFormulaSatisfied(`=LOWER(${referenceCell})="${palette[0]}"`) // here the function name can also go in [ES] | |
.setBackground(`#${palette[cellIndex + 1]}`) | |
.build() | |
); | |
})); | |
// Updates the sheet's formatting rules | |
sheet.setConditionalFormatRules(conditionalFormattingRules); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment