Scopo di questo progetto è potenziare uno spreadsheet Google con funzionalità avanzate di formattazione condizionale.
L’idea è quella di aggiungere 4 nuove voci al menu.
con l’obbiettivo di ottenere il seguente risultato:
Realizzazione.
I metodi principali sono tre. Il primo si occupa di aggiungere le nuove voci ai menu.
function onOpen() { var sheet = SpreadsheetApp.getActiveSpreadsheet(); var entries = [ { name : "Color Scale - Negative/Positive (Red/Green)", functionName : "FormatAsSemaphore" }, { name : "Color Scale - Gradient (Red/Yellow/Green)", functionName : "FormatAsGradientRYG" }, { name : "Color Scale - Gradient (Yellow/Green)", functionName : "FormatAsGradientYG" }, { name : "Color Scale - Gradient (White/Green)", functionName : "FormatAsGradientWG" }, null, { name : "About", functionName : "About" } ]; sheet.addMenu("Conditional Formatting", entries); };
Il secondo individua le celle soggette alla variazione del colore.
function FormatAsSemaphore() { var sheet = SpreadsheetApp.getActiveSheet(); var activeRange = sheet.getActiveRange(); var numRows = activeRange.getNumRows(); var numCols = activeRange.getNumColumns() var grid = new Array(); Log("FormatAsSemaphore Rows:" + numRows + ", Cols:" + numCols); for (var r = 1; r <= numRows; r++) { var row = new Array(); for (var c = 1; c <= numCols; c++) row[c-1] = Semaphore(activeRange.getCell(r,c).getValue()); grid[r-1] = row; } activeRange.setBackgrounds(grid); }
Il terzo calcola il colore da applicare partendo dal valore della cella.
function Semaphore(value) { var color = "#00FF00"; if (value < 0) color = "#FF0000"; else if (!IsNumber(value)) color = ""; return color; }Qui lo spreadsheet con il sorgente . Una volta visualizzato effettuatene una copia.