Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing background color of specific cell on Google sheet

Tags:

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");      }    } } 
like image 414
Dom Avatar asked Aug 25 '17 19:08

Dom


People also ask

How do I color half cells in Google Sheets?

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.

How do I select only certain cells in Google Sheets to color?

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.


1 Answers

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');      }   } } 
like image 186
Ritesh Nair Avatar answered Sep 19 '22 06:09

Ritesh Nair