Google-sheets – How to send cell as argument for function

google sheetsgoogle-apps-scriptgoogle-sheets-custom-function

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?

Best Answer

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.