2

I am trying to write a custom function for Google spreadsheets that checks the formatting of a given cell and acts accordingly. I know that I can give a string as argument and get it to work but then I have to rewrite the command for each cell I want to use it in instead of just dragging it out over several cells.

That is, I can write:

 =IFBOLD("A1", val1, val2) 

but I want to be able to write:

 =IFBOLD(A1, val1, val2).

Now the script looks like this:

function IFBOLD(a1Notation,trueval, falseval) {
  var cell = SpreadsheetApp.getActiveSpreadsheet().getRange(a1Notation);

  if(cell.getFontWeight() == 'bold')
    return trueval;
  return falseval;

};

Any ideas on how to solve my problem?

1

With this little snippet you can do that.

Code

function isBold(startcol, startrow) {
  // prepare string
  var str = String.fromCharCode(64 + startcol) + startrow;

  // retrieve font weight
  var bCell = SpreadsheetApp.getActiveSpreadsheet()
    .getRange(str).getFontWeight();

  // return true if is bold 
  return bCell == 'bold' ? true : false;
}

Screenshot

enter image description here

In this answer, Tom Horwood found a way to circumvent the usage of quotes.

Example

I've created an example file for you: Check for Font Weight
Add the script under Tools>Script editor and press the bug button to activate the authentication.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.