Zebra Stripes on Google Apps Spreadsheets

1321970017000 » Tagged as: cloud , Google Apps , JavaScript

It was a bit of a surprise to find that there is no easy way to add zebra stripes to a Google spreadsheet. There is a template with Zebra stripes added in.  But if you sort the spreadsheet, you lose the stripes, or it gets randomized. However I did run across a small script by Otávio A. Ribeiro that allows you to add stripes to any spreadsheet. But by the author's own admission it's rather slow.  So I took a couple of minutes to improve it and here is the effort:

 

function zebraStripe()
{
  var i, thisRow;
  var s = SpreadsheetApp.getActiveSheet();
  var aWhites = new Array(1);
  var aGrays = new Array(1);
  var lastCol = s.getMaxRows();
  aWhites = "#FAFAFA";;
  aGrays = "white";

  for (i = 1; i <= lastCol; i++)
  {
    thisRow = s.getRange(i, 1, 1, lastCol);
    thisRow.setBackgroundColor((i % 2 == 0)?aWhites:aGrays);
  }
}

If you add zebra stripes to a spreadsheet with thousands of rows, it will not update at the blink of an eye (apps scripts rarely do) but it's still reasonably quick. Certainly quick enough to add a trigger and make the stripes update automatically each time you sort.

comments powered by Disqus