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.
This script is not working on google Spreasheet.
gave error as Illegal character. (line 13)
Thanks
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.
This website is replacing the regular “quote character” with a fancy one.
Change the single quotes instead of double quotes.
// 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++;
}
}
Thanks
You can omit the if-condition if you use
row += 2