I am working on this script bound to a Google Sheets spreadsheet where I have this function running from a time-driven trigger. I would like to be able to target specific cells on the sheet (if the cell value = "Open") so that I can change the background color of the cell.
I am wondering how can I go about making this work? I am able to target the cell, however, I don't know how to change the property of the cell background as the .setBackground()
cannot be called.
function myColorFunction() { var s = SpreadsheetApp.getActiveSheet(); var ss = SpreadsheetApp.getActiveSpreadsheet(); var range = ss.getSheetByName("Form Responses 1").getRange(2,6,ss.getLastRow()); var cellRange = range.getValues(); Logger.log(cellRange); Logger.log(cellRange.length); Logger.log(cellRange.valueOf()); for(i = 1; i<cellRange.length; i++){ if(cellRange[i] == "Open") { Logger.log("change color here"); } else { Logger.log("don't change color"); } } }
To color a cell or a range of cells in Google Sheets, do the following: Select the cell or range of cells that you want to change the color of. Then click the fill color button/menu found in the toolbar. Then select the color that you want.
With the filters added to your dataset, click one to bring up the menu. Select “Filter by color” and then select to filter on the background cell color or the text color.
You can use setBackground
property with getRange
. Try the below snippet.
function myColorFunction() { var ss = SpreadsheetApp.getActiveSpreadsheet(); var range = ss.getSheetByName("Form Responses 1").getRange(2,6,ss.getLastRow()); var cellRange = range.getValues(); for(i = 0; i<cellRange.length-1; i++){ if(cellRange[i][0] == "Open") { ss.getSheetByName("Form Responses 1").getRange(i+2,6).setBackground("red"); ss.getSheetByName("Form Responses 1").getRange(i+2,6).setFontColor('white'); } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With