Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Script to make cells in a column of a spreadsheet bold based on value. (Conditional formatting?)

I need a script that formats the cells in column A bold, but only the cells that contain the word 'Hello'. Also, a similar script that makes the cells containing 'hello' aligned center and one that also formats underlined. You can't conditionally format bold or align or underline in Google Sheets, so I will need a script.

I think it's something like this but I don't know what the last line would be.

function formatBold() {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var s = ss.getSheetByName('Sheet1');
  var values = s.getDataRange().getValues();

  for( var row = values.length -1; row >= 0; --row )
    if (values[row][0] == 'Hello')

};

Does anyone know how to complete this script or if there is a better script for the job?

like image 237
Vin Avatar asked Jan 17 '13 03:01

Vin


1 Answers

This is pretty well documented in the documentation, you can simply use

// row+1 because array are 0 indexed while range is 1 indexed
s.getRange(row+1,1).setFontWeight("bold");

The other "styles" are available in the same documentation and apply the same way.

EDIT : following your comment, if you want to reset all cells of a specified range to 'normal font' just set to 'normal' in a loop for example. Please have a look at this example sheet (make a copy)

like image 66
Serge insas Avatar answered Sep 28 '22 07:09

Serge insas