Skip to content

Instantly share code, notes, and snippets.

@hhkaos
Created October 7, 2025 08:25
Show Gist options
  • Save hhkaos/00832d4ce7da507e33916f0afd15300f to your computer and use it in GitHub Desktop.
Save hhkaos/00832d4ce7da507e33916f0afd15300f to your computer and use it in GitHub Desktop.
/**
* Devuelve el color de fondo (hex) de una celda.
* Uso: =GETCOLOR("Hoja1!B2")
*/
function GETCOLOR(a1Address) {
if (typeof a1Address !== 'string') return '';
const ss = SpreadsheetApp.getActiveSpreadsheet();
let sheetName = '';
let cellAddress = a1Address;
// Detectar si se incluye nombre de hoja, como "Hoja1!B2"
const match = a1Address.match(/^(.*?)!(.*)$/);
if (match) {
sheetName = match[1].replace(/^'|'$/g, ''); // quitar comillas simples si las hay
cellAddress = match[2];
}
const sheet = sheetName ? ss.getSheetByName(sheetName) : ss.getActiveSheet();
const bg = sheet.getRange(cellAddress).getBackground();
return bg.toLowerCase(); // p. ej. "#ffff00"
}
/**
* Suma valores cuyo fondo coincide con un color o celda.
* Uso:
* =SUMBYCOLOR("Hoja1!A1", "Hoja1!C2:C20", COLOR_TRIGGER)
* =SUMBYCOLOR("#ffff00", "Hoja1!C2:C20", COLOR_TRIGGER)
*/
function SUMBYCOLOR(color, rangeA1, trigger) {
const ss = SpreadsheetApp.getActiveSpreadsheet();
let targetColor = '';
// Resolver color de referencia
if (typeof color === 'string' && color.startsWith('#')) {
targetColor = color.toLowerCase();
} else if (typeof color === 'string') {
// Soporta "Hoja1!A1"
const match = color.match(/^(.*?)!(.*)$/);
const sheetName = match ? match[1].replace(/^'|'$/g, '') : '';
const cellAddress = match ? match[2] : color;
const sheet = sheetName ? ss.getSheetByName(sheetName) : ss.getActiveSheet();
targetColor = sheet.getRange(cellAddress).getBackground().toLowerCase();
} else {
return 0;
}
// Resolver rango a sumar
const match = rangeA1.match(/^(.*?)!(.*)$/);
const sheetName = match ? match[1].replace(/^'|'$/g, '') : '';
const cellAddress = match ? match[2] : rangeA1;
const sheet = sheetName ? ss.getSheetByName(sheetName) : ss.getActiveSheet();
const rng = sheet.getRange(cellAddress);
const values = rng.getValues();
const bgs = rng.getBackgrounds();
let total = 0;
for (let r = 0; r < values.length; r++) {
for (let c = 0; c < values[0].length; c++) {
if (bgs[r][c].toLowerCase() === targetColor && typeof values[r][c] === 'number') {
total += values[r][c];
}
}
}
return total;
}
/**
* Menú y función para forzar recálculo cambiando el valor de un disparador.
*/
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('🧮 Color Tools')
.addItem('Refrescar valores', 'refreshColors')
.addToUi();
}
function refreshColors() {
const ss = SpreadsheetApp.getActive();
const triggerName = 'COLOR_TRIGGER';
let named = null// ss.getNamedRanges().find(n => n.getName() === triggerName);
if (!named) {
const helper = ss.getSheets()[0];
helper.getRange('A2').setValue(new Date());
ss.setNamedRange(triggerName, helper.getRange('A2'));
} else {
named.getRange().setValue(new Date());
}
ss.toast('Valores actualizados');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment