Change alternate row background colors in Spreadsheet using Google Apps Script

Here is a Google Apps Script that would set the background color of alternate rows in Google Spreadsheet. This script would set the background color of of the selection, this may not be an efficient script but would do the job.

Let us take the following sample data which will be used by the script for setting the background color.

Install the below script and select the data by excluding the Header columns and execute the script.

function alternateColor() {
  var totalRows = SpreadsheetApp.getActiveRange().getNumRows();
  var totalColumns = SpreadsheetApp.getActiveRange().getNumColumns();
  var startRow = SpreadsheetApp.getActiveRange().getRow();
  var startColumn = SpreadsheetApp.getActiveRange().getColumn();
  var sheet = SpreadsheetApp.getActiveSheet();
  var row = startRow;
  while (row < totalRows+startRow)
  {
    var column = startColumn
    while (column < totalColumns+startColumn){
      if(row%2 == 0){
           sheet.getRange(row, column).setBackgroundColor(“lightgrey”);
      }
      column++;
  }
    row++;
  }
}

After executing the script, the sample data would look as shown below.

If you want to change the white color then you can add an else statement to set the background color for odd row numbers as well.

if(row%2 == 0){
           sheet.getRange(row, column).setBackgroundColor(“lightgrey”);
      } else {
          sheet.getRange(row, column).setBackgroundColor(“lightblue”);
}

Let me know if any one have a better suggestionn.

In Category: Technical

Ravi Shankar

A Software developer and blogger who is always looking to provide technical help to the wider community.

Show 7 Comments
  • somnath September 28, 2012, 10:59 am

    This script is not working on google Spreasheet.

    gave error as Illegal character. (line 13)

    Thanks

  • brasilboy November 13, 2012, 9:03 pm

    I had the same problem Somnath. All you need to do is replace the “lightgrey” with “lightgrey”. The quotes are of the wrong character type in the example above. Just replace with the regular double quote ” character.

  • brasilboy November 13, 2012, 9:05 pm

    This website is replacing the regular “quote character” with a fancy one.

  • elango January 10, 2016, 12:37 pm

    Change the single quotes instead of double quotes.

  • Ariel Devulsky January 21, 2016, 3:41 am

    // Fixed script Zebra or Alternate colors

    function onOpen() {
    alternateColor();
    }

    function alternateColor() {

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getSheetByName(“Revisões Periódicas v2”);

    //odd color
    var linecolor = “#a8b7d1”
    //Chosse the range
    var range = sheet.getRange(“B5:U82”);

    //Define start and end… Lines and columns

    var totalRows = range.getNumRows();
    var totalColumns = range.getNumColumns();
    var startRow = range.getRow();
    var startColumn = range.getColumn();
    var row = startRow;

    while (row < totalRows+startRow)
    {
    var column = startColumn;
    while (column < totalColumns+startColumn){
    if( row%2 == 1 ) {
    sheet.getRange(row, column).setBackground(linecolor)
    }
    column++;
    }
    row++;
    }
    }

  • Ravi Shankar January 22, 2016, 12:34 pm

    Thanks

Leave a Comment

Get your free copies of the following tech guides by joining the Digital Answers mailing list.