Last Updated: February 25, 2016
·
2.373K
· diurnalist

Restripe tables in Google Docs using Apps Scripts

Are you also annoyed that there's no way to add some sort of zebra-striping to tables in a Google Doc? To add this functionality to your document (which provides a way to re-stripe the rows if you add/remove rows), go to Tools > Script Editor inside the document and add this script:

function onOpen() {
  var ui = DocumentApp.getUi();
  ui.createMenu('Macros')
    .addItem('Restripe tables...', 'restripe')
    .addToUi();
}

function restripe() {
  var tables = DocumentApp.getActiveDocument()
    .getBody()
    .getTables();

  var colors = ['#ffffff', '#f0f0f0'];

  var table, numRows, row, numCells, cell, bgCol;
  for (var i = 0; i < tables.length; i++) {
    table = tables[i];
    numRows = table.getNumRows();
    for (var j = 0; j < numRows; j++) {
      row = table.getRow(j);
      bgCol = colors[j % colors.length];
      numCells = row.getNumCells();
      for (var k = 0; k < numCells; k++) {
        cell = row.getCell(k);
        cell.setBackgroundColor(bgCol);
      }
    }
  }
}

Then reload your document. You should now have a new menu item that will re-stripe the rows in all of your tables. Adjust the colors array as you see fit.