Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Reference Cells in Google Apps Scripts

I'm transitioning from knowing VBA to Scripts for spreadsheet management. I created a function to flag cells that contain certain words within my spreadsheet.

How do I reference specific cells as it relates the overall code:

The issue is with the following being undefined.

setBackgroundColor("#99CC99")

Would this be appropriate?:

values[i][0].setBackgroundColor("#99CC99")

Code below:

function foo(){
  var range = SpreadsheetApp.openById("1oh5wXFi4YM1bDvfF27pu1qHDKQEhn02drtfsrRgxArQ").getSheetByName("General").getRange("D:D");
var values = range.getValues();
for(var i in values){
  if(values[i][0].match("Rensselaer")!=null){
     setBackgroundColor("#99CC99")
  } 
  if(values[i][0].match("Albany")!=null){
     setBackgroundColor("#99CC99")
  }
  if(values[i][0].match("Saratoga")!=null){
     setBackgroundColor("#99CC99")
  }
}
}
like image 769
Coding Novice Avatar asked Nov 15 '25 13:11

Coding Novice


2 Answers

setBackgroundColor(color) is a method of the Class Range, in other words, first you should get a range object then you could call that setBackgroundColor(color) for that object

values[i][0] isn't an appropriate way to reference a range because this variable holds cell content not the cell itself.

There are a several ways to reference a range. The basics forms are A1 Notation, and row and column numbers.

A very simple example

function example1(){
  var range = SpreadsheetApp.getActiveRange();
  range.setBackgroundColor('red');
}

Recently the Class RangeList was introduced so now is possible to speedup the algorithms to edit non-adjacent cells.

function example2() {
  var sheet = SpreadsheetApp.getActiveSheet()
  var redList = [];
  var vls = sheet.getRange('A1:A' + sheet.getLastRow()).getValues();
  for(var i = 0; i < vls.length; i++){
    if(vls[i][0]=== 'red') redList.push('A'+ (i + 1));
  }
  var redRangeList = sheet.getRangeList(redList).setBackground('red');
}

Please note that the for loop doesn't make calls to the Google Apps Script classes and methods, it use pure JavaScript which make it faster than other alternatives. Then with a single call the color of several non-adjacent cells is set.

like image 87
Rubén Avatar answered Nov 18 '25 19:11

Rubén


You might like to try it this way:

  function foo(){
  var ss=SpreadsheetApp.openById('1oh5wXFi4YM1bDvfF27pu1qHDKQEhn02drtfsrRgxArQ');
  //var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('General');
  //var sh=ss.getActiveSheet();
  var range=sh.getRange(1,4,sh.getLastRow(),1);
  var values=range.getValues();
  for(var i=0;i<values.length;i++){
    if(values[i][0].match(/Rensselaer|Albany|Saratoga/)){
      sh.getRange(i+1,4).setBackgroundColor("#99CC99");
    } 
  }
}

This works too:

function foo(){
  var ss=SpreadsheetApp.openById('1oh5wXFi4YM1bDvfF27pu1qHDKQEhn02drtfsrRgxArQ');
  //var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('General');
  //var sh=ss.getActiveSheet();
  var range=sh.getRange(1,4,sh.getLastRow(),1);//gets the smallest range that has data in it
  var values=range.getValues();
  var colors=range.getBackgrounds();//Creates the correct size area with the starting colors
  for(var i=0;i<values.length;i++){
    if(values[i][0].match(/Rensselaer|Albany|Saratoga/)){//you can add more alternatives easily
      colors[i][0]='#99cc99';
      //sh.getRange(i+1,4).setBackgroundColor("#99CC99");
    } 
  }
  range.setBackgrounds(colors);
}

Have questions?

Try reading the Google Apps Script Documentation.

like image 36
Cooper Avatar answered Nov 18 '25 19:11

Cooper